File size: 1,850 Bytes
cec3e7a
7f5c593
cec3e7a
 
88457d4
 
 
 
 
7f5c593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cec3e7a
7f5c593
 
 
 
cec3e7a
7f5c593
cec3e7a
7f5c593
 
 
 
 
 
 
 
 
 
 
cec3e7a
7f5c593
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
import gradio as gr
import yt_dlp
import os
from glob import glob

def convert_time_to_seconds(time_str):
    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()