Hev832 commited on
Commit
5806219
1 Parent(s): 74a6084

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +25 -37
run.py CHANGED
@@ -1,44 +1,32 @@
1
  import gradio as gr
 
2
  import os
3
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def get_video_title(url):
6
- result = subprocess.run(["yt-dlp", "--get-title", url], capture_output=True, text=True)
7
- if result.returncode == 0:
8
- return result.stdout.strip()
9
- else:
10
- return "Unknown Video"
11
 
12
- def fetch(url, custom_name, ext):
13
- title = get_video_title(url)
14
- max_length = 50
15
- truncated_title = title[:max_length].strip()
16
 
17
- filename = f"{custom_name}.{ext}" if custom_name else f"{truncated_title}.{ext}"
18
- opts = {
19
- "wav": ["-f", "ba", "-x", "--audio-format", "wav"],
20
- "mp4": ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"],
21
- }[ext]
22
- command = ["yt-dlp"] + opts + [url, "-o", filename]
23
- subprocess.run(command)
24
-
25
- return filename
26
-
27
- def play_audio(output_audio):
28
- audio = AudioSegment.from_file(output_audio)
29
- audio.export("output.wav", format="wav")
30
- gr.Interface(fn=None, live=False, outputs="audio").play("output.wav")
31
-
32
- app = gr.Interface(
33
- theme='Hev832/EasyAndCool',
34
- fn=play_audio,
35
- inputs=[
36
- gr.Textbox(label="YouTube video address", placeholder="Paste video link here..."),
37
- gr.Textbox(label="File name", placeholder="Defaults to video title"),
38
- gr.Dropdown(value="wav", label="Format")
39
- ],
40
- outputs=None,
41
- description="<div style='font-size:30px; text-align:center;'>YouTube Audio downloader</div>"
42
- )
43
 
44
- app.launch(debug=True, share=True)
 
1
  import gradio as gr
2
+ import yt_dlp
3
  import os
4
 
5
+ def downloader(video_url, audio_format, audio_name):
6
+ save_path = os.path.join(audio_name)
7
+ ydl_opts = {
8
+ 'format': 'bestaudio/best',
9
+ 'postprocessors': [{
10
+ 'key': 'FFmpegExtractAudio',
11
+ 'preferredcodec': audio_format,
12
+ }],
13
+ 'outtmpl': save_path,
14
+ }
15
 
16
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
17
+ ydl.download([video_url])
18
+ return save_path
 
 
 
19
 
20
+ with gr.Blocks() as demo:
21
+ gr.Markdown("# YouTube Audio Downloader")
 
 
22
 
23
+ video_url = gr.Textbox(label="YouTube video link")
24
+ audio_name = gr.Textbox(label="Audio name of YouTube audio")
25
+ audio_format = gr.Radio(["wav", "flac", "mp3"], label="Select the output format")
26
+
27
+ output = gr.Textbox(label="Output")
28
+
29
+ download_button = gr.Button("Download")
30
+ download_button.click(downloader, inputs=[video_url, audio_format, audio_name], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ demo.launch()