使用FFmpeg库(版本号为:4.4.2-0ubuntu0.22.04.1)把AAC文件和H264文件合成MP4/FLV文件。
需要aac和h264测试文件的,可以从我上传的MP4文件中用ffmpeg提取,命令如下:
ffmpeg -i <input.mp4> -map 0:v -c:v copy <output.h264> -map 0:a -c:a copy <output.aac>
代码如下:
#include <stdio.h>
#include "libavformat/avformat.h"
int open_input_file(const char *filename, AVFormatContext **ifmt_ctx);
int create_output_stream(AVFormatContext *ofmt_ctx, AVFormatContext *ifmt_ctx, enum AVMediaType type,
int *index_in, int *index_out);
int rw_frame(AVFormatContext *ifmt_ctx, AVFormatContext *ofmt_ctx,
int stream_index_in, int stream_index_out, int *frame_index, int64_t *cur_pts);
int main(int argc, char *argv[])
{
int ret = -1;
const char *in_filename_v = argv[1];
const char *in_filename_a = argv[2];
const char *out_filename = argv[3];
int videoindex_in = -1, videoindex_out = -1;
int audioindex_in = -1, audioindex_out = -1;
int frame_index = 0;
int64_t cur_pts_v = 0, cur_pts_a = 0;
int writing_v = 1, writing_a = 1;
const AVOutputFormat *ofmt = NULL;
AVFormatContext *ifmt_ctx_v = NULL, *ifmt_ctx_a = NULL, *ofmt_ctx = NULL;
if (argc < 4)
{
printf("Usage: %s <h264 filename> <aac filename> <output filename>\n", argv[0]);
return -1;
}
// 打印ffmpeg版本信息
printf("ffmpeg version: %s\n", av_version_info());
// 打开视频输入文件
if (open_input_file(in_filename_v, &ifmt_ctx_v) < 0)
{
goto end;
}
// 打开音频输入文件
if (open_input_file(in_filename_a, &ifmt_ctx_a) < 0)
{
goto end;
}
// 分配输出上下文
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx)
{
printf("avformat_alloc_output_context2 failed\n");
goto end;
}
ofmt = ofmt_ctx->oformat;
// 创建视频输出流
if (create_output_stream(ofmt_ctx, ifmt_ctx_v, AVMEDIA_TYPE_VIDEO, &videoindex_in, &videoindex_out) < 0)
goto end;
// 创建音频输出流
if (create_output_stream(ofmt_ctx, ifmt_ctx_a, AVMEDIA_TYPE_AUDIO, &audioindex_in, &audioindex_out) < 0)
goto end;
// 打开输出文件
if (!(ofmt->flags & AVFMT_NOFILE))
{
if (avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE))
{
printf("open %s file failed\n", out_filename);
goto end;
}
}
// 写入文件头
if (avformat_write_header(ofmt_ctx, NULL) < 0)
{
printf("avformat_write_header failed\n");
goto end;
}
// 循环写入视频和音频帧
while (writing_v || writing_a)
{
// 如果还在写视频帧,且(不写音频帧或者视频帧的PTS小于等于音频帧的PTS)
if (writing_v &&
(!writing_a ||
av_compare_ts(cur_pts_v, ifmt_ctx_v->streams[videoindex_in]->time_base, cur_pts_a,
ifmt_ctx_a->streams[audioindex_in]->time_base) <= 0))
{
// 读取并写入视频帧
ret = rw_frame(ifmt_ctx_v, ofmt_ctx, videoindex_in,
videoindex_out, &frame_index, &cur_pts_v);
if (ret == -2)
{
writing_v = 0;
}
else if (ret < 0)
{
goto end;
}
}
else
{ // 读取并写入音频帧
ret = rw_frame(ifmt_ctx_a, ofmt_ctx, audioindex_in,
audioindex_out, &frame_index, &cur_pts_a);
if (ret == -2)
{
writing_a = 0;
}
else if (ret < 0)
{
goto end;
}
}
}
// 写入文件尾
av_write_trailer(ofmt_ctx);
end:
if (ifmt_ctx_v)
avformat_close_input(&ifmt_ctx_v);
if (ifmt_ctx_a)
avformat_close_input(&ifmt_ctx_a);
if (ofmt && !(ofmt->flags & AVFMT_NOFILE))
avio_close(ofmt_ctx->pb);
if (ofmt_ctx)
avformat_free_context(ofmt_ctx);
return 0;
}
// 打开输入文件并查找流信息
int open_input_file(const char *filename, AVFormatContext **ifmt_ctx)
{
// 打开输入文件
if (avformat_open_input(ifmt_ctx, filename, 0, 0) < 0)
{
printf("open %s file failed\n", filename);
return -1;
}
// 查找流信息
if (avformat_find_stream_info(*ifmt_ctx, 0) < 0)
{
printf("avformat_find_stream_info failed\n");
return -1;
}
return 0;
}
// 创建输出流
int create_output_stream(AVFormatContext *ofmt_ctx, AVFormatContext *ifmt_ctx, enum AVMediaType type,
int *index_in, int *index_out)
{
for (unsigned int i = 0; i < ifmt_ctx->nb_streams; i++)
{
// 查找指定类型的流
if (ifmt_ctx->streams[i]->codecpar->codec_type == type)
{
AVStream *out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream)
{
printf("avformat_new_stream failed\n");
return -1;
}
*index_in = i;
*index_out = out_stream->index;
// 复制输入流的编码参数到输出流
if (avcodec_parameters_copy(out_stream->codecpar, ifmt_ctx->streams[i]->codecpar) < 0)
{
printf("avcodec_parameters_copy failed\n");
return -1;
}
return 0;
}
}
return -1;
}
// 读取并写入帧
int rw_frame(AVFormatContext *ifmt_ctx, AVFormatContext *ofmt_ctx,
int stream_index_in, int stream_index_out, int *frame_index, int64_t *cur_pts)
{
int ret;
AVPacket pkt;
AVStream *in_stream, *out_stream;
// 读取帧
if ((ret = av_read_frame(ifmt_ctx, &pkt)) < 0)
{
if (ret == AVERROR_EOF) // 读到文件尾
{
return -2;
}
else
{
printf("av_read_frame failed\n");
return -1;
}
}
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[stream_index_out];
// 处理指定的流
if (pkt.stream_index == stream_index_in)
{
// 如果PTS值无效,计算并设置PTS和DTS
if (pkt.pts == AV_NOPTS_VALUE)
{
AVRational time_base = in_stream->time_base;
int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(in_stream->r_frame_rate);
pkt.pts = (double)(*frame_index * calc_duration) / (double)(av_q2d(time_base) * AV_TIME_BASE);
pkt.dts = pkt.pts;
pkt.duration = (double)calc_duration / (double)(av_q2d(time_base) * AV_TIME_BASE);
(*frame_index)++;
}
*cur_pts = pkt.pts;
// 转换PTS和DTS
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
(enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
(enum AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
pkt.stream_index = stream_index_out;
// if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
// {
// printf("Write 1 video Packet. size:%d pts:%ld\n", pkt.size, pkt.pts);
// }
// else if (in_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
// {
// printf("Write 1 audio Packet. size:%d pts:%ld\n", pkt.size, pkt.pts);
// }
// 写入帧
if ((ret = av_interleaved_write_frame(ofmt_ctx, &pkt)) < 0)
{
printf("av_interleaved_write_frame failed\n");
av_packet_unref(&pkt);
return -1;
}
}
av_packet_unref(&pkt);
return 0;
}
参考博客链接:FFMPEG库实现mp4/flv文件(H264+AAC)的封装与分离_ffmpeg mp4 flv-CSDN博客