Spaces:
Build error
Build error
Create run.py
Browse files
run.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
|
| 5 |
+
def get_video_title(url):
|
| 6 |
+
# 使用 yt-dlp 获取视频标题
|
| 7 |
+
result = subprocess.run(["yt-dlp", "--get-title", url], capture_output=True, text=True)
|
| 8 |
+
if result.returncode == 0:
|
| 9 |
+
return result.stdout.strip()
|
| 10 |
+
else:
|
| 11 |
+
return "Unknown Video"
|
| 12 |
+
|
| 13 |
+
def fetch(url, custom_name, ext):
|
| 14 |
+
title = get_video_title(url)
|
| 15 |
+
# 截断标题为一个合理的长度
|
| 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 |
+
interface = gr.Interface(
|
| 30 |
+
fn=fetch,
|
| 31 |
+
inputs=[
|
| 32 |
+
gr.Textbox(label="YouTube video address", placeholder="Paste video link here..."),
|
| 33 |
+
gr.Textbox(label="file name", placeholder="默认为视频标题"),
|
| 34 |
+
gr.Dropdown(value="wav", label="format")
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.File(label="Download the file! (MP3 files available)"),
|
| 37 |
+
description="<div style='font-size:30px; text-align:center;'>YouTube wav downloader</div>"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
app.launch()
|