JUNGU commited on
Commit
20a7e87
·
verified ·
1 Parent(s): 5a8ee25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -42
app.py CHANGED
@@ -1,55 +1,49 @@
1
  import gradio as gr
2
- import yt_dlp
3
  import os
4
- import io
5
 
6
- def download_youtube_video(youtube_url):
7
- """Downloads a YouTube video using yt_dlp and returns the video data and filename."""
8
- try:
9
- ydl_opts = {
10
- 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
11
- 'outtmpl': 'video.%(ext)s',
12
- }
13
-
14
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
15
- info = ydl.extract_info(youtube_url, download=False)
16
- video_title = info.get('title', 'video')
17
- video_ext = info.get('ext', 'mp4')
18
-
19
- # Use a simple filename
20
- filename = f"youtube_video.{video_ext}"
21
-
22
- # Download to memory
23
- ydl_opts['outtmpl'] = '-'
24
- ydl_opts['logtostderr'] = True
25
-
26
- with io.BytesIO() as video_buffer:
27
- ydl.download([youtube_url])
28
- video_data = video_buffer.getvalue()
29
-
30
- return video_data, filename, video_title
31
-
32
- except Exception as e:
33
- print("An error occurred:", e)
34
- return None, None, None
35
-
36
- def app(video_link):
37
- video_data, filename, video_title = download_youtube_video(video_link)
38
- if video_data and filename:
39
- return (filename, video_data), f"Download successful! Video: {video_title}"
40
  else:
41
- return None, "Download failed. Please check the URL and try again."
42
 
43
  with gr.Blocks() as interface:
44
- gr.Markdown("## YouTube Video Downloader")
45
- video_link = gr.Textbox(label="Enter YouTube link 🔗 To Download Video⬇️")
46
- download_button = gr.Button("Download")
47
- output = gr.File(label="Downloaded Video")
48
  message = gr.Markdown()
49
 
50
  download_button.click(
51
  fn=app,
52
- inputs=video_link,
53
  outputs=[output, message],
54
  api_name="download"
55
  )
 
1
  import gradio as gr
2
+ from pytube import YouTube
3
  import os
 
4
 
5
+ def download_youtube_videos(urls):
6
+ """Downloads multiple YouTube videos and returns a list of downloaded file paths."""
7
+ downloaded_files = []
8
+ for url in urls:
9
+ try:
10
+ yt = YouTube(url)
11
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
12
+ filename = f"{yt.title}.mp4"
13
+ video.download(filename=filename)
14
+ downloaded_files.append(filename)
15
+ except Exception as e:
16
+ print(f"Error downloading {url}: {str(e)}")
17
+
18
+ return downloaded_files
19
+
20
+ def app(urls):
21
+ # Split the input string into a list of URLs
22
+ url_list = [url.strip() for url in urls.split('\n') if url.strip()]
23
+
24
+ if not url_list:
25
+ return None, "No valid URLs provided. Please enter at least one YouTube URL."
26
+
27
+ downloaded_files = download_youtube_videos(url_list)
28
+
29
+ if downloaded_files:
30
+ # Create a list of (filename, file path) tuples for Gradio File component
31
+ file_tuples = [(f, f) for f in downloaded_files if os.path.exists(f)]
32
+ message = f"Successfully downloaded {len(file_tuples)} videos."
33
+ return file_tuples, message
 
 
 
 
 
34
  else:
35
+ return None, "Download failed. Please check the URLs and try again."
36
 
37
  with gr.Blocks() as interface:
38
+ gr.Markdown("## YouTube Batch Video Downloader")
39
+ urls_input = gr.Textbox(label="Enter YouTube links (one per line) 🔗", lines=5)
40
+ download_button = gr.Button("Download Videos")
41
+ output = gr.File(label="Downloaded Videos", file_count="multiple")
42
  message = gr.Markdown()
43
 
44
  download_button.click(
45
  fn=app,
46
+ inputs=urls_input,
47
  outputs=[output, message],
48
  api_name="download"
49
  )