gpt4 book ai didi

ffmpeg - 将 AVPackets 复用到 mp4 文件中 - 重新审视

转载 作者:行者123 更新时间:2023-12-04 22:59:01 27 4
gpt4 key购买 nike

我在这里指的是这个线程:
Muxing AVPackets into mp4 file

那边的问题与我的问题基本相同,第一个答案看起来很有希望。
用户 pogorskiy 提供的源代码(某种伪)似乎完全符合我的需要:

AVOutputFormat * outFmt = av_guess_format("mp4", NULL, NULL);
AVFormatContext *outFmtCtx = NULL;
avformat_alloc_output_context2(&outFmtCtx, outFmt, NULL, NULL);
AVStream * outStrm = av_new_stream(outFmtCtx, 0);

AVCodec * codec = NULL;
avcodec_get_context_defaults3(outStrm->codec, codec);
outStrm->codec->coder_type = AVMEDIA_TYPE_VIDEO;

///....
/// set some required value, such as
/// outStrm->codec->flags
/// outStrm->codec->sample_aspect_ratio
/// outStrm->disposition
/// outStrm->codec->codec_tag
/// outStrm->codec->bits_per_raw_sample
/// outStrm->codec->chroma_sample_location
/// outStrm->codec->codec_id
/// outStrm->codec->codec_tag
/// outStrm->codec->time_base
/// outStrm->codec->extradata
/// outStrm->codec->extradata_size
/// outStrm->codec->pix_fmt
/// outStrm->codec->width
/// outStrm->codec->height
/// outStrm->codec->sample_aspect_ratio
/// see ffmpeg.c for details

avio_open(&outFmtCtx->pb, outputFileName, AVIO_FLAG_WRITE);

avformat_write_header(outFmtCtx, NULL);

for (...)
{
av_write_frame(outFmtCtx, &pkt);
}

av_write_trailer(outFmtCtx);
avio_close(outFmtCtx->pb);
avformat_free_context(outFmtCtx);

pkt 数据,我从我的 connectec 相机的第三方 API 收到。没有要打开的文件来读取输入数据,也没有要从相机接收的 RTSP 流。这只是一个 API 调用,它为我提供了指向 H264 编码帧的指针,这正是 AVPacket 的原始数据。

无论如何,我尝试将此代码用作我的应用程序的基础,但发生的第一个问题是,我得到一个运行时错误:
Could not find tag for codec none in stream #0, codec not currently supported in container

所以我开始向编解码器添加更多信息,正如 pogorskiy 建议的那样:
outStrm->codec->codec_id = AV_CODEC_ID_H264;
outStrm->codec->width = 1920;
outStrm->codec->height = 1080;

现在我提供了一个 codec_id,我希望运行时消息至少会更改为不同的东西,但它仍然是相同的:
Could not find tag for codec none in stream #0, codec not currently supported in container

关于如何设置结构的任何想法,以便我可以打开一个 mp4 文件来写入我的数据包?

最佳答案

好的,我得到它的工作。至少我可以打开一个 mp4 文件并将我的 H264 编码数据包写入其中。该文件甚至在 VLC 中打开并显示第一帧......仅此而已,但这是一个开始。

所以我把代码放在她身上,以展示这个最小的解决方案。如果有人对此发表他/她的意见,我仍然很高兴,因为它仍然不能完美地工作......

char outputFileName[] = "camera.mp4";

av_log_set_level(AV_LOG_DEBUG);

AVOutputFormat * outFmt = av_guess_format("mp4", NULL, NULL);
AVFormatContext *outFmtCtx = NULL;
avformat_alloc_output_context2(&outFmtCtx, outFmt, NULL, NULL);
AVStream * outStrm = avformat_new_stream(outFmtCtx, NULL);
outStrm->id = 0;
outStrm->time_base = {1, 30};
outStrm->avg_frame_rate = {1, 30};

AVCodec * codec = NULL;
avcodec_get_context_defaults3(outStrm->codec, codec);

outFmtCtx->video_codec_id = AV_CODEC_ID_H264;

///....
/// set some required value, such as
/// outStrm->codec->flags
/// outStrm->codec->sample_aspect_ratio
/// outStrm->disposition
/// outStrm->codec->codec_tag
/// outStrm->codec->bits_per_raw_sample
/// outStrm->codec->chroma_sample_location
outStrm->codecpar->codec_id = AV_CODEC_ID_H264;
outStrm->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
/// outStrm->codec->time_base
/// outStrm->codec->extradata
/// outStrm->codec->extradata_size
/// outStrm->codec->pix_fmt
outStrm->codecpar->width = 1920;
outStrm->codecpar->height = 1080;
/// outStrm->codec->sample_aspect_ratio
/// see ffmpeg.c for details

avio_open(&outFmtCtx->pb, outputFileName, AVIO_FLAG_WRITE);

avformat_write_header(outFmtCtx, NULL);

*** Camera access loop via GenICam API starts here ***
n++;
av_init_packet(&avPacket);
avPacket.data = static_cast<uint8_t*>(pPtr); // raw data from the Camera with H264 encoded frame
avPacket.size = datasize; // datasize received from the GenICam API along with pPtr (the raw data)
avPacket.pts = (1/30) * n; // stupid try to set pts and dts somehow... Working on this...
avPacket.dts = (1/30) * (n-1);
avPacket.pos = n;
avPacket.stream_index = outStrm->index;

av_write_frame(outFmtCtx, &avPacket);

**** Camera access loop ends here ****

av_write_trailer(outFmtCtx);
avio_close(outFmtCtx->pb);
avformat_free_context(outFmtCtx);

正如我所说,生成的 mp4 文件会在一瞬间显示第一帧,然后停止播放。
我认为会显示第一帧,因为我确保这是一个包含完整图像的 I 帧。

我不知道是否必须向复用器提供一些额外的数据才能获得工作的 mp4 文件。我仍在努力。

非常欢迎任何意见和想法!

谢谢,
迈克

关于ffmpeg - 将 AVPackets 复用到 mp4 文件中 - 重新审视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56054702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com