【发布时间】:2020-03-13 21:09:39
【问题描述】:
我必须在 Python 中将波 data 流传输到 ffmpeg。我可以轻松地从输入 mp3 文件创建输出管道,例如:
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)
这里waveform 将包含波形音频文件。
现在,我想通过管道传输相同的数据,但来自输入流,但由于某种原因它不能按预期工作:
# 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())
从process.communicate得到结果后,这里的输出buffer是空的,而err是
Unable to find a suitable output format for 'pipe:'\npipe:: Invalid argument\n"
【问题讨论】:
-
你也想用ffmpeg自己实现还是可以using other solutions.
-
谢谢,我使用的是
https://github.com/kkroening/ffmpeg-python,所以它应该很简单,但在同时使用input和output作为管道的情况下就不行了。