C++ FFMPEG "Starting new cluster"错误

c++ ffmpeg "Starting new cluster" error

本文关键字:cluster 错误 new Starting FFMPEG C++      更新时间:2023-10-16

我想将直播流编码到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;