File size: 1,372 Bytes
92b4d48
a0ea356
cec3e7a
 
88457d4
78d7e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d735853
 
 
78d7e34
 
 
 
 
 
 
d735853
 
78d7e34
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
import gradio as gr
import yt_dlp
import os
from glob import glob

def download_video(video_url, start_time, end_time):
    options = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '128',
        }],
        'outtmpl': '%(title)s.%(ext)s',
        'postprocessor_args': [
            '-ss', start_time,  # 使用时间字符串直接传递
            '-to', end_time
        ]
    }

    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

iface = gr.Interface(
    fn=download_video,
    inputs=[
        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.Textbox(label="Status Message"),
        gr.outputs.File(label="Downloaded MP3")
    ],
    title="YouTube Video Downloader",
    description="Enter YouTube video URL and specify start and end times to download audio as MP3."
)

iface.launch()