Hev832 commited on
Commit
96bac53
1 Parent(s): a8e769e

Create other.py

Browse files
Files changed (1) hide show
  1. other.py +46 -0
other.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import subprocess
4
+ from pydub import AudioSegment
5
+ from IPython.display import Audio
6
+
7
+ def get_video_title(url):
8
+ result = subprocess.run(["yt-dlp", "--get-title", url], capture_output=True, text=True)
9
+ if result.returncode == 0:
10
+ return result.stdout.strip()
11
+ else:
12
+ return "Unknown Video"
13
+
14
+ def fetch(url, custom_name, ext):
15
+ title = get_video_title(url)
16
+ max_length = 50
17
+ truncated_title = title[:max_length].strip()
18
+
19
+ filename = f"{custom_name}.{ext}" if custom_name else f"{truncated_title}.{ext}"
20
+ opts = {
21
+ "wav": ["-f", "ba", "-x", "--audio-format", "wav"],
22
+ "mp4": ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"],
23
+ }[ext]
24
+ command = ["yt-dlp"] + opts + [url, "-o", filename]
25
+ subprocess.run(command)
26
+
27
+ return filename
28
+
29
+ def play_audio(output_audio):
30
+ audio = AudioSegment.from_file(output_audio)
31
+ audio.export("output.wav", format="wav")
32
+ gr.Interface(fn=None, live=False, outputs="audio").play("output.wav")
33
+
34
+ app = gr.Interface(
35
+ theme='Hev832/EasyAndCool',
36
+ fn=play_audio,
37
+ inputs=[
38
+ gr.Textbox(label="YouTube video address", placeholder="Paste video link here..."),
39
+ gr.Textbox(label="File name", placeholder="Defaults to video title"),
40
+ gr.Dropdown(value="wav", label="Format")
41
+ ],
42
+ outputs=None,
43
+ description="<div style='font-size:30px; text-align:center;'>YouTube Audio Player</div>"
44
+ )
45
+
46
+ app.launch(debug=True, share=True)