ffmpeg:每个块刷新输出文件

ffmpeg: flushing output file every chunk

我正在使用 ffmpeg 实时生成 10 秒的正弦音。不幸的是,ffmpeg 似乎很少刷新输出文件,每隔几秒。我希望它每 2048 字节刷新一次(=2 字节样本宽度*1024 样本,我的自定义块大小)。

以下脚本的输出:

import os
import time
import subprocess

cmd = 'ffmpeg -y -re -f lavfi -i "sine=frequency=440:duration=10" -blocksize 2048 test.wav'    

subprocess.Popen(cmd, shell=True)

time.sleep(0.1)
while True:
    print(os.path.getsize("test.wav"))
    time.sleep(0.1)

看起来像:

[...]
78
78
78
262222
262222
262222
[...]

#ffmpeg IRC 上的一位用户提议使用

ffmpeg -re -f lavfi -i "sine=frequency=1000:duration=10" -f wav pipe: > test.wav

有效。但是仅使用ffmpeg就可以实现吗?

对于输出到文件,ffmpeg 在写入之前等待填充 256 KiB 的缓冲区。

您可以禁用该行为,使用 flush_packets

ffmpeg -y -re -f lavfi -i "sine=f=440:d=10" -blocksize 2048 -flush_packets 1 test.wav