Lamara091 commited on
Commit
d735853
·
verified ·
1 Parent(s): 133c149

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -60
app.py CHANGED
@@ -1,65 +1,66 @@
 
 
1
  import gradio as gr
2
  import yt_dlp
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': [{
15
- 'key': 'FFmpegExtractAudio',
16
- 'preferredcodec': 'mp3',
17
- 'preferredquality': '128',
18
- }],
19
- 'outtmpl': '%(title)s.%(ext)s',
20
- 'postprocessor_args': [
21
- '-ss', str(start_seconds),
22
- '-to', str(end_seconds)
23
- ]
24
- }
25
-
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.Dropdown(list(range(24)), label="Start Hour")
37
- start_time_minutes = gr.Dropdown(list(range(60)), label="Start Minute")
38
- start_time_seconds = gr.Number(value=0, label="Start Seconds", maximum=59)
39
- end_time_hours = gr.Dropdown(list(range(24)), label="End Hour")
40
- end_time_minutes = gr.Dropdown(list(range(60)), label="End Minute")
41
- end_time_seconds = gr.Number(value=0, label="End Seconds", maximum=59)
42
-
43
- interface = gr.Interface(
44
- fn=download_video,
45
- inputs=[
46
- gr.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.Text(label="Status Message"),
56
- gr.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."
60
- )
61
- return interface
62
-
63
- if __name__ == "__main__":
64
- iface = setup_interface()
65
- iface.launch()
 
1
+
2
+
3
  import gradio as gr
4
  import yt_dlp
5
  import os
6
  from glob import glob
7
 
8
+ hours, minutes, seconds = map(int, time_str.split(':'))
9
+ return hours * 3600 + minutes * 60 + seconds
10
+
11
+ def download_videos(data):
12
+ output_files = []
13
+ for item in data:
14
+ video_url = item['video_url']
15
+ start_time = item['start_time']
16
+ end_time = item['end_time']
17
+
18
+ start_seconds = convert_time_to_seconds(start_time)
19
+ end_seconds = convert_time_to_seconds(end_time)
20
+
21
+ options = {
22
+ 'format': 'bestaudio/best',
23
+ 'postprocessors': [{
24
+ 'key': 'FFmpegExtractAudio',
25
+ 'preferredcodec': 'mp3',
26
+ 'preferredquality': '128',
27
+ }],
28
+ 'outtmpl': '%(title)s.%(ext)s',
29
+ 'postprocessor_args': [
30
+ '-ss', str(start_seconds),
31
+ '-to', str(end_seconds)
32
+ ]
33
+ }
34
+
35
+ with yt_dlp.YoutubeDL(options) as ydl:
36
+ ydl.download([video_url])
37
+
38
+ download_files = glob('*.mp3')
39
+ for file in download_files:
40
+ os.rename(file, os.path.join('downloaded', file))
41
+ output_files.append(os.path.join('downloaded', file))
42
+
43
+ return output_files
44
+
45
+
46
+
47
+
48
+
49
+ interface = gr.Interface(
50
+ fn=download_videos,
51
+ inputs=gr.inputs.DynamicList([
52
+ gr.inputs.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here..."),
53
+ gr.inputs.Textbox(label="Start Time (HH:MM:SS)", placeholder="00:00:00"),
54
+ gr.inputs.Textbox(label="End Time (HH:MM:SS)", placeholder="00:05:00")
55
+ ]),
56
+ outputs=gr.outputs.File(label="Downloaded MP3 Files"),
57
+ title="Batch YouTube Video Downloader",
58
+ description="Add multiple entries to download specific clips from YouTube videos as MP3."
59
+ )
60
+
61
+
62
+
63
+
64
+
65
+
66
+ interface.launch()