本文记录一个基于FFmpeg的视音频复用器(Simplest FFmpeg muxer)。视音频复用器(Muxer)即是将视频压缩数据(例如H.264)和音频压缩数据(例如AAC)合并到一个封装格式数据(例如MKV)中去。如图所示。在这个过程中并不涉及到编码和解码。
-
/**
-
* 最简单的基于FFmpeg的视音频复用器
-
* Simplest FFmpeg Muxer
-
*
-
* 雷霄骅 Lei Xiaohua
-
* leixiaohua1020@126.com
-
* 中国传媒大学/数字电视技术
-
* Communication University of China / Digital TV Technology
-
* http://blog.csdn.net/leixiaohua1020
-
*
-
* 本程序可以将视频码流和音频码流打包到一种封装格式中。
-
* 程序中将MP3编码的音频码流和H.264编码(MPEG2TS封装中)的视频码流打包成
-
* MP4封装格式的文件。
-
* 需要注意的是本程序并不改变视音频的编码格式。
-
*
-
* This software mux a video bitstream and a audio bitstream
-
* together into a file.
-
* In this example, it mux a H.264 bitstream (in MPEG2TS) and
-
* a MP3 bitstream file together into MP4 format file.
-
*
-
*/
-
-
#include
-
-
extern "C"
-
{
-
#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 0
-
-
/*
-
FIX:AAC in some container format (FLV, MP4, MKV etc.) need
-
"aac_adtstoasc" bitstream filter (BSF)
-
*/
-
//'1': Use AAC Bitstream Filter
-
#define USE_AACBSF 0
-
-
-
-
int main(int argc, char* argv[])
-
{
-
AVOutputFormat *ofmt = NULL;
-
//输入对应一个AVFormatContext,输出对应一个AVFormatContext
-
//(Input AVFormatContext and Output AVFormatContext)
-
AVFormatContext *ifmt_ctx_v = NULL, *ifmt_ctx_a = NULL,*ofmt_ctx = NULL;
-
AVPacket pkt;
-
int ret, i;
-
-
char *in_filename_v = "cuc_ieschool.ts";//输入文件名(Input file URL)
-
//char *in_filename_v = "cuc_ieschool.h264";
-
//char *in_filename_a = "cuc_ieschool.mp3";
-
//char *in_filename_a = "gowest.m4a";
-
//char *in_filename_a = "gowest.aac";
-
char *in_filename_a = "huoyuanjia.mp3";
-
-
char *out_filename = "cuc_ieschool.mp4";//输出文件名(Output file URL)
-
av_register_all();
-
//输入(Input)
-
if ((ret = avformat_open_input(&ifmt_ctx_v, in_filename_v, 0, 0)) < 0) {
-
printf( "Could not open input file.");
-
goto end;
-
}
-
if ((ret = avformat_find_stream_info(ifmt_ctx_