Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,15 @@
|
|
1 |
-
import gradio as gr
|
2 |
import yt_dlp
|
|
|
3 |
import os
|
4 |
from glob import glob
|
5 |
|
|
|
|
|
|
|
|
|
6 |
def download_video(video_url, start_time, end_time):
|
|
|
|
|
7 |
options = {
|
8 |
'format': 'bestaudio/best',
|
9 |
'postprocessors': [{
|
@@ -13,32 +19,37 @@ def download_video(video_url, start_time, end_time):
|
|
13 |
}],
|
14 |
'outtmpl': '%(title)s.%(ext)s',
|
15 |
'postprocessor_args': [
|
16 |
-
'-ss',
|
17 |
-
'-to',
|
18 |
]
|
19 |
}
|
20 |
|
21 |
with yt_dlp.YoutubeDL(options) as ydl:
|
22 |
ydl.download([video_url])
|
23 |
|
|
|
24 |
download_files = glob('*.mp3')
|
25 |
if download_files:
|
26 |
return f"Download successful: {download_files[0]}", download_files[0]
|
27 |
return "No MP3 file found.", None
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
1 |
import yt_dlp
|
2 |
+
import gradio as gr
|
3 |
import os
|
4 |
from glob import glob
|
5 |
|
6 |
+
def convert_time_to_seconds(time_str):
|
7 |
+
hours, minutes, seconds = map(int, time_str.split(':'))
|
8 |
+
return hours * 3600 + minutes * 60 + seconds
|
9 |
+
|
10 |
def download_video(video_url, start_time, end_time):
|
11 |
+
start_seconds = convert_time_to_seconds(start_time)
|
12 |
+
end_seconds = convert_time_to_seconds(end_time)
|
13 |
options = {
|
14 |
'format': 'bestaudio/best',
|
15 |
'postprocessors': [{
|
|
|
19 |
}],
|
20 |
'outtmpl': '%(title)s.%(ext)s',
|
21 |
'postprocessor_args': [
|
22 |
+
'-ss', str(start_seconds),
|
23 |
+
'-to', str(end_seconds)
|
24 |
]
|
25 |
}
|
26 |
|
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 |
+
gr.Textbox(placeholder="Start Time (e.g., 00:00:00)", label="Start Time (HH:MM:SS)"),
|
42 |
+
gr.Textbox(placeholder="End Time (e.g., 00:05:30)", label="End Time (HH:MM:SS)")
|
43 |
+
],
|
44 |
+
outputs=[
|
45 |
+
gr.Text(label="Status Message"),
|
46 |
+
gr.File(label="Download MP3")
|
47 |
+
],
|
48 |
+
title="YouTube Video Downloader",
|
49 |
+
description="Enter YouTube video URL and specify start and end times to download audio as MP3."
|
50 |
+
)
|
51 |
+
return interface
|
52 |
|
53 |
+
if __name__ == "__main__":
|
54 |
+
iface = setup_interface()
|
55 |
+
iface.launch()
|