简体   繁体   中英

Unable to flush buffer data to disk when using FFmpeg and avio library to write a mp4 video file

I am using FFmpeg to create a mp4 file from screenshots and it works fine. But I want to flush the video to disk every 30 seconds so that in the case of program crash I don't lose everything. I tried to use avio_flush method, but the problem is I have avformat_write_header(AVFormatContext* s) methos at the initialization and if I don't close it using av_write_trailer(AVFormatContext* s) it doesn't save the video correctly. If I close and reopen it, only the first portion gets saved (even if file size increases). I tried everything I could think of but I am stuck.

thanks in advance.

For this use-case you could use MKV container instead of MP4. MKV does not need the av_write_trailer to be called for being valid. When you record on MKV containers, you could watch the video on the fly while the file is still open and you are writing to it. So in case of a crash you don't miss anything that has been written to video file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM