Search code examples
c++ffmpegmp4h.264

How to set the moov atom position when encoding video using ffmpeg in c++


I'm encoding some h264 video into a mp4 container using ffmpeg in c++. But the result videos place the moov atom(or metadata?) at the end of the video file, it's bad for internet streaming. So how can I set the moov atom position to the front?


Solution

  • You need to use faststart flag of ffmpeg to place the moov atom in the beginning of the MP4 file, Here is the explanation of the flag. Programatically you need to set the flag in output context, here is the sample code and its working for me,

    AVFormatContext *outFormatCtx;
    
    // Write MOOV atom at the begining of the MP4 file
    MOVMuxContext *mov = NULL;
    
    mov = (MOVMuxContext *)outFormatCtx->priv_data;
    mov->flags |= FF_MOV_FLAG_FASTSTART;