目前,使用libavformat从代码生成碎片化MP4文件时遇到问题.我的文件可以使用VLC播放,但无法在(Chrome)浏览器中流式传输(通过WebSocket)和播放(通过MediaSource).(我用它来通过WebSocket测试流式分段MP4文件到浏览器).
注意:下面的文件由Baseline profile 4级编码.因此,您应该将MIME类型(在index.html中)更改为const mimeCodec ='video/mp4; 编解码器= "avc1.42C028"'; 能够发挥它们.
我检查并发现我生成的Mp4文件与使用ffmpeg工具生成的文件略有不同.
这就是我所做的:
我有一个.h264 文件
第一种方法,我使用ffmpeg生成碎片化的MP4文件.
ffmpeg -i temp.h264 -vcodec copy -f mp4 -movflags empty_moov+default_base_moof+frag_keyframe ffmpeg.mp4
Run Code Online (Sandbox Code Playgroud)
生成的文件可以由Quicktime播放器和VLC播放器播放
第二种方法,我使用libavformat以 programmaticaly方式生成碎片化的Mp4文件
首先我初始化上下文,codecin代码是AVCodecContext*输入流的
av_register_all();
avcodec_register_all();
int ret;
AVOutputFormat* fmt = av_guess_format("mp4", 0, 0);
if(!fmt) {
return;
}
AVFormatContext* ctx = avformat_alloc_context();
// Create AVIO context to capture generated Mp4 contain
uint8_t *avio_ctx_buffer = NULL;
size_t avio_ctx_buffer_size = 50000;
IOOutput buffer = {};
const …Run Code Online (Sandbox Code Playgroud)