JUNGU commited on
Commit
7924003
·
verified ·
1 Parent(s): e543beb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -6,14 +6,18 @@ def download_youtube_video(youtube_url):
6
  """Downloads a YouTube video using yt_dlp and returns the downloaded file path."""
7
  try:
8
  ydl_opts = {
9
- 'format': 'best',
10
  'outtmpl': '%(title)s.%(ext)s',
 
 
 
 
11
  }
12
 
13
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
14
  result = ydl.extract_info(youtube_url, download=True)
15
  video_title = result.get('title', None)
16
- video_ext = result.get('ext', None)
17
  downloaded_file = f"{video_title}.{video_ext}"
18
  return downloaded_file
19
 
@@ -24,17 +28,23 @@ def download_youtube_video(youtube_url):
24
  def app(video_link):
25
  video_path = download_youtube_video(video_link)
26
  if video_path:
27
- return video_path
28
  else:
29
- return "An error occurred during the download."
30
 
31
  with gr.Blocks() as interface:
32
  gr.Markdown("## YouTube Video Downloader")
33
  video_link = gr.Textbox(label="Enter YouTube link 🔗 To Download Video⬇️")
34
  download_button = gr.Button("Download")
35
- output = gr.File(label="Downloaded Video Path")
 
36
 
37
- download_button.click(fn=app, inputs=video_link, outputs=output)
 
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
- interface.launch(debug=True)
 
6
  """Downloads a YouTube video using yt_dlp and returns the downloaded file path."""
7
  try:
8
  ydl_opts = {
9
+ 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
10
  'outtmpl': '%(title)s.%(ext)s',
11
+ 'postprocessors': [{
12
+ 'key': 'FFmpegVideoConvertor',
13
+ 'preferedformat': 'mp4',
14
+ }],
15
  }
16
 
17
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
18
  result = ydl.extract_info(youtube_url, download=True)
19
  video_title = result.get('title', None)
20
+ video_ext = 'mp4' # We're forcing mp4 format
21
  downloaded_file = f"{video_title}.{video_ext}"
22
  return downloaded_file
23
 
 
28
  def app(video_link):
29
  video_path = download_youtube_video(video_link)
30
  if video_path:
31
+ return gr.File.update(value=video_path, visible=True), gr.Markdown.update(visible=False)
32
  else:
33
+ return gr.File.update(value=None, visible=False), gr.Markdown.update(visible=True, value="Download failed. Please check the URL and try again.")
34
 
35
  with gr.Blocks() as interface:
36
  gr.Markdown("## YouTube Video Downloader")
37
  video_link = gr.Textbox(label="Enter YouTube link 🔗 To Download Video⬇️")
38
  download_button = gr.Button("Download")
39
+ output = gr.File(label="Downloaded Video", visible=False)
40
+ error_message = gr.Markdown(visible=False)
41
 
42
+ download_button.click(
43
+ fn=app,
44
+ inputs=video_link,
45
+ outputs=[output, error_message],
46
+ api_name="download"
47
+ )
48
 
49
  if __name__ == "__main__":
50
+ interface.launch(debug=True)