Search code examples
audioffmpegaudio-streamingflush

ffmpeg: flushing output file every chunk


I'm using ffmpeg to generate a sine tone in real time for 10 seconds. Unfortunately, ffmpeg seems to flush the output file only rarely, every few seconds. I'd like it to flush every 2048 bytes (=2bytes sample width*1024 samples, my custom chunk size).

The output of the following script:

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)

looks like:

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

A user on the #ffmpeg IRC proposed using

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

which works. But can this be achieved just using ffmpeg?


Solution

  • For output to a file, ffmpeg waits to fill a buffer of 256 KiB before a write.

    You can disable that behaviour, using flush_packets.

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