File size: 2,314 Bytes
92b4d48
a0ea356
cec3e7a
 
88457d4
0d6dbf8
 
88457d4
0d6dbf8
 
 
92b4d48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d6dbf8
a0ea356
 
 
 
 
 
0d6dbf8
92b4d48
 
 
a0ea356
0d6dbf8
 
 
 
 
 
92b4d48
 
a0ea356
 
92b4d48
 
 
 
 
 
 
 
 
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
import gradio as gr
import yt_dlp
import os
from glob import glob

def convert_time_to_seconds(hours, minutes, seconds):
    return int(hours) * 3600 + int(minutes) * 60 + int(seconds)

def download_video(video_url, start_hour, start_minute, start_second, end_hour, end_minute, end_second):
    start_seconds = convert_time_to_seconds(start_hour, start_minute, start_second)
    end_seconds = convert_time_to_seconds(end_hour, end_minute, end_second)
    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')
    if download_files:
        return f"Download successful: {download_files[0]}", download_files[0]
    return "No MP3 file found.", None

def setup_interface():
    # 定义输入组件
    start_time_hours = gr.Dropdown(list(range(24)), label="Start Hour")
    start_time_minutes = gr.Dropdown(list(range(60)), label="Start Minute")
    start_time_seconds = gr.Number(value=0, label="Start Seconds", maximum=59)
    end_time_hours = gr.Dropdown(list(range(24)), label="End Hour")
    end_time_minutes = gr.Dropdown(list(range(60)), label="End Minute")
    end_time_seconds = gr.Number(value=0, label="End Seconds", maximum=59)
    
    interface = gr.Interface(
        fn=download_video,
        inputs=[
            gr.Textbox(lines=2, placeholder="Enter YouTube video URL here...", label="YouTube Video URL"),
            start_time_hours,
            start_time_minutes,
            start_time_seconds,
            end_time_hours,
            end_time_minutes,
            end_time_seconds
        ],
        outputs=[
            gr.Text(label="Status Message"),
            gr.File(label="Downloaded MP3")
        ],
        title="YouTube Video Downloader",
        description="Enter YouTube video URL and specify start and end times to download audio as MP3."
    )
    return interface

if __name__ == "__main__":
    iface = setup_interface()
    iface.launch()