Olg*_*013 5 c++ video mp4 ffmpeg libav
我正在开发一种工具,该工具将从网络接收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 << "stream" << std::endl;
AVStream * video_st = av_new_stream(formatContext, 0);
AVCodec * codec = NULL;
avcodec_get_context_defaults3(video_st->codec, codec);
video_st->codec->coder_type = AVMEDIA_TYPE_VIDEO;
video_st->codec->flags = fc->streams[0]->codec->flags;
video_st->codec->sample_aspect_ratio = fc->streams[0]->codec->sample_aspect_ratio;
video_st->disposition = fc->streams[0]->disposition;
video_st->codec->codec_tag = fc->streams[0]->codec->codec_tag;
video_st->codec->bits_per_raw_sample = fc->streams[0]->codec->bits_per_raw_sample;
video_st->codec->chroma_sample_location = fc->streams[0]->codec->chroma_sample_location;
video_st->codec->codec_id = fc->streams[0]->codec->codec_id;
video_st->codec->codec_tag = fc->streams[0]->codec->codec_tag;
video_st->codec->time_base = fc->streams[0]->codec->time_base;
video_st->codec->extradata = fc->streams[0]->codec->extradata;
video_st->codec->extradata_size = fc->streams[0]->codec->extradata_size;
video_st->codec->pix_fmt = fc->streams[0]->codec->pix_fmt;
video_st->codec->width = fc->streams[0]->codec->width;
video_st->codec->height = fc->streams[0]->codec->height;
video_st->codec->sample_aspect_ratio = fc->streams[0]->codec->sample_aspect_ratio;
std::cout << "file" << std::endl;
avio_open(&formatContext->pb, outputFilename, AVIO_FLAG_WRITE);
std::cout << "header" << std::endl;
avformat_write_header(formatContext, NULL);
// av_write_header(formatContext);
// loop to write AVPackets with av_write_frame
Run Code Online (Sandbox Code Playgroud)
avformat_write_header(formatContext,NULL)出现内存错误后,程序崩溃。我还尝试了avformat_alloc_output_context()而不是avformat_alloc_output_context2(),但两个函数均不起作用。所以我用了avformat_alloc_context()(见上文)
我认为存在分配问题,但是为什么我不能使用avformat_alloc_output_context2或avformat_alloc_output_context。有人可以在这里解决我的问题吗?
| 归档时间: |
|
| 查看次数: |
10497 次 |
| 最近记录: |