Hev832 commited on
Commit
62bd18f
1 Parent(s): 91243d9

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +30 -24
run.py CHANGED
@@ -4,35 +4,40 @@ import os
4
 
5
  def download_youtube(url, file_format):
6
  ydl_opts = {}
7
- if file_format == "audio":
8
- ydl_opts = {
9
- 'format': 'bestaudio/best',
10
- 'postprocessors': [{
11
- 'key': 'FFmpegExtractAudio',
12
- 'preferredcodec': 'mp3',
13
- 'preferredquality': '192',
14
- }],
15
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
16
- }
17
- else:
18
- ydl_opts = {
19
- 'format': 'bestvideo+bestaudio/best',
20
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
21
- }
22
-
23
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
24
- info_dict = ydl.extract_info(url, download=True)
25
- file_path = ydl.prepare_filename(info_dict)
26
  if file_format == "audio":
27
- file_path = os.path.splitext(file_path)[0] + ".mp3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- return file_path
 
 
30
 
31
  def show_output(file_path):
 
 
32
  if file_path.endswith(".mp3"):
33
- return gr.Audio(file_path, label="Audio Output"), None
34
  else:
35
- return None, gr.Video(file_path, label="Video Output")
36
 
37
  with gr.Blocks() as demo:
38
  with gr.Row():
@@ -41,11 +46,12 @@ with gr.Blocks() as demo:
41
  format_input = gr.Dropdown(choices=["audio", "video"], label="Format")
42
  download_button = gr.Button("Download")
43
  with gr.Column():
 
44
  audio_output = gr.Audio(label="Audio Output", visible=False)
45
  video_output = gr.Video(label="Video Output", visible=False)
46
 
47
  download_button.click(download_youtube, inputs=[url_input, format_input], outputs=None).then(
48
- show_output, inputs=None, outputs=[audio_output, video_output]
49
  )
50
 
51
  demo.launch()
 
4
 
5
  def download_youtube(url, file_format):
6
  ydl_opts = {}
7
+ try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  if file_format == "audio":
9
+ ydl_opts = {
10
+ 'format': 'bestaudio/best',
11
+ 'postprocessors': [{
12
+ 'key': 'FFmpegExtractAudio',
13
+ 'preferredcodec': 'mp3',
14
+ 'preferredquality': '192',
15
+ }],
16
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
17
+ }
18
+ else:
19
+ ydl_opts = {
20
+ 'format': 'bestvideo+bestaudio/best',
21
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
22
+ }
23
+
24
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
+ info_dict = ydl.extract_info(url, download=True)
26
+ file_path = ydl.prepare_filename(info_dict)
27
+ if file_format == "audio":
28
+ file_path = os.path.splitext(file_path)[0] + ".mp3"
29
 
30
+ return file_path
31
+ except Exception as e:
32
+ return str(e)
33
 
34
  def show_output(file_path):
35
+ if not file_path or not os.path.exists(file_path):
36
+ return "Error downloading file.", None, None
37
  if file_path.endswith(".mp3"):
38
+ return None, gr.Audio(file_path, label="Audio Output"), None
39
  else:
40
+ return None, None, gr.Video(file_path, label="Video Output")
41
 
42
  with gr.Blocks() as demo:
43
  with gr.Row():
 
46
  format_input = gr.Dropdown(choices=["audio", "video"], label="Format")
47
  download_button = gr.Button("Download")
48
  with gr.Column():
49
+ error_output = gr.Textbox(label="Error Output", visible=False)
50
  audio_output = gr.Audio(label="Audio Output", visible=False)
51
  video_output = gr.Video(label="Video Output", visible=False)
52
 
53
  download_button.click(download_youtube, inputs=[url_input, format_input], outputs=None).then(
54
+ show_output, inputs=None, outputs=[error_output, audio_output, video_output]
55
  )
56
 
57
  demo.launch()