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