You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何在单个AVFormatContext中处理多媒体源时源顺序至关重要?

问题:单个AVFormatContext动态切换不同多媒体源的异常问题

我尝试在单个AVFormatContext中动态打开格式与编码各异的多媒体源,包括BlackMagic DeckLink Duo SDI输入、MP4文件或RTSP流。

当先调用avformat_open_input打开MP4/RTSP流,再打开DeckLink设备时流程正常;但先打开DeckLink再打开MP4/RTSP流时,avformat_open_input会删除AVFormatContext并返回-6,后续调用avformat_find_stream_info会触发段错误。

错误复现代码

AVFormatContext* context{avformat_alloc_context()};
const char* url_source1{"DeckLink Duo (1)"};
const AVInputFormat* format_source1{av_find_input_format("decklink")};

const char* url_source2{"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"};

// 打开第一个媒体输入
int result = avformat_open_input(&context, url_source1, format_source1, NULL);

if(result < 0) {
  exit(1);
}

// 打开第二个媒体输入
// 当前顺序下该函数会删除context并返回-6
result = avformat_open_input(&context, url_source2, NULL, NULL);
if(result < 0) {
  exit(1);
}

// 由于上一步context已被删除,此处会触发段错误!
result = avformat_find_stream_info(context, NULL);
if(result < 0) {
  exit(1);
}

std::cout << "Total number of streams: " << context->nb_streams << std::endl;

正常执行的代码顺序

AVFormatContext* context{avformat_alloc_context()};
const char* url_source1{"DeckLink Duo (1)"};
const AVInputFormat* format_source1{av_find_input_format("decklink")};

const char* url_source2{"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"};


// 打开第二个媒体输入
int result = avformat_open_input(&context, url_source2, NULL, NULL);
if(result < 0) {
  exit(1);
}


// 打开第一个媒体输入
result = avformat_open_input(&context, url_source1, format_source1, NULL);

if(result < 0) {
  exit(1);
}


result = avformat_find_stream_info(context, NULL);
if(result < 0) {
  exit(1);
}

std::cout << "Total number of streams: " << context->nb_streams << std::endl;

内容的提问来源于stack exchange,提问作者Mehmet YILMAZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:24:40