1.程序代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#define ERROR_STR_SIZE 1024
int main(int argc, char **argv)
{
AVFormatContext *fmt_ctx = NULL;
AVFormatContext *ofmt_ctx = NULL;
AVOutputFormat *output_fmt = NULL;
AVStream *in_stream = NULL;
AVStream *out_stream = NULL;
void *in_codecpar = NULL;
AVPacket pkt;
char *src_filename = NULL;
char *dst_filename = NULL;
char errors[ERROR_STR_SIZE];
int audio_stream_index;
int err_code;
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
if(argc < 3)
{
av_log(NULL, AV_LOG_DEBUG, "argc < 3!\n");
return -1;
}
src_filename = argv[1];
dst_filename = argv[2];
if(src_filename == NULL || dst_filename == NULL)
{
av_log(NULL, AV_LOG_DEBUG, "src or dts file is null!\n");
return -1;
}
if((err_code=avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0)
{
av_strerror(err_code, errors, 1024);
av_log(NULL, AV_LOG_DEBUG, "打开输入文件失败: %s, %d(%s)", src_filename, err_code, errors);
return -1;
}
av_dump_format(fmt_ctx, 0, src_filename, 0);
if(fmt_ctx->nb_streams < 2)
{
av_log(NULL, AV_LOG_DEBUG, "输入文件错误, 流不足2条\n");
return AVERROR(EINVAL);
}
//拿到文件中音频流
/**只需要修改这里AVMEDIA_TYPE_VIDEO参数**/
audio_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if(audio_stream_index < 0)
{
av_log(NULL, AV_LOG_DEBUG, "获取音频流失败, %s,%s\n", av_get_media_type_string(AVMEDIA_TYPE_VIDEO), src_filename);
return AVERROR(EINVAL);
}
printf("audio_stream_index=[%d]\n", audio_stream_index);
in_stream = fmt_ctx->streams[audio_stream_index];
in_codecpar = (void *)(in_stream->codec);
//输出上下文
ofmt_ctx = avformat_alloc_context();
//根据目标文件名生成最适合的输出容器
output_fmt = av_guess_format(NULL, dst_filename, NULL);
if(!output_fmt)
{
av_log(NULL, AV_LOG_DEBUG, "根据目标生成输出容器失败!\n");
return -1;
}
ofmt_ctx->oformat = output_fmt;
//printf("%s\n", output_fmt);
//新建输出流
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if(!out_stream)
{
av_log(NULL, AV_LOG_DEBUG, "创建输出流失败!\n");
return -1;
}
//将参数信息拷贝到输出流中,我们只是抽取音频流,并不做音频处理,所以这里只是Copy
if((err_code = avcodec_copy_context(out_stream->codec, in_codecpar)) < 0)
{
av_strerror(err_code, errors, ERROR_STR_SIZE);
av_log(NULL, AV_LOG_ERROR, "拷贝编码参数失败!, %d(%s)\n", err_code, errors);
}
out_stream->codec->codec_tag = 0;
//初始化AVIOContext,文件操作由它完成
if((err_code = avio_open(&ofmt_ctx->pb, dst_filename, AVIO_FLAG_WRITE)) < 0)
{
av_strerror(err_code, errors, 1024);
av_log(NULL, AV_LOG_DEBUG, "文件打开失败 %s, %d(%s)\n", dst_filename, err_code, errors);
return -1;
}
av_dump_format(ofmt_ctx, 0, dst_filename, 1);
//初始化 AVPacket, 我们从文件中读出的数据会暂存在其中
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
//写头部信息
if(avformat_write_header(ofmt_ctx, NULL) < 0)
{
av_log(NULL, AV_LOG_DEBUG, "写入头部信息失败!");
return -1;
}
//每读出一帧数据
while(av_read_frame(fmt_ctx, &pkt) >= 0)
{
if(pkt.stream_index == audio_stream_index)
{
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
(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 = 0;
//将包写到输出媒体文件
av_interleaved_write_frame(ofmt_ctx, &pkt);
//减少引用计数,避免内存泄漏
av_packet_unref(&pkt);
}
}
//写尾部信息
av_write_trailer(ofmt_ctx);
//最后别忘了释放内存
avformat_close_input(&fmt_ctx);
avio_close(ofmt_ctx->pb);
return 0;
}
2.编译程序
$ gcc -o ffm_audio ffm_audio.c -I$HOME/local/include -lavformat
3.运行程序
$ ./ffm_audio test.mp4 test.aac
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8dc0020] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x8dc0020] ISO: File Type Major Brand: isom
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.71.100
Duration: 00:00:22.11, bitrate: N/A
Stream #0:0(und), 0, 1/90000: Video: h264, 1 reference frame (avc1 / 0x31637661), none, 720x1280 (0x0), 0/1, 446 kb/s, SAR 1:1 DAR 9:16, 30 fps, 30 tbr, 90k tbn (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und), 0, 1/44100: Audio: aac (mp4a / 0x6134706D), 44100 Hz, 2 channels, 127 kb/s (default)
Metadata:
handler_name : SoundHandler
audio_stream_index=[1]
Output #0, adts, to 'test.aac':
Stream #0:0, 0, 0/0: Audio: aac, 44100 Hz, 2 channels, 127 kb/s
[AVIOContext @ 0x8dc8740] Statistics: 1663412 bytes read, 2 seeks
[AVIOContext @ 0x8dd51e0] Statistics: 0 seeks, 952 writeouts