Hev832 commited on
Commit
6383b52
1 Parent(s): 26027a0

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +33 -37
run.py CHANGED
@@ -1,43 +1,39 @@
1
  import gradio as gr
2
- import os
3
- import subprocess
4
- from IPython.display import Audio
5
 
6
- def get_video_title(url):
7
- # 使用 yt-dlp 获取视频标题
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
- # 截断标题为一个合理的长度
17
- max_length = 50 # 调整为适当的值
18
- truncated_title = title[:max_length].strip()
19
-
20
- filename = f"{custom_name}.{ext}" if custom_name else f"{truncated_title}.{ext}"
21
- opts = {
22
- "wav": ["-f", "ba", "-x", "--audio-format", "wav"],
23
- "mp4": ["-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"],
24
- }[ext]
25
- command = ["yt-dlp"] + opts + [url, "-o", filename]
26
- subprocess.run(command)
27
 
28
- return filename
29
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- app = gr.Interface(
32
- theme='Hev832/EasyAndCool',
33
- fn=fetch,
34
- inputs=[
35
- gr.Textbox(label="YouTube video address", placeholder="Paste video link here..."),
36
- gr.Textbox(label="file name", placeholder="Defaults to video title"),
37
- gr.Dropdown(value="wav", label="format")
38
- ],
39
- outputs=gr.Audio(label="Download the file!"),
40
- description="<div style='font-size:30px; text-align:center;'>YouTube wav downloader</div>"
41
- )
42
-
43
- app.launch(debug=True, share=True)
 
1
  import gradio as gr
2
+ import yt_dlp
3
+ import ffmpeg
 
4
 
 
 
 
 
 
 
 
5
 
6
+ def download_audio(url):
7
+ ydl_opts = {
8
+ 'format': 'bestaudio/best',
9
+ 'postprocessors': [{
10
+ 'key': 'FFmpegExtractAudio',
11
+ 'preferredcodec': 'wav',
12
+ 'preferredquality': '192',
13
+ }],
14
+ 'outtmpl': '%(title)s.%(ext)s',
15
+ }
16
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
17
+ ydl.download([url])
18
+ return "audio downloaded as '%(title)s.wav'"
19
 
 
20
 
21
+ with gr.Blocks() as demo:
22
+ used_letters_var = gr.State([])
23
+ with gr.Column():
24
+ gr.Markdown("# YT_DLP GRADIO DEMO")
25
+ gr.Markdown("Please press like button to support me :]")
26
+ with gr.Row() as row:
27
+ with gr.Column():
28
+ url = gr.Textbox(label="URL INPUT")
29
+
30
+ with gr.Column():
31
+ btn = gr.Button("download!")
32
+ outputs = gr.Audio(label="outputs")
33
 
34
+ btn.click(
35
+ download_audio,
36
+ [url],
37
+ [outputs]
38
+ )
39
+ demo.launch(debug=True, share=True)