Update app.py
Browse files
app.py
CHANGED
@@ -3,62 +3,42 @@ import yt_dlp
|
|
3 |
import os
|
4 |
from glob import glob
|
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 |
-
with yt_dlp.YoutubeDL(options) as ydl:
|
34 |
-
ydl.download([video_url])
|
35 |
-
|
36 |
-
download_files = glob('*.mp3')
|
37 |
-
for file in download_files:
|
38 |
-
os.rename(file, os.path.join('downloaded', file))
|
39 |
-
output_files.append(os.path.join('downloaded', file))
|
40 |
-
|
41 |
-
return output_files
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
interface = gr.Interface(
|
48 |
-
fn=download_videos,
|
49 |
-
inputs=gr.inputs.DynamicList([
|
50 |
gr.inputs.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here..."),
|
51 |
gr.inputs.Textbox(label="Start Time (HH:MM:SS)", placeholder="00:00:00"),
|
52 |
gr.inputs.Textbox(label="End Time (HH:MM:SS)", placeholder="00:05:00")
|
53 |
-
]
|
54 |
-
outputs=
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
interface.launch()
|
|
|
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': [{
|
10 |
+
'key': 'FFmpegExtractAudio',
|
11 |
+
'preferredcodec': 'mp3',
|
12 |
+
'preferredquality': '128',
|
13 |
+
}],
|
14 |
+
'outtmpl': '%(title)s.%(ext)s',
|
15 |
+
'postprocessor_args': [
|
16 |
+
'-ss', start_time, # 使用时间字符串直接传递
|
17 |
+
'-to', end_time
|
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 |
+
iface = gr.Interface(
|
30 |
+
fn=download_video,
|
31 |
+
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
gr.inputs.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here..."),
|
33 |
gr.inputs.Textbox(label="Start Time (HH:MM:SS)", placeholder="00:00:00"),
|
34 |
gr.inputs.Textbox(label="End Time (HH:MM:SS)", placeholder="00:05:00")
|
35 |
+
],
|
36 |
+
outputs=[
|
37 |
+
gr.outputs.Textbox(label="Status Message"),
|
38 |
+
gr.outputs.File(label="Downloaded MP3")
|
39 |
+
],
|
40 |
+
title="YouTube Video Downloader",
|
41 |
+
description="Enter YouTube video URL and specify start and end times to download audio as MP3."
|
42 |
)
|
43 |
|
44 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|