YT-DLP / app.py
Lamara091's picture
Update app.py
78d7e34 verified
raw
history blame
1.37 kB
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()