import gradio as gr import yt_dlp import os from glob import glob hours, minutes, seconds = map(int, time_str.split(':')) return hours * 3600 + minutes * 60 + seconds def download_videos(data): output_files = [] for item in data: video_url = item['video_url'] start_time = item['start_time'] end_time = item['end_time'] start_seconds = convert_time_to_seconds(start_time) end_seconds = convert_time_to_seconds(end_time) options = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '128', }], 'outtmpl': '%(title)s.%(ext)s', 'postprocessor_args': [ '-ss', str(start_seconds), '-to', str(end_seconds) ] } with yt_dlp.YoutubeDL(options) as ydl: ydl.download([video_url]) download_files = glob('*.mp3') for file in download_files: os.rename(file, os.path.join('downloaded', file)) output_files.append(os.path.join('downloaded', file)) return output_files interface = gr.Interface( fn=download_videos, inputs=gr.inputs.DynamicList([ gr.inputs.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here..."), gr.inputs.Textbox(label="Start Time (HH:MM:SS)", placeholder="00:00:00"), gr.inputs.Textbox(label="End Time (HH:MM:SS)", placeholder="00:05:00") ]), outputs=gr.outputs.File(label="Downloaded MP3 Files"), title="Batch YouTube Video Downloader", description="Add multiple entries to download specific clips from YouTube videos as MP3." ) interface.launch()