如何使用 ffmpeg 复制数据流?

问题描述

我正在尝试使用 ffmpeg 直接流式复制视频文件。 (备注:我通过 FFmpeg.AutoGen 库使用 C# 中的 ffmpeg,但这与问题无关。)

这里是一些简化的代码(例如省略了错误处理):

// Open input file:
fixed (AVFormatContext** formatContextPtrPtr = &inputFormatContextPtr)
{
    ffmpeg.avformat_open_input(formatContextPtrPtr,inputFileName,null,null);
}
ffmpeg.avformat_find_stream_info(this.inputFormatContextPtr,null);

// Open output file
AVIOContext* outputIOContextPtr;
ffmpeg.avio_open(&outputIOContextPtr,outputFileName,ffmpeg.AVIO_FLAG_WRITE);

// Create output context
AVFormatContext* outputFormatContextPtr;
ffmpeg.avformat_alloc_output_context2(&outputFormatContextPtr,outputFileName);
outputFormatContextPtr->pb = outputIOContextPtr;

// Create the same streams in the output context as in the input context
AVStream** streamsPointer = formatContext.streams;
for (int i = 0; i < streams.Length; i++)
{
    AVStream inputStream = **streamsPointer;
    
    AVStream* newStream = ffmpeg.avformat_new_stream(outputFormatContextPtr,null);
    ffmpeg.avcodec_parameters_copy(newStream->codecpar,inputStream.codecpar);

    streamsPointer++;
}

// Write header
ffmpeg.avformat_write_header(outputFormatContextPtr,null);

// copy all streams packet by packet
do
{
    AVPacket* packetPtr = ffmpeg.av_packet_alloc();
    ffmpeg.av_read_frame(inputFormatContextPtr,packetPtr);

    ffmpeg.av_write_frame(outputFormatContextPtr,packetPtr);
    
    ffmpeg.av_packet_unref(packetPtr);
    ffmpeg.av_free(packetPtr);
}

// Close input and output files and contexts
ffmpeg.av_write_trailer(outputFormatContextPtr);
ffmpeg.avformat_free_context(outputFormatContextPtr);
ffmpeg.avio_close(outputIOContextPtr);

fixed (AVFormatContext** formatContextPtrPtr = &inputFormatContextPtr)
{
    ffmpeg.avformat_close_input(formatContextPtrPtr);
}

只要流是视频或音频流(AVMEDIA_TYPE_VIDEOAVMEDIA_TYPE_AUdio),它就可以工作,但我收到来自 avformat_write_header 或 {{1} 的错误} 如果流是数据流 (AVMEDIA_TYPE_DATA)。

例如 gopro 视频 (.mp4) 文件具有包含 GPS 和其他传感器数据的数据流(除了视频和音频流):

av_write_frame

对于流 #2 和 #4,我从 Stream #0 Type: AVMEDIA_TYPE_VIDEO Codec: AV_CODEC_ID_H264 Frame rate: 29,97 fps Duration: 00:00:03.7037000 Frame number: 111 Stream #1 Type: AVMEDIA_TYPE_AUdio Codec: AV_CODEC_ID_AAC Frame rate: NaN fps Duration: 00:00:03.7120000 Frame number: 174 Stream #2 Type: AVMEDIA_TYPE_DATA Codec: AV_CODEC_ID_NONE Frame rate: 29,00 fps Duration: 00:00:03.7037000 Frame number: 1 Stream #3 Type: AVMEDIA_TYPE_DATA Codec: AV_CODEC_ID_BIN_DATA Frame rate: NaN fps Duration: 00:00:03.7120000 Frame number: 3 Stream #4 Type: AVMEDIA_TYPE_DATA Codec: AV_CODEC_ID_NONE Frame rate: NaN fps Duration: 00:00:03.7037000 Frame number: 290 得到 0xffffffea "Invalid argument" 错误代码,控制台输出显示以下文本:"Could not find流 #2 中没有编解码器的标记,容器中当前不支持编解码器"

对于流 #3,我从 avformat_write_header 得到相同的错误代码,控制台输出显示以下文本:"Invalid packet stream index: 3"

知道如何复制这些数据流吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)