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

  • Post author:
  • Post category:其他







#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");
             



版权声明:本文为Nil88原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。