Lamara091 commited on
Commit
78d7e34
·
verified ·
1 Parent(s): 7cc4670

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -54
app.py CHANGED
@@ -3,62 +3,42 @@ import yt_dlp
3
  import os
4
  from glob import glob
5
 
6
- hours, minutes, seconds = map(int, time_str.split(':'))
7
- return hours * 3600 + minutes * 60 + seconds
8
-
9
- def download_videos(data):
10
- output_files = []
11
- for item in data:
12
- video_url = item['video_url']
13
- start_time = item['start_time']
14
- end_time = item['end_time']
15
-
16
- start_seconds = convert_time_to_seconds(start_time)
17
- end_seconds = convert_time_to_seconds(end_time)
18
-
19
- options = {
20
- 'format': 'bestaudio/best',
21
- 'postprocessors': [{
22
- 'key': 'FFmpegExtractAudio',
23
- 'preferredcodec': 'mp3',
24
- 'preferredquality': '128',
25
- }],
26
- 'outtmpl': '%(title)s.%(ext)s',
27
- 'postprocessor_args': [
28
- '-ss', str(start_seconds),
29
- '-to', str(end_seconds)
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=gr.outputs.File(label="Downloaded MP3 Files"),
55
- title="Batch YouTube Video Downloader",
56
- description="Add multiple entries to download specific clips from YouTube videos as MP3."
 
 
 
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()