CoolFace
Apppublic

vslasor/VLS4-ArtificialSuperIntelligenceASRVideo-SL

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
streaming.py66 linesDownload Raw Back to root
1import subprocess2 3import numpy as np4 5 6def 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 * 213 14    ar = f"{sampling_rate}"15    ac = "1"16    format_for_conversion = "f32le"17    dtype = np.float3218    size_of_sample = 419 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_sample48 49        raw = ffmpeg_process.stdout.read(buflen)50        if raw == b"":51            break52 53        if len(acc) + len(raw) > buflen:54            acc = raw55        else:56            acc += raw57 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 this62            break63        yield audio64 65        leftover = audio[-pad_len * 2 :]66        read_chunk_len = chunk_len