Spaces:
Runtime error
Runtime error
speech-test
commited on
Commit
•
4c5f593
1
Parent(s):
cfbc0a5
reqs
Browse files- README.md +3 -3
- package.txt +1 -0
- requirements.txt +2 -0
- streaming.py +69 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
emoji: 💩
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
app_file: app.py
|
8 |
pinned: false
|
|
|
1 |
---
|
2 |
+
title: YouTube Streaming ASR
|
3 |
emoji: 💩
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: red
|
6 |
sdk: streamlit
|
7 |
app_file: app.py
|
8 |
pinned: false
|
package.txt
CHANGED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
CHANGED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit-player
|
2 |
+
yt-dlp
|
streaming.py
CHANGED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
def ffmpeg_stream(youtube_url, sampling_rate=16_000, chunk_duration_ms=5000, pad_duration_ms=200):
|
7 |
+
"""
|
8 |
+
Helper function to read an audio file through ffmpeg.
|
9 |
+
"""
|
10 |
+
chunk_len = int(sampling_rate * chunk_duration_ms / 1000)
|
11 |
+
pad_len = int(sampling_rate * pad_duration_ms / 1000)
|
12 |
+
read_chunk_len = chunk_len + pad_len * 2
|
13 |
+
|
14 |
+
ar = f"{sampling_rate}"
|
15 |
+
ac = "1"
|
16 |
+
format_for_conversion = "f32le"
|
17 |
+
dtype = np.float32
|
18 |
+
size_of_sample = 4
|
19 |
+
|
20 |
+
ffmpeg_command = [
|
21 |
+
"ffmpeg",
|
22 |
+
"-i",
|
23 |
+
"pipe:",
|
24 |
+
"-ac",
|
25 |
+
ac,
|
26 |
+
"-ar",
|
27 |
+
ar,
|
28 |
+
"-f",
|
29 |
+
format_for_conversion,
|
30 |
+
"-hide_banner",
|
31 |
+
"-loglevel",
|
32 |
+
"quiet",
|
33 |
+
"pipe:1",
|
34 |
+
]
|
35 |
+
|
36 |
+
ytdl_command = ["yt-dlp", "-f", "bestaudio", youtube_url, "--quiet", "-o", "-"]
|
37 |
+
|
38 |
+
try:
|
39 |
+
ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=-1)
|
40 |
+
ytdl_process = subprocess.Popen(ytdl_command, stdout=ffmpeg_process.stdin)
|
41 |
+
except FileNotFoundError:
|
42 |
+
raise ValueError("ffmpeg was not found but is required to stream audio files from filename")
|
43 |
+
|
44 |
+
running = True
|
45 |
+
acc = b""
|
46 |
+
leftover = np.zeros((0,), dtype=np.float32)
|
47 |
+
while running and ytdl_process.poll() is None:
|
48 |
+
buflen = read_chunk_len * size_of_sample
|
49 |
+
|
50 |
+
raw = ffmpeg_process.stdout.read(buflen)
|
51 |
+
if raw == b"":
|
52 |
+
running = False
|
53 |
+
break
|
54 |
+
|
55 |
+
if len(acc) + len(raw) > buflen:
|
56 |
+
acc = raw
|
57 |
+
else:
|
58 |
+
acc += raw
|
59 |
+
|
60 |
+
audio = np.frombuffer(acc, dtype=dtype)
|
61 |
+
audio = np.concatenate([leftover, audio])
|
62 |
+
if len(audio) < pad_len * 2:
|
63 |
+
# TODO: handle end of stream better than this
|
64 |
+
running = False
|
65 |
+
break
|
66 |
+
yield audio
|
67 |
+
|
68 |
+
leftover = audio[-pad_len * 2 :]
|
69 |
+
read_chunk_len = chunk_len
|