gpt4 book ai didi

c++ - 使用 FFMPEG 编码 H264 时如何转储缓冲区?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:21 29 4
gpt4 key购买 nike

我正在使用 C++ 库将从网络摄像头捕获的图像写入 libx264 编码的 mp4 文件。编码工作正常,但当它开始时,它会向缓冲区写入 40 帧。当我关闭文件时,这些帧不会刷新,因此大约有 6 秒的视频未写入(cam 约为 6fps)。

所以我调用:

out_size = libffmpeg::avcodec_encode_video( codecContext, data->VideoOutputBuffer,data->VideoOutputBufferSize, data->VideoFrame );

// if zero size, it means the image was buffered
if ( out_size > 0 )
{
//... write to file
}

我看不到访问缓冲区中剩余图像的方法。有什么想法吗?

最佳答案

我已经使用以下代码刷新缓冲区来完成这项工作。似乎我在搜索错误的术语 - 应该是“延迟帧”...

void VideoFileWriter::Flush(void)
{
if ( data != nullptr )
{
int out_size = 0;
int ret = 0;

libffmpeg::AVCodecContext* c = data->VideoStream->codec;
/* get the delayed frames */
while (1) {
libffmpeg::AVPacket packet;
libffmpeg::av_init_packet(&packet);

out_size = libffmpeg::avcodec_encode_video(c, data->VideoOutputBuffer, data->VideoOutputBufferSize, NULL);
if (out_size < 0) {
//fprintf(stderr, "Error encoding delayed frame %d\n", out_size);
break;
}
if (out_size == 0) {
break;
}
if (c->coded_frame->pts != AV_NOPTS_VALUE) {
packet.pts = av_rescale_q(c->coded_frame->pts,
c->time_base,
data->VideoStream->time_base);
//fprintf(stderr, "Video Frame PTS: %d\n", (int)packet.pts);
} else {
//fprintf(stderr, "Video Frame PTS: not set\n");
}
if (c->coded_frame->key_frame) {
packet.flags |= AV_PKT_FLAG_KEY;
}
packet.stream_index = data->VideoStream->index;
packet.data = data->VideoOutputBuffer;
packet.size = out_size;

ret = libffmpeg::av_interleaved_write_frame( data->FormatContext, &packet );
if (ret != 0) {
//fprintf(stderr, "Error writing delayed frame %d\n", ret);
break;
}
}
libffmpeg::avcodec_flush_buffers(data->VideoStream->codec);
}
}

关于c++ - 使用 FFMPEG 编码 H264 时如何转储缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518829/

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