C++ ffmpeg "Starting new cluster" 错误

c++ ffmpeg "Starting new cluster" error

我想将实时流编码为 webm,但 ffmpeg 在 5 秒后卡在实时锁定状态

[webm @ 0x1d81940] Starting new cluster at offset 0 bytes, pts 5040dts 5040

我尝试增加相关的 AVFormatContext 参数

av_opt_set_int(oc->priv_data, "chunk_duration", INT_MAX, 0);
av_opt_set_int(oc->priv_data, "cluster_time_limit", INT_MAX, 0);
av_opt_set_int(oc->priv_data, "cluster_size_limit", INT_MAX, 0);

这避免了大约 30 秒的错误,但随后又 ffmpeg 挂起

[webm @ 0xbc9940] Starting new cluster due to timestamp
[webm @ 0xbc9940] Starting new cluster at offset 0 bytes, pts 32800dts 32800

这个错误可以通过官方示例重现doc/examples/muxing.c只需写入缓冲区而不是像这样的文件

oc = avformat_alloc_context();
oc->oformat = av_guess_format("webm", NULL, NULL);
oc->oformat->flags |= AVFMT_NOFILE;

和实际写作

uint8_t *output_buf;
avio_open_dyn_buf(&oc->pb);

avformat_write_header(oc, &opt);
/* or */
av_interleaved_write_frame(fmt_ctx, pkt);

avio_close_dyn_buf(oc->pb, &output_buf);
av_free(output_buf);

如何将 webm 编码到缓冲区中? (为什么它适用于文件?)

可能 webm 格式执行某种异步写入操作,这些操作以某种方式与 av_write_frame 调用分离。 但是,我能够通过实施适当的 AVIOContext.

来解决我的问题

要设置自定义 IO,您需要一个回调函数,它应该如下所示:

static int dispatch_output_packet(void* opaque, uint8_t* buffer, int buffer_size)
{
    YourOutputType *out = (YourOutputType*) opaque;
    int result = out->do_something(buffer, buffer_size);
    return 0;
}

然后创建一个AVIOContext并插入:

size_t io_buffer_size = 3 * 1024 * 1024;
io_buffer = new unsigned char[io_buffer_size];
AVIOContext* io_ctx = avio_alloc_context(io_buffer, io_buffer_size, AVIO_FLAG_WRITE, &your_output_object, NULL, dispatch_output_packet, NULL);
io_ctx->seekable = 0;
format_context_->oformat->flags |= AVFMT_FLAG_CUSTOM_IO;
format_context_->oformat->flags |= AVFMT_NOFILE;
format_context_->pb = io_ctx;