Hev832 commited on
Commit
051b0bd
1 Parent(s): 08560b9

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +55 -30
run.py CHANGED
@@ -1,33 +1,58 @@
1
  import gradio as gr
2
- import subprocess
3
  import os
4
 
5
- def download_media(url):
6
- # Determine if the URL is for audio or video
7
- ydl_opts = {
8
- 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', # Prefer mp4 format
9
- 'outtmpl': 'downloaded_media.%(ext)s', # Output file name
10
- }
11
-
12
- # Download using yt-dlp
13
- subprocess.run(['yt-dlp', url, '--merge-output-format', 'mp4', '--recode-video', 'mp4'])
14
-
15
- # Check if downloaded file exists
16
- if os.path.exists('downloaded_media.mp4'):
17
- return gr.outputs.Video("downloaded_media.mp4")
18
- elif os.path.exists('downloaded_media.m4a'):
19
- return gr.outputs.Audio("downloaded_media.m4a")
20
- else:
21
- return "Media could not be downloaded or unsupported format."
22
-
23
- # Create Gradio interface
24
- iface = gr.Interface(
25
- fn=download_media,
26
- inputs="text",
27
- outputs="auto",
28
- title="YouTube Downloader",
29
- description="Enter a YouTube video URL to download its audio or video."
30
- )
31
-
32
- # Launch the interface
33
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from yt_dlp import YoutubeDL
3
  import os
4
 
5
+ def download_youtube(url, output_format):
6
+ ydl_opts = {}
7
+ file_path = ""
8
+
9
+ if output_format == "audio":
10
+ ydl_opts = {
11
+ 'format': 'bestaudio/best',
12
+ 'postprocessors': [{
13
+ 'key': 'FFmpegExtractAudio',
14
+ 'preferredcodec': 'mp3',
15
+ 'preferredquality': '192',
16
+ }],
17
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
18
+ }
19
+ elif output_format == "video":
20
+ ydl_opts = {
21
+ 'format': 'bestvideo+bestaudio/best',
22
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
23
+ }
24
+
25
+ with YoutubeDL(ydl_opts) as ydl:
26
+ result = ydl.extract_info(url, download=True)
27
+ file_path = ydl.prepare_filename(result)
28
+
29
+ if output_format == "audio":
30
+ file_path = file_path.replace(result['ext'], 'mp3')
31
+
32
+ return file_path
33
+
34
+ def show_media(file_path, output_format):
35
+ if output_format == "audio":
36
+ return gr.Audio.update(value=file_path)
37
+ elif output_format == "video":
38
+ return gr.Video.update(value=file_path)
39
+
40
+ def yt_download(url, output_format):
41
+ file_path = download_youtube(url, output_format)
42
+ return show_media(file_path, output_format)
43
+
44
+ with gr.Blocks() as demo:
45
+ gr.Markdown("## YouTube Downloader")
46
+
47
+ with gr.Row():
48
+ url_input = gr.Textbox(label="YouTube URL")
49
+ format_input = gr.Radio(choices=["audio", "video"], label="Format")
50
+
51
+ download_btn = gr.Button("Download")
52
+
53
+ audio_output = gr.Audio(label="Audio Output")
54
+ video_output = gr.Video(label="Video Output")
55
+
56
+ download_btn.click(yt_download, inputs=[url_input, format_input], outputs=[audio_output, video_output])
57
+
58
+ demo.launch()