RCaz's picture
Update app.py
0db6ea0 verified
raw
history blame
1.88 kB
import gradio as gr
from textblob import TextBlob
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 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()