c ++ ffmpeg"启动新集群"错误

时间:2016-10-27 11:59:24

标签: c++ ffmpeg webm

我想将实时流编码为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编码为缓冲区? (为什么它适用于文件?)

1 个答案:

答案 0 :(得分:0)

可能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;