Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -45,49 +45,59 @@ def _return_yt_html_embed(yt_url):
|
|
45 |
)
|
46 |
return HTML_str
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def yt_transcribe(yt_url, task):
|
49 |
-
# yt = pt.YouTube(yt_url) # 用 yt_dlp 代替 pytube
|
50 |
-
ydl = youtube_dl.YoutubeDL({"format": "bestaudio"}) # 创建 yt_dlp 对象
|
51 |
-
info = ydl.extract_info(yt_url, download=False) # 提取视频信息
|
52 |
-
audio_url = info["formats"][0]["url"] # 获取音频链接
|
53 |
html_embed_str = _return_yt_html_embed(yt_url)
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
return html_embed_str, text
|
65 |
|
66 |
-
# Assuming srt format is a sequence of subtitles with index, time range and text
|
67 |
-
def convert_to_srt(input):
|
68 |
-
output = ""
|
69 |
-
index = 1
|
70 |
-
for chunk in input["chunks"]:
|
71 |
-
start, end = chunk["timestamp"]
|
72 |
-
text = chunk["text"]
|
73 |
-
if end is None:
|
74 |
-
end = "None"
|
75 |
-
# Convert seconds to hours:minutes:seconds,milliseconds format
|
76 |
-
start = format_time(start)
|
77 |
-
end = format_time(end)
|
78 |
-
output += f"{index}\n{start} --> {end}\n{text}\n\n"
|
79 |
-
index += 1
|
80 |
-
return output
|
81 |
-
|
82 |
-
# Helper function to format time
|
83 |
-
def format_time(seconds):
|
84 |
-
if seconds == "None":
|
85 |
-
return seconds
|
86 |
-
hours = int(seconds // 3600)
|
87 |
-
minutes = int((seconds % 3600) // 60)
|
88 |
-
seconds = int(seconds % 60)
|
89 |
-
milliseconds = int((seconds % 1) * 1000)
|
90 |
-
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
91 |
|
92 |
demo = gr.Blocks()
|
93 |
mf_transcribe = gr.Interface(
|
|
|
45 |
)
|
46 |
return HTML_str
|
47 |
|
48 |
+
def download_yt_audio(yt_url, filename):
|
49 |
+
info_loader = youtube_dl.YoutubeDL()
|
50 |
+
|
51 |
+
try:
|
52 |
+
info = info_loader.extract_info(yt_url, download=False)
|
53 |
+
except youtube_dl.utils.DownloadError as err:
|
54 |
+
raise gr.Error(str(err))
|
55 |
+
|
56 |
+
file_length = info["duration_string"]
|
57 |
+
file_h_m_s = file_length.split(":")
|
58 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
59 |
+
|
60 |
+
if len(file_h_m_s) == 1:
|
61 |
+
file_h_m_s.insert(0, 0)
|
62 |
+
if len(file_h_m_s) == 2:
|
63 |
+
file_h_m_s.insert(0, 0)
|
64 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
65 |
+
|
66 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
67 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
68 |
+
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
69 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
70 |
+
|
71 |
+
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
72 |
+
|
73 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
74 |
+
try:
|
75 |
+
ydl.download([yt_url])
|
76 |
+
except youtube_dl.utils.ExtractorError as err:
|
77 |
+
raise gr.Error(str(err))
|
78 |
+
|
79 |
+
|
80 |
def yt_transcribe(yt_url, task):
|
|
|
|
|
|
|
|
|
81 |
html_embed_str = _return_yt_html_embed(yt_url)
|
82 |
+
|
83 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
84 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
85 |
+
download_yt_audio(yt_url, filepath)
|
86 |
+
with open(filepath, "rb") as f:
|
87 |
+
inputs = f.read()
|
88 |
+
|
89 |
+
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
90 |
+
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
91 |
+
|
92 |
+
|
93 |
+
text = pipe(inputs,return_timestamps=True)
|
94 |
+
# text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"language":"zh"}, return_timestamps=True)
|
95 |
+
# text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
96 |
+
# text = pipe("audio.mp3",return_timestamps=True)
|
97 |
+
#trans to SRT
|
98 |
+
text= convert_to_srt(text)
|
99 |
return html_embed_str, text
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
demo = gr.Blocks()
|
103 |
mf_transcribe = gr.Interface(
|