Lamara091 commited on
Commit
7f5c593
verified
1 Parent(s): de9edc2

Update app.py

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