我正在开发一种工具,该工具将从网络接收h.264实时流(发送者是硬件编码器),缓冲最后的x分钟,并在触发后创建最后x分钟的视频文件。
我的工具既可以接收实时流,也可以使用boost :: serialization缓冲AVPackets。此外,我能够加载缓冲的AVPackets。
但是,当我尝试使用加载的AVPackets创建/混合视频文件(mp4)时,mp4文件或mp4标头中会出现错误。我能够创建mp4文件,也可以使用av_interleaved_write_frame()将数据包写入文件。mp4文件的大小也合理。因此,它表明AVPackets已写入文件。
但是,当我尝试使用vlc等播放器打开mp4文件时,视频无法播放。播放器提供以下错误:标题不正确,不播放任何帧。但是我将带有writeHeader()函数的标头写入AVFormatContext。
我想这是因为AVFormatContext没有正确初始化,但是使用ffmpeg / libav(混合示例)示例无法解决问题。
有没有一个解决方案(示例代码)或另一个示例如何正确初始化AVFormatContext?
谢谢你的帮助。奥尔根
avcodec_init();
avcodec_register_all();
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
char *outputFilename = "/tmp/output.mp4";
// define AVOutputFormat
AVOutputFormat *outputFormat = NULL;
outputFormat = av_guess_format("mp4", NULL, NULL);
if (!outputFormat) {
fprintf(stderr, "Could not find suitable output format\n");
return 1;
}
// define AVFormatContext
AVFormatContext *formatContext = NULL;
// lallocate the output media context
std::cout << "alloc" << std::endl;
formatContext = avformat_alloc_context();
// avformat_alloc_output_context(&formatContext, outputFormat, NULL, NULL);
if (!formatContext) {
fprintf(stderr, "Memory error\n");
return 1;
}
std::cout …Run Code Online (Sandbox Code Playgroud)