8

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?

11

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
2
  • how can i detect is this working or no ? i put -blocksize 32 -flush_packets 1 but it output is 600 byte data after 3-4 second! i use libopencore_amrnb codec and need to get data every second. – peiman F. Dec 27 '19 at 20:27
  • libopencore_amrnb might be doing some internal buffering? – rogerdpack Apr 7 '20 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.