Blane187 commited on
Commit
0014839
·
verified ·
1 Parent(s): d1c57b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -2,34 +2,43 @@ import gradio as gr
2
  import yt_dlp
3
  import os
4
 
 
 
5
 
6
- os.system("python3 -m pip install --no-deps -U yt-dlp ")
7
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
-
10
- def download_video(url):
11
- ydl_opts = {
12
- 'format': 'best',
13
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
14
- }
15
 
16
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
17
- info_dict = ydl.extract_info(url, download=True)
18
- video_title = info_dict.get('title', None)
19
- file_extension = info_dict.get('ext', None)
20
- file_name = f"{video_title}.wav"
21
- file_path = f"downloads/{file_name}"
22
 
23
- return file_path
24
-
25
- # Create a Gradio interface
26
- iface = gr.Interface(
27
- fn=download_video,
28
- inputs="text",
29
- outputs="audio",
30
- title="Video Downloader",
31
- description="Enter the URL of the video you want to download."
32
- )
33
 
34
- # Launch the interface
35
- iface.launch()
 
2
  import yt_dlp
3
  import os
4
 
5
+ def download_media(url, output_format):
6
+ ydl_opts = {}
7
 
8
+ if output_format == "Audio":
9
+ ydl_opts = {
10
+ 'format': 'bestaudio/best',
11
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
12
+ 'postprocessors': [{
13
+ 'key': 'FFmpegExtractAudio',
14
+ 'preferredcodec': 'mp3',
15
+ 'preferredquality': '192',
16
+ }],
17
+ }
18
+ elif output_format == "Video":
19
+ ydl_opts = {
20
+ 'format': 'bestvideo+bestaudio/best',
21
+ 'outtmpl': 'downloads/%(title)s.%(ext)s',
22
+ }
23
+
24
+ try:
25
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
26
+ ydl.download([url])
27
+ return "Download successful!"
28
+ except Exception as e:
29
+ return f"Error: {str(e)}"
30
 
31
+ # Gradio Blocks UI
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("# Video and Audio Downloader using yt-dlp")
 
 
 
34
 
35
+ with gr.Row():
36
+ url_input = gr.Textbox(label="Video/Audio URL")
37
+ format_dropdown = gr.Dropdown(choices=["Audio", "Video"], label="Output Format")
38
+
39
+ download_button = gr.Button("Download")
40
+ output_text = gr.Textbox(label="Output Message")
41
 
42
+ download_button.click(download_media, inputs=[url_input, format_dropdown], outputs=output_text)
 
 
 
 
 
 
 
 
 
43
 
44
+ demo.launch()