c++ - 提取视频文件的 h264 部分(解复用)

标签 c++ video ffmpeg

我正在尝试将视频文件解复用到视频部分 (h264、mpeg4、h265、vp8 等) 和音频部分 (mp3、aac、ac3 等) 和字幕部分 (srt) 在 c++ 中使用 ffmpeg。

音频部分效果不错,可以在我所有的媒体播放器上播放,字幕部分也可以。然而,视频部分毫无错误地出现并保存到 .h264 文件中,但是当我使用 ffprobe 检查它或使用 ffplay 播放它时,它总是给出错误 “处理输入时发现无效数据”

下面的代码

/* Separate a media file into audio, video and subtitle files (demuxing, complex) */
//TODO: mute error when subtitle is not present
#define __STDC_CONSTANT_MACROS

extern "C"
{
    #include "libavformat/avformat.h"
}


int main()
{
    //Input AVFormatContext and Output AVFormatContext
    AVOutputFormat *ofmt_a = NULL, *ofmt_v = NULL, *ofmt_s = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx_a = NULL, *ofmt_ctx_v = NULL, *ofmt_ctx_s = NULL;
    AVPacket pkt;

    int ret, i;
    int videoindex=-1, audioindex=-1, srtindex=-1;
    int frame_index=0;

    //Input file URL
    const char *in_filename  = "sample.mp4";

    //Output file URL
    const char *out_filename_v = "sample.h264";
    const char *out_filename_a = "sample.mp3";
    const char *out_filename_s = "sample.srt";

    av_register_all();

    //Input
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        printf( "Could not open input file.");
        goto end;
    }
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        printf( "Failed to retrieve input stream information");
        goto end;
    }

    //Output
    avformat_alloc_output_context2(&ofmt_ctx_v, NULL, NULL, out_filename_v);
    if (!ofmt_ctx_v) {
        printf( "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    ofmt_v = ofmt_ctx_v->oformat;

    avformat_alloc_output_context2(&ofmt_ctx_a, NULL, NULL, out_filename_a);
    if (!ofmt_ctx_a) {
        printf( "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    ofmt_a = ofmt_ctx_a->oformat;

    avformat_alloc_output_context2(&ofmt_ctx_s, NULL, NULL, out_filename_s);
    if (!ofmt_ctx_a) {
        printf( "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    ofmt_s = ofmt_ctx_s->oformat;

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
            //Create output AVStream according to input AVStream
            AVFormatContext *ofmt_ctx;
            AVStream *in_stream = ifmt_ctx->streams[i];
            AVStream *out_stream = NULL;

            if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
                videoindex=i;
                out_stream=avformat_new_stream(ofmt_ctx_v, in_stream->codec->codec);
                ofmt_ctx=ofmt_ctx_v;
            }
            else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
                audioindex=i;
                out_stream=avformat_new_stream(ofmt_ctx_a, in_stream->codec->codec);
                ofmt_ctx=ofmt_ctx_a;
            }
            else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
                srtindex=i;
                out_stream=avformat_new_stream(ofmt_ctx_s, in_stream->codec->codec);
                ofmt_ctx=ofmt_ctx_s;
            }
            else{
                break;
            }

            if (!out_stream) {
                printf( "Failed allocating output stream\n");
                ret = AVERROR_UNKNOWN;
                goto end;
            }
            //Copy the settings of AVCodecContext
            if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {
                printf( "Failed to copy context from input to output stream codec context\n");
                goto end;
            }
            out_stream->codec->codec_tag = 0;

            if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
                out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    //Dump Format------------------
    printf("\n==============Input Video=============\n");
    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    printf("\n==============Output Video============\n");
    av_dump_format(ofmt_ctx_v, 0, out_filename_v, 1);
    printf("\n==============Output Audio============\n");
    av_dump_format(ofmt_ctx_a, 0, out_filename_a, 1);
    /*printf("\n==============Output Subtitle============\n");
    av_dump_format(ofmt_ctx_s, 0, out_filename_s, 1);*/
    printf("\n======================================\n");

    //Open output file
    if (!(ofmt_v->flags & AVFMT_NOFILE)) {
        if (avio_open(&ofmt_ctx_v->pb, out_filename_v, AVIO_FLAG_WRITE) < 0) {
            printf( "Could not open output file '%s'", out_filename_v);
            goto end;
        }
    }
    if (!(ofmt_a->flags & AVFMT_NOFILE)) {
        if (avio_open(&ofmt_ctx_a->pb, out_filename_a, AVIO_FLAG_WRITE) < 0) {
            printf( "Could not open output file '%s'", out_filename_a);
            goto end;
        }
    }
    if (!(ofmt_a->flags & AVFMT_NOFILE)) {
        if (avio_open(&ofmt_ctx_s->pb, out_filename_s, AVIO_FLAG_WRITE) < 0) {
            printf( "Could not open output file '%s'", out_filename_s);
            goto end;
        }
    }

    //Write file header
    if (avformat_write_header(ofmt_ctx_v, NULL) < 0) {
        printf( "Error occurred when opening video output file\n");
        goto end;
    }
    system("pause");


    if (avformat_write_header(ofmt_ctx_a, NULL) < 0) {
        printf( "Error occurred when opening audio output file\n");
        goto end;
    }
    if (avformat_write_header(ofmt_ctx_s, NULL) < 0) {
        printf( "Error occurred when opening audio output file\n");
        goto end;
    }

    AVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb"); 

    while (1) {
        AVFormatContext *ofmt_ctx;
        AVStream *in_stream, *out_stream;
        //Get an AVPacket
        if (av_read_frame(ifmt_ctx, &pkt) < 0)
            break;
        in_stream  = ifmt_ctx->streams[pkt.stream_index];


        if(pkt.stream_index==videoindex){
            out_stream = ofmt_ctx_v->streams[0];
            ofmt_ctx=ofmt_ctx_v;
            printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
            av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
        }else if(pkt.stream_index==audioindex){
            out_stream = ofmt_ctx_a->streams[0];
            ofmt_ctx=ofmt_ctx_a;
            printf("Write Audio Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
        }
        else if(pkt.stream_index==srtindex){
            out_stream = ofmt_ctx_s->streams[0];
            ofmt_ctx=ofmt_ctx_s;
            printf("Write Subtitle Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
        }
        else{
            continue;
        }


        //Convert PTS/DTS
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        pkt.stream_index=0;
        //Write
        if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {
            printf( "Error muxing packet\n");
            break;
        }
        //printf("Write %8d frames to output file\n",frame_index);
        av_free_packet(&pkt);
        frame_index++;
    }

    av_bitstream_filter_close(h264bsfc);  

    //Write file trailer
    av_write_trailer(ofmt_ctx_a);
    av_write_trailer(ofmt_ctx_v);
    av_write_trailer(ofmt_ctx_s);
end:
    avformat_close_input(&ifmt_ctx);
    /* close output */
    if (ofmt_ctx_a && !(ofmt_a->flags & AVFMT_NOFILE))
        avio_close(ofmt_ctx_a->pb);

    if (ofmt_ctx_v && !(ofmt_v->flags & AVFMT_NOFILE))
        avio_close(ofmt_ctx_v->pb);

    if (ofmt_ctx_s && !(ofmt_s->flags & AVFMT_NOFILE))
        avio_close(ofmt_ctx_s->pb);

    avformat_free_context(ofmt_ctx_a);
    avformat_free_context(ofmt_ctx_v);
    avformat_free_context(ofmt_ctx_s);

    system("pause");
    if (ret < 0 && ret != AVERROR_EOF) {
        printf( "Error occurred.\n");
        return -1;
    }

    return 0;
}

编辑 1 生成的 h264 文件的屏幕截图 enter image description here

编辑 2 我认为“错误”与 FFMPEG 的“不推荐使用 AVStream.codec.time_base 作为多路复用器的时基提示。改为设置 AVStream.time_base”错误有关。 我恢复到旧版本的 FFMPEG 并使用相同的代码,生成的 h264 文件没问题!

最佳答案

您需要将 H.264 比特流从长度前缀模式转换为起始代码前缀模式。某些流媒体格式需要这样做,通常是 MPEG-2 传输流格式(“mpegts”)。

看看https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmp4toannexb

查看从 402 到 424 以及从 842 到 843 的行。 https://www.ffmpeg.org/doxygen/0.7/crystalhd_8c-source.html

我用它来从 mp4 中提取 h264。

//Use this filter on your first h264 input AVPacket
AVFormatContext *ifmt_ctx = NULL;
//...
//...   //init input
//...
AVPacket *firstPacket;
//...
//...   //get packet from stream
//...
uint8_t *dummy_p;
int dummy_int;
AVBitStreamFilterContext *filter = v_bitstream_filter_init("h264_mp4toannexb");
if (!filter)
{
    printf("Can't open filter\n");
    exit(1);
}
ret = av_bitstream_filter_filter(filter, ifmt_ctx->streams[videoindex]->codec, NULL,
                                           &dummy_p, &dummy_int,
                                           firstPacket->data, firstPacket->size, 0);
if( ret < 0 )
{
     printf("Can't filter\n");
     exit(1);
}
//  use dummy_p to write to file, as first packet

关于c++ - 提取视频文件的 h264 部分(解复用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524322/

相关文章:

c++ - 无法在动态链接库中找到过程入口点 _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev

c++ - 有条件地跳过 dll 依赖项

c# - 如何从USB摄像头获取的每一帧中提取时间戳?

video - FFMpeg 将 AVI 转换为 MP4 - 音频/视频不同步

android - 使用 Gstreamer 或 ffmpeg 在 Android 上创建 rtsp 客户端

c++ - 将线程指针绑定(bind)到对象时出错

c++ - openCV 中的彩色对象跟踪不断检测皮肤

python - 如何使用 gstreamer 查找媒体的长度?

c++ - 如何从 C++ 中的网络摄像头读取输入?

opencv - 如何在 Windows 中使用 openCV FFMPEG 视频 I/O 而不是 DirectShow ?