How to generate fragmented mp4 files programmatically

3.2k views Asked by At

I have a media .h264 live streaming server, and want to mux .h264 frames to fragmented mp4 files. I am wondering does any library could support this?

As far as I know, ffmpeg.exe and Bento4 could support this, but I want to use a library to do that in my code, not executing another process.

To specify my point, I want to generate fragmented mp4 files, which could be achieved by executing ffmpeg.exe like below,

ffmpeg -i xx.h264 
        -vcodec copy -an -f mp4 -reset_timestamps 0 
        -movflags empty_moov+default_base_moof+frag_keyframe -loglevel quiet 
        xxx.mp4"

I want to mux mp4 files in my code, not create another process to do it.

Thanks.

1

There are 1 answers

0
dajuric On

More detailed:

AVDictionary* opts = NULL;
av_dict_set(&opts, "movflags", "frag_keyframe+empty_moov", 0);

and then later:

//init muxer, write output file header
avformat_write_header(formatContext, &opts);

where formatContext is pointer of AVFormatContext obtained when output file is opened using: avformat_alloc_output_context2 and avio_open functions.