Using ffmpeg to create mp4 file from video stream

I’m reading data from a video stream and using ffmpeg to create a mp4 file. After writing data for a period of time I always get a file with a shorter duration. For example, if I write data for 10 minutes, the final file duration is sometimes 9.8 or 9.6 (the result varies a lot).
Here is how I’m doing:

char command[1024];
sprintf(command, "ffmpeg -y -hide_banner -nostats -loglevel warning -f rawvideo -vcodec rawvideo -pix_fmt yuv420p -r 15 -s 640x480 -i - -an -vsync vfr -vcodec libx264 -vf \"scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2\" %s", video_filename.c_str());
output_video_pipe = popen(command, "w");

Then I write data with:

fwrite(video_buffer, 1, video_buffer_size, output_video_pipe);

I tried different values for the -vsync parameter but the file is always shorter than it should be. Any tips on how I can make the file have the correct duration?

Any help will be greatly appreciated.


Read more here: Source link