2

The Question

I want to load an audio file of any type (mp3, m4a, flac, etc) and write it to an output stream.

I tried using pydub, but it loads the entire file at once which takes forever and runs out of memory easily. I also tried using python-vlc, but it's been unreliable and too much of a black box.

So, how can I open large audio files chunk-by-chunk for streaming?

Edit #1

I found half of a solution here, but I'll need to do more research for the other half.

TL;DR: Use subprocess and ffmpeg to convert the file to wav data, and pipe that data into np.frombuffer. The problem is, the subprocess still has to finish before frombuffer is used.

...unless it's possible to have the pipe written to on 1 thread while np reads it from another thread, which I haven't tested yet. For now, this problem is not solved.

user81198
  • 21
  • 2

1 Answers1

0

I think the python package https://github.com/irmen/pyminiaudio can be of helpful. You can stream an audio file like this

import miniaudio


audio_path = "my_audio_file.mp3"
target_sampling_rate = 44100  #the input audio will be resampled a this sampling rate
n_channels = 1  #either 1 or 2
waveform_duration = 30 #in seconds
offset = 15 #this means that we read only in the interval [15s, duration of file]

waveform_generator = miniaudio.stream_file(
     filename = audio_path,
     sample_rate = target_sampling_rate,
     seek_frame = int(offset * target_sampling_rate),
     frames_to_read = int(waveform_duration * target_sampling_rate),
     output_format = miniaudio.SampleFormat.FLOAT32,
     nchannels = n_channels)


for waveform in waveform_generator:
    #do something with the waveform....

I know for sure that this works on mp3, ogg, wav, flac but for some reason it does not on mp4/acc and I am actually looking for a way to read mp4/acc

The Wave
  • 19
  • 4