Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,12 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from glob import glob
|
| 5 |
|
| 6 |
-
def convert_time_to_seconds(
|
| 7 |
-
hours
|
| 8 |
-
return hours * 3600 + minutes * 60 + seconds
|
| 9 |
|
| 10 |
-
def download_video(video_url,
|
| 11 |
-
start_seconds = convert_time_to_seconds(
|
| 12 |
-
end_seconds = convert_time_to_seconds(
|
| 13 |
options = {
|
| 14 |
'format': 'bestaudio/best',
|
| 15 |
'postprocessors': [{
|
|
@@ -27,27 +26,34 @@ def download_video(video_url, start_time, end_time):
|
|
| 27 |
with yt_dlp.YoutubeDL(options) as ydl:
|
| 28 |
ydl.download([video_url])
|
| 29 |
|
| 30 |
-
# 查找和提供下载的文件
|
| 31 |
download_files = glob('*.mp3')
|
| 32 |
if download_files:
|
| 33 |
return f"Download successful: {download_files[0]}", download_files[0]
|
| 34 |
return "No MP3 file found.", None
|
| 35 |
|
| 36 |
def setup_interface():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
interface = gr.Interface(
|
| 38 |
fn=download_video,
|
| 39 |
inputs=[
|
| 40 |
-
gr.Textbox(lines=2, placeholder="Enter YouTube video URL here...", label="YouTube Video URL"),
|
| 41 |
-
start_time_hours
|
| 42 |
-
start_time_minutes
|
| 43 |
-
start_time_seconds
|
| 44 |
-
end_time_hours
|
| 45 |
-
end_time_minutes
|
| 46 |
-
end_time_seconds
|
| 47 |
],
|
| 48 |
outputs=[
|
| 49 |
-
gr.Text(label="Status Message"),
|
| 50 |
-
gr.File(label="
|
| 51 |
],
|
| 52 |
title="YouTube Video Downloader",
|
| 53 |
description="Enter YouTube video URL and specify start and end times to download audio as MP3."
|
|
|
|
| 3 |
import os
|
| 4 |
from glob import glob
|
| 5 |
|
| 6 |
+
def convert_time_to_seconds(hours, minutes, seconds):
|
| 7 |
+
return int(hours) * 3600 + int(minutes) * 60 + int(seconds)
|
|
|
|
| 8 |
|
| 9 |
+
def download_video(video_url, start_hour, start_minute, start_second, end_hour, end_minute, end_second):
|
| 10 |
+
start_seconds = convert_time_to_seconds(start_hour, start_minute, start_second)
|
| 11 |
+
end_seconds = convert_time_to_seconds(end_hour, end_minute, end_second)
|
| 12 |
options = {
|
| 13 |
'format': 'bestaudio/best',
|
| 14 |
'postprocessors': [{
|
|
|
|
| 26 |
with yt_dlp.YoutubeDL(options) as ydl:
|
| 27 |
ydl.download([video_url])
|
| 28 |
|
|
|
|
| 29 |
download_files = glob('*.mp3')
|
| 30 |
if download_files:
|
| 31 |
return f"Download successful: {download_files[0]}", download_files[0]
|
| 32 |
return "No MP3 file found.", None
|
| 33 |
|
| 34 |
def setup_interface():
|
| 35 |
+
# 定义输入组件
|
| 36 |
+
start_time_hours = gr.inputs.Dropdown(list(range(24)), label="Start Hour")
|
| 37 |
+
start_time_minutes = gr.inputs.Dropdown(list(range(60)), label="Start Minute")
|
| 38 |
+
start_time_seconds = gr.inputs.Number(default=0, label="Start Seconds", maximum=59)
|
| 39 |
+
end_time_hours = gr.inputs.Dropdown(list(range(24)), label="End Hour")
|
| 40 |
+
end_time_minutes = gr.inputs.Dropdown(list(range(60)), label="End Minute")
|
| 41 |
+
end_time_seconds = gr.inputs.Number(default=0, label="End Seconds", maximum=59)
|
| 42 |
+
|
| 43 |
interface = gr.Interface(
|
| 44 |
fn=download_video,
|
| 45 |
inputs=[
|
| 46 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter YouTube video URL here...", label="YouTube Video URL"),
|
| 47 |
+
start_time_hours,
|
| 48 |
+
start_time_minutes,
|
| 49 |
+
start_time_seconds,
|
| 50 |
+
end_time_hours,
|
| 51 |
+
end_time_minutes,
|
| 52 |
+
end_time_seconds
|
| 53 |
],
|
| 54 |
outputs=[
|
| 55 |
+
gr.outputs.Text(label="Status Message"),
|
| 56 |
+
gr.outputs.File(label="Downloaded MP3")
|
| 57 |
],
|
| 58 |
title="YouTube Video Downloader",
|
| 59 |
description="Enter YouTube video URL and specify start and end times to download audio as MP3."
|