awacke1 commited on
Commit
f0d99e0
1 Parent(s): b45f867

Create streaming.py

Browse files
Files changed (1) hide show
  1. streaming.py +66 -0
streaming.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ acc = b""
45
+ leftover = np.zeros((0,), dtype=np.float32)
46
+ while ytdl_process.poll() is None:
47
+ buflen = read_chunk_len * size_of_sample
48
+
49
+ raw = ffmpeg_process.stdout.read(buflen)
50
+ if raw == b"":
51
+ break
52
+
53
+ if len(acc) + len(raw) > buflen:
54
+ acc = raw
55
+ else:
56
+ acc += raw
57
+
58
+ audio = np.frombuffer(acc, dtype=dtype)
59
+ audio = np.concatenate([leftover, audio])
60
+ if len(audio) < pad_len * 2:
61
+ # TODO: handle end of stream better than this
62
+ break
63
+ yield audio
64
+
65
+ leftover = audio[-pad_len * 2 :]
66
+ read_chunk_len = chunk_len