I have to pipe a wave data
stream to ffmpeg in Python. I can easily create an output pipe from an input mp3
file like:
process = (
ffmpeg
.input(path)
.output('pipe:', **output_kwargs)
.run_async(pipe_stdout=True, pipe_stderr=True))
buffer, _ = process.communicate()
# because of we need (n_channels, samples)
waveform = np.frombuffer(buffer, dtype='<f4').reshape(-1, n_channels)
if not waveform.dtype == np.dtype(dtype):
waveform = waveform.astype(dtype)
Here waveform
will contain the wave audio file.
Now, I want to pipe the same data, but from an input stream, but for some reason it does not work as expected:
# data shape is like (9161728, 2) for two channels audio data
input_kwargs = {'ar': sample_rate, 'ac': data.shape[1]}
output_kwargs = {'ar': sample_rate, 'strict': '-2'}
n_channels = 2
process = (
ffmpeg
.input('pipe:', format='f32le', **input_kwargs)
.output('pipe:', **output_kwargs)
.run_async(pipe_stdin=True, quiet=True))
buffer, err = process.communicate(input=data.astype('<f4').tobytes())
The output buffer
is empty here after getting the results from process.communicate
, while err
is
Unable to find a suitable output format for 'pipe:'\npipe:: Invalid argument\n"