美文网首页
Ffmpeg Muxer 替换视频音频,添加多个音轨

Ffmpeg Muxer 替换视频音频,添加多个音轨

作者: 贼噶人 | 来源:发表于2019-08-16 12:43 被阅读0次
static void muxer(){
    AVFormatContext *videoFormatContext = NULL;
    avformat_open_input(&videoFormatContext,"/home/gangzhou/zaojiaoji.mp4",NULL,NULL);
    if(NULL == videoFormatContext){
        printf("avformat_open_input video error\n");
        exit(0);
    }
    if(avformat_find_stream_info(videoFormatContext,NULL) < 0){
        printf("avformat_find_stream_info video error\n");
        exit(0);
    }
    AVFormatContext *audioFormatContext = NULL;
    avformat_open_input(&audioFormatContext,"/home/gangzhou/mv.mp4",NULL,NULL);
    if(NULL == audioFormatContext){
        printf("avformat_open_input audio error\n");
        exit(0);
    }
    if(avformat_find_stream_info(audioFormatContext,NULL) < 0){
        printf("avformat_find_stream_info video error\n");
        exit(0);
    }
    AVFormatContext *outAVFormatContext = NULL;
    avformat_alloc_output_context2(&outAVFormatContext,NULL,NULL,"home/gangzhou/out.mp4");
    if(NULL == outAVFormatContext){
        printf("avformat_alloc_output_context2 video error\n");
        exit(0);
    }
    int videoStreamIndex;
    int outVideoStreamIndex;
    int audioStreamIndex1;
    int outAudioStreamIndex1;
    for (int i = 0;i < videoFormatContext->nb_streams;++i){
        AVStream *avStream = videoFormatContext->streams[i];
        if(AVMEDIA_TYPE_VIDEO == avStream->codecpar->codec_type){
            AVStream *outStream = avformat_new_stream(outAVFormatContext
                    ,avStream->codec->codec);
            videoStreamIndex = i;
            if(NULL == outStream){
                printf("avformat_new_stream video error\n");
                exit(0);
            }
            outVideoStreamIndex = outStream->index;
            if(avcodec_parameters_copy(outStream->codecpar,avStream->codecpar) < 0){
                printf("avcodec_parameters_copy video error\n");
                exit(0);
            }
            outStream->time_base.num = avStream->time_base.num;
            outStream->time_base.den = avStream->time_base.den;
            outStream->duration = avStream->duration;
            outStream->avg_frame_rate.num = avStream->avg_frame_rate.num;
            outStream->avg_frame_rate.den = avStream->avg_frame_rate.den;
        }else if(AVMEDIA_TYPE_AUDIO == avStream->codecpar->codec_type){
            AVStream *outStream = avformat_new_stream(outAVFormatContext
                    ,avcodec_find_encoder(avStream->codecpar->codec_id));
            audioStreamIndex1 = i;
            if(NULL == outStream){
                printf("avformat_new_stream audio error\n");
                exit(0);
            }
            outAudioStreamIndex1 = outStream->index;
            if(avcodec_parameters_copy(outStream->codecpar,avStream->codecpar) < 0){
                printf("avcodec_parameters_copy audio error\n");
                exit(0);
            }
            break;
        }
    }
    int audioStreamIndex;
    int outAudioStreamIndex;
    for (int i = 0;i < audioFormatContext->nb_streams;++i){
        AVStream *avStream = audioFormatContext->streams[i];
        if(AVMEDIA_TYPE_AUDIO == avStream->codecpar->codec_type){
            AVStream *outStream = avformat_new_stream(outAVFormatContext
                    ,avcodec_find_encoder(avStream->codecpar->codec_id));
            audioStreamIndex = i;
            if(NULL == outStream){
                printf("avformat_new_stream audio error\n");
                exit(0);
            }
            outAudioStreamIndex = outStream->index;
            if(avcodec_parameters_copy(outStream->codecpar,avStream->codecpar) < 0){
                printf("avcodec_parameters_copy audio error\n");
                exit(0);
            }
            break;
        }
    }
    if(!(outAVFormatContext->flags & AVFMT_NOFILE)){
        if(avio_open(&outAVFormatContext->pb,"/home/gangzhou/out.mp4",AVIO_FLAG_WRITE) < 0){
            printf("avio_open  error\n");
            exit(0);
        }
    }
    if(avformat_write_header(outAVFormatContext,NULL) < 0){
        printf("avformat_write_header  error\n");
        exit(0);
    }
    AVPacket *avPacket = av_packet_alloc();
    while (1){
        if(!av_read_frame(videoFormatContext,avPacket)){
            if(avPacket->stream_index == videoStreamIndex){
                avPacket->stream_index = outVideoStreamIndex;
                avPacket->pos = -1;
                av_interleaved_write_frame(outAVFormatContext,avPacket);
                av_packet_unref(avPacket);
            }

            if(avPacket->stream_index == audioStreamIndex1) {
                avPacket->stream_index = outAudioStreamIndex1;
                av_interleaved_write_frame(outAVFormatContext, avPacket);
                av_packet_unref(avPacket);
            }
        } else{
            break;
        }
        if(!av_read_frame(audioFormatContext,avPacket)){
            if(avPacket->stream_index == audioStreamIndex) {
                avPacket->stream_index = outAudioStreamIndex;
                av_interleaved_write_frame(outAVFormatContext, avPacket);
                av_packet_unref(avPacket);
            }
        } else{
            break;
        }
    }
    av_write_trailer(outAVFormatContext);
}

相关文章

网友评论

      本文标题:Ffmpeg Muxer 替换视频音频,添加多个音轨

      本文链接:https://www.haomeiwen.com/subject/gfmfsctx.html