Hev832 commited on
Commit
db6d4d7
1 Parent(s): 2854b1b

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +27 -68
run.py CHANGED
@@ -1,74 +1,33 @@
1
  import gradio as gr
 
2
  import os
3
- import yt_dlp
4
- import markdown
5
 
6
- def downloader(video_url, audio_format):
7
- # Ensure the directory exists
8
- os.makedirs('audios', exist_ok=True)
9
-
10
- # Use a temporary placeholder for the output file
11
- final_output_path = f"audios/%(title)s.%(ext)s"
12
-
13
  ydl_opts = {
14
- 'format': 'bestaudio/best',
15
- 'postprocessors': [{
16
- 'key': 'FFmpegExtractAudio',
17
- 'preferredcodec': audio_format,
18
- }],
19
- 'outtmpl': final_output_path
20
  }
21
 
22
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
23
- result = ydl.download([video_url])
24
-
25
- # Replace %(ext)s with the actual format
26
- final_output_path = final_output_path.replace('%(ext)s', audio_format)
27
-
28
- return final_output_path
29
-
30
-
31
-
32
- logschart = """
33
- ### Changelog:
34
-
35
- #### 2.0 - 2024-05-16
36
-
37
- ##### Added
38
- - **Change WebUI**: Updated the web interface to a new version.
39
- - **Directory Check**: Added a check to ensure the `audios` directory exists before attempting to save files to it.
40
- - `os.makedirs('audios', exist_ok=True)`
41
-
42
-
43
-
44
- ##### Fixed
45
- - **File Not Found Error**: Resolved the issue where the file could not be found due to incorrect output path handling.
46
-
47
- ### Summary of Changes
48
-
49
- 1. **Ensured Directory Exists**: Added code to create the `audios` directory if it doesn't exist to prevent file saving errors.
50
- 2. **Output Path Handling**: Modified the output template to use a placeholder for the file extension, which yt-dlp will replace with the actual extension upon downloading.
51
- 3. **File Renaming Logic**: After downloading the audio, the code now renames the file to match the desired audio name and format.
52
- 4. **Correct File Path Return**: The correct file path is now returned from the `downloader` function, ensuring that the Gradio `gr.Audio` component can properly display and play the downloaded audio file.
53
-
54
- These changes collectively ensure that the audio file is downloaded correctly, renamed appropriately, and made accessible to the Gradio interface for playback.
55
- """
56
-
57
- with gr.Blocks() as demo:
58
- gr.Markdown("# YouTube Downloader 2.0")
59
- with gr.Row():
60
- video_url = gr.Textbox(label="YouTube video link")
61
- with gr.Row():
62
- audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format", value="wav")
63
- with gr.Row():
64
- output = gr.Audio(label="Output")
65
- with gr.Row():
66
- download_button = gr.Button("Download")
67
- download_button.click(downloader, inputs=[video_url, audio_format], outputs=[output])
68
-
69
- with gr.Group():
70
- with gr.Row():
71
- gr.Markdown(logschart)
72
-
73
-
74
- demo.launch()
 
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()