avcodec_copy_context() copy方式实现h264和mp3合成mp4

/**
实现h264和mp3合成mp4
通过avcodec_copy_context()函数可以将输入视频/音频的参数拷贝至输出视频/音频的AVCodecContext结构体。
不涉及编解码
*/
#include <stdio.h>
#include <stdint.h>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"

};

int64_t cur_pts_v = 0, cur_pts_a = 0;
int framecount;
void init_packet(AVPacket *packet){
    av_init_packet(packet);
    packet->data = NULL;
    packet->size = 0;
}

int main(int argc, char **argv){
    const char *in_filename_v = "cuc_ieschool.h264";
    const char *in_filename_a = "cuc_ieschool.mp3";
    const char *out_file = "cuc_ieschool.mp4";

    av_register_all();

    AVFormatContext *ifmt_ctx_v=NULL,* ifmt_ctx_a=NULL;
    int ret;
    if ((ret = avformat_open_input(&ifmt_ctx_v, in_filename_v, 0, 0)) < 0) {
        printf("Could not open input file.");

    }

    avformat_find_stream_info(ifmt_ctx_v, 0);

    avformat_open_input(&ifmt_ctx_a, in_filename_a, 0, 0);
    avformat_find_stream_info(ifmt_ctx_a, 0);


    AVOutputFormat* fmt = av_guess_format(NULL, out_file, NULL);
    if (!fmt) {
        fprintf(stderr, "Could not find suitable output format");
        exit(1);
    }
    AVFormatContext* ofmt_ctx = avformat_alloc_context();
    ofmt_ctx->oformat = fmt;

    AVStream *ist_v=NULL,*ist_a=NULL;

    AVStream *ost_v=NULL, *ost_a=NULL;

    for (int i = 0; i < ifmt_ctx_v->nb_streams; i++) {
        if (ifmt_ctx_v->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            ist_v = ifmt_ctx_v->streams[i];

            //Create output AVStream according to input AVStream  
            ost_v = avformat_new_stream(ofmt_ctx, ist_v->codec->codec);
            //Create output AVCodecContext according to input AVCodecContext  
            avcodec_copy_context(ost_v->codec, ist_v->codec);

            if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
                ost_v->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
            break;
        }
    }
    for (int i = 0; i < ifmt_ctx_a->nb_streams; i++) {
        if (ifmt_ctx_a->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){
            ist_a = ifmt_ctx_a->streams[i];

            //Create output AVStream according to input AVStream  
            ost_a = avformat_new_stream(ofmt_ctx, ist_a->codec->codec);
            //Create output AVCodecContext according to input AVCodecContext  
            avcodec_copy_context(ost_a->codec, ist_a->codec);

            if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
                ost_a->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
            break;
        }
    }
    AVCodecContext *ic_ctx_v = ist_v->codec;
    AVCodecContext *ic_ctx_a = ist_a->codec;


    AVCodecContext *oc_ctx_v = ost_v->codec;
    AVCodecContext *oc_ctx_a = ost_a->codec;


    /* open the output file, if needed */
    if (!(fmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_file, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "Could not open '%s': %s\n", out_file,"");
            return 1;
        }
    }

    ret = avformat_write_header(ofmt_ctx, NULL);
    int enc_video = 1, enc_audio = 1;
    while (enc_video&&enc_audio){

        if (av_compare_ts(cur_pts_v, ost_v->time_base, cur_pts_a, ost_a->time_base) <= 0){
            if (enc_video){
                AVPacket pkt;
                init_packet(&pkt);
                enc_video = (av_read_frame(ifmt_ctx_v, &pkt) >= 0); //此时的pts是基于AVStream的时间基的
                if (enc_video){

                    if (pkt.pts == AV_NOPTS_VALUE){
                        //之前获取每帧多少秒,都是通过AVcodecContext的time_base ,用于这个值是我们自己设置的
                        //还有一种获取帧率的方法,取AVStream的r_frame_rate,注意这个值要再取倒数才是每帧多少秒
                        double duration_s = 1/av_q2d(ist_v->r_frame_rate);//每帧多少秒
                        double duration = duration_s / av_q2d(ist_v->time_base); 
                        pkt.pts = framecount*duration;
                        pkt.dts = pkt.pts;
                        pkt.duration = duration;//这里duration必须设置,否则得到的视频总时长不对
                        framecount++;
                    }

                    pkt.pts = av_rescale_q(pkt.pts, ist_v->time_base, ost_v->time_base);
                    pkt.dts = pkt.pts;
                    pkt.duration = av_rescale_q(pkt.duration, ist_v->time_base, ost_v->time_base);
                    pkt.stream_index = ost_v->index;

                    cur_pts_v = pkt.pts;

                    av_interleaved_write_frame(ofmt_ctx, &pkt);

                }
                av_free_packet(&pkt);
            }
        }
        else{
            if (enc_audio){
                AVPacket pkt;
                init_packet(&pkt);
                enc_audio = (av_read_frame(ifmt_ctx_a, &pkt) >= 0);
                if (enc_audio){

                    if (pkt.pts == AV_NOPTS_VALUE){
                        double duration_s = 1 / av_q2d(ist_v->r_frame_rate);//每帧多少秒
                        double duration = duration_s / av_q2d(ist_v->time_base);
                        pkt.pts = framecount*duration;
                        pkt.dts = pkt.pts;
                        pkt.duration = duration;
                        framecount++;
                    }
                    pkt.pts = av_rescale_q(pkt.pts, ist_a->time_base, ost_a->time_base);
                    pkt.dts = pkt.pts;
                    pkt.duration = av_rescale_q(pkt.duration, ist_a->time_base, ost_a->time_base);
                    pkt.stream_index = ost_a->index;
                    cur_pts_a=pkt.pts;
                    av_interleaved_write_frame(ofmt_ctx, &pkt);
                }
                av_free_packet(&pkt);
            }
        }
    }
    av_write_trailer(ofmt_ctx);
    avformat_close_input(&ifmt_ctx_v);
    avformat_close_input(&ifmt_ctx_a);
    if (ofmt_ctx) {
        avio_closep(&ofmt_ctx->pb);
        avformat_free_context(ofmt_ctx);
    }
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值