Spaces:
Sleeping
Sleeping
File size: 2,793 Bytes
3e54926 0db6ea0 e9af851 0db6ea0 eb13b82 0db6ea0 eb13b82 0db6ea0 eb13b82 0db6ea0 bdfdf41 c38e1e7 bdfdf41 c38e1e7 bdfdf41 0db6ea0 e9af851 0db6ea0 e9af851 0db6ea0 eb13b82 e9af851 0db6ea0 eb13b82 0db6ea0 e9af851 eb13b82 e9af851 0db6ea0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import gradio as gr
import yt_dlp
import os
def download_video(url, request):
"""downlad video and audio from youtube url
Args:
url (str): youtube video url
output_path (str): path to save the downloaded files
Returns:
video_filename (str): path to the downloaded video file
audio_filename (str): path to the downloaded audio file
"""
# instanciate output path
output_path='./cache'
if not os.path.exists(output_path):
os.mkdir(output_path)
# get cookies
export_cookies_path = "./cache/exported_cookies.txt"
os.makedirs(os.path.dirname(export_cookies_path), exist_ok=True)
try:
ydl_opts_export_cookies = {
'cookiesfrombrowser': ('firefox',None,None,None),
'cookiefile': export_cookies_path,
'quiet': True,
}
print(f"Attempting to export cookies from Firefox to {export_cookies_path}...")
with yt_dlp.YoutubeDL(ydl_opts_export_cookies) as ydl:
# A dummy URL is often sufficient for cookie export
ydl.extract_info("https://www.youtube.com", download=False)
print("Cookies exported successfully (if Firefox was installed and logged in).")
except yt_dlp.utils.DownloadError as e:
print(f"Could not export cookies from browser: {e}")
print("Please ensure a supported browser is installed and logged in, or manually create a 'cookies.txt' file.")
# get video
ydl_opts_video = {
'format': 'worst[ext=mp4]',
'outtmpl': output_path+'/video/'+'%(title)s_video.%(ext)s',
'quiet': True
}
print('Downloading video...')
with yt_dlp.YoutubeDL(ydl_opts_video) as ydl:
info_dict = ydl.extract_info(url, download=True)
video_filename = ydl.prepare_filename(info_dict)
# get audio
audio_opts = {
'format': 'bestaudio[ext=m4a]',
'outtmpl': output_path+'/audio/'+'%(title)s.audio.%(ext)s',
'quiet': False,
'noplaylist': True,
}
print('Downloading audio...')
with yt_dlp.YoutubeDL(audio_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
audio_filename = ydl.prepare_filename(info_dict)
return {
"video_path": video_filename,
"audio_path": audio_filename,
"request": request
}
# Create the Gradio interface
demo = gr.Interface(
fn=download_video, # will use function arguments to define inputs names
inputs=[gr.Textbox(placeholder="Enter video url"),gr.Textbox(placeholder="Enter request")],
outputs=gr.JSON(),
title="Video information request",
description="Retreive user's request information from a video"
)
# Launch the interface and MCP server
if __name__ == "__main__":
demo.launch() |