FFmpeg合成h264视频流和aac音频流为mp4

#include "MyMuxerToMP4.h"

#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>


/*
 FIX: H.264 in some container format (FLV, MP4, MKV etc.) need
 "h264_mp4toannexb" bitstream filter (BSF)
 *Add SPS,PPS in front of IDR frame
 *Add start code ("0,0,0,1") in front of NALU
 H.264 in some container (MPEG2TS) don't need this BSF.
 */
//'1': Use H.264 Bitstream Filter
#define USE_H264BSF 1

/*
 FIX:AAC in some container format (FLV, MP4, MKV etc.) need
 "aac_adtstoasc" bitstream filter (BSF)
 */
//'1': Use AAC Bitstream Filter
#define USE_AACBSF 1


//
//static int open_input_file(const char *filename)
//{
//    int ret;
//    AVCodec *dec;
//    
//    if ((ret = avformat_open_input(&pFormatCtx, filename, NULL, NULL)) < 0) {
//        printf( "Cannot open input file\n");
//        return ret;
//    }
//    
//    if ((ret = avformat_find_stream_info(pFormatCtx, NULL)) < 0) {
//        printf( "Cannot find stream information\n");
//        return ret;
//    }
//    
//    /* select the video stream */
//    ret = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
//    if (ret < 0) {
//        printf( "Cannot find a video stream in the input file\n");
//        return ret;
//    }
//    video_stream_index = ret;
//    pCodecCtx = pFormatCtx->streams[video_stream_index]->codec;
//    
//    /* init the video decoder */
//    if ((ret = avcodec_open2(pCodecCtx, dec, NULL)) < 0) {
//        printf( "Cannot open video decoder\n");
//        return ret;
//    }
//    
//    return 0;
//}

int muxer_main(char *inputH264FileName,char *inputAacFileName,char *outMP4FileName)
{
    AVOutputFormat *ofmt =NULL;
    //Input AVFormatContext and Output AVFormatContext
    AVFormatContext *ifmt_ctx_v =NULL, *ifmt_ctx_a =NULL,*ofmt_ctx = NULL;
    AVPacket pkt;
    AVCodec *dec;
   int ret, i;
   int videoindex_v=-1,videoindex_out=-1;
   int audioindex_a=-1,audioindex_out=-1;
   int frame_index=0;
    int64_t cur_pts_v=0,cur_pts_a=0;
    
    //const char *in_filename_v = "cuc_ieschool.ts";//Input file URL
   constchar *in_filename_v =inputH264FileName;
    //const char *in_filename_a = "cuc_ieschool.mp3";
    //const char *in_filename_a = "gowest.m4a";
    //const char *in_filename_a = "gowest.aac";
   constchar *in_filename_a =inputAacFileName;
    
   constchar *out_filename =outMP4FileName;//Output file URL
    
    printf("==========in h264==filename:%s\n",in_filename_v);
    printf("==========in aac ===filename:%s\n",in_filename_a);
    printf("==========outfile:%s\n",out_filename);
    avcodec_register_all();
    av_register_all();
    //Input
   if ((ret = avformat_open_input(&ifmt_ctx_a, in_filename_a,NULL,NULL)) <0) {
//        printf("=====11========RET:%d\n",ret);
        printf( "Could not open input file.");
       goto end;
    }
//    printf("=====2========RET:%d\n",ret);
   if ((ret = avformat_find_stream_info(ifmt_ctx_a,0)) <0) {
        printf( "Failed to retrieve input stream information");
       goto end;
    }
    
    
   if ((ret = avformat_open_input(&ifmt_ctx_v, in_filename_v,NULL,NULL)) <0) {
        printf( "Could not open input file:%d\n",ret);
       goto end;
    }
//    printf("=====0========RET:%d\n",ret);
   if ((ret = avformat_find_stream_info(ifmt_ctx_v,0)) <0) {
        printf( "Failed to retrieve input stream information");
       goto end;
    }
    
//    /* init the video decoder */
//    if ((ret = avcodec_open2(ifmt_ctx_a->, dec, NULL)) < 0) {
//        printf( "Cannot open video decoder\n");
//        return ret;
//    }
//    

   
    printf("===========Input Information==========\n");
    av_dump_format(ifmt_ctx_v,0, in_filename_v,0);
    av_dump_format(ifmt_ctx_a,0, in_filename_a,0);
    printf("======================================\n");
    //Output
    avformat_alloc_output_context2(&ofmt_ctx,NULL,NULL, out_filename);
   if (!ofmt_ctx) {
        printf( "Could not create output context\n");
        ret = AVERROR_UNKNOWN;
       goto end;
    }
    ofmt = ofmt_ctx->oformat;
    
   for (i =0; i < ifmt_ctx_v->nb_streams; i++) {
        //Create output AVStream according to input AVStream
       if(ifmt_ctx_v->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
            AVStream *in_stream = ifmt_ctx_v->streams[i];
            AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
            videoindex_v=i;
           if (!out_stream) {
                printf( "Failed allocating output stream\n");
             
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了悟生死大事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值