本文将使用FFmpeg从mp4/aac/mp3等包含音频流的多媒体文件中提取PCM数据保存到本地。
一、使用命令行提取
ffmpeg -i input.mp4 -ar 44100 -ac 2 -f s16le output.pcm
就这样一条命令搞定,非常简单。
二、使用代码编程提取
主要步骤如下:
1、使用avformat_open_input
函数打开输入文件获取AVFormatContext上下文ifmt_ctx;
2、使用avformat_find_stream_info
函数查找流信息;
3、使用av_find_best_stream
函数得到音频流的索引a_stream_index和解码器a_dec;
4、通过上面得到的a_dec使用avcodec_alloc_context3
函数初始化解码器,得到解码器上下文AVCodecContext a_dec_ctx;
5、通过avcodec_parameters_to_context
函数把音频流的参数信息赋值给a_dec_ctx;
6、使用avcodec_open2
函数打开编码器;
7、使用av_read_frame
函数从ifmt_ctx上下文中读取数据包;
8、使用avcodec_send_packet
、avcodec_receive_frame
函数解码,得到原始数据AVFrame,这就是咱们需要的PCM数据,再通过SwrContext转换成我们需要的格式,保存到本地就行了。
代码如下:
初始化输入输出文件的路径
self.inputPath = [[NSBundle mainBundle] pathForResource:@"bb1.mp4" ofType:nil];
self.outputPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"bb1_44100_2_s16le.pcm"];
NSLog(@"%@", self.outputPath);
[NSFileManager.defaultManager removeItemAtPath:self.outputPath error:nil];
[[NSFileManager defaultManager] createFileAtPath:self.outputPath contents:nil attributes:nil];
self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.outputPath];
解码多媒体文件并保存到本地
AVFormatContext *ifmt_ctx = NULL;
int a_stream_index = -1;
AVCodec *a_dec = NULL;
AVCodecContext *a_dec_ctx = NULL;
AVPacket *pkt;
AVFrame *frame;
SwrContext *swr_ctx;
int ret;
ret = avformat_open_input(&ifmt_ctx, self.inputPath.UTF8String, NULL, NULL);
if (ret<0) {
NSLog(@"avformat_open_input出错了");
return;
}
avformat_find_stream_info(ifmt_ctx, NULL);
a_stream_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &a_dec, 0);
if (a_stream_index<0) {
NSLog(@"没有音频流");
goto __FAIL;
}
a_dec_ctx = avcodec_alloc_context3(a_dec);
a_dec_ctx->pkt_timebase = ifmt_ctx->streams[a_stream_index]->time_base;
avcodec_parameters_to_context(a_dec_ctx, ifmt_ctx->streams[a_stream_index]->codecpar);
if (!a_dec_ctx) {
NSLog(@"avcodec_alloc_context3出错");
goto __FAIL;
}
ret = avcodec_open2(a_dec_ctx, a_dec, NULL);
if (ret<0) {
NSLog(@"avcodec_open2出错");
goto __FAIL;
}
frame = av_frame_alloc();
pkt = av_packet_alloc();
int64_t out_ch_layout = AV_CH_LAYOUT_STEREO;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
int out_sample_rate = 44100;
swr_ctx = swr_alloc_set_opts(NULL, out_ch_layout, out_sample_fmt, out_sample_rate, a_dec_ctx->channel_layout, a_dec_ctx->sample_fmt, a_dec_ctx->sample_rate, 0, NULL);
ret = swr_init(swr_ctx);
uint8_t *out_buffer = (uint8_t *)av_malloc(44100*2);
while (1) {
ret = av_read_frame(ifmt_ctx, pkt);
if (ret<0) {
break;
}
if (pkt->stream_index!=a_stream_index) {
NSLog(@"%d", pkt->size);
continue;
}
ret = avcodec_send_packet(a_dec_ctx, pkt);
if (ret<0) {
NSLog(@"avcodec_send_packet出错");
break;
}
while (1) {
ret = avcodec_receive_frame(a_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
swr_convert(swr_ctx, &out_buffer, 44100*2, frame->data, frame->nb_samples);
int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);
NSData *outBufferData = [NSData dataWithBytes:out_buffer length:out_buffer_size];
[self.fileHandle writeData:outBufferData error:nil];
}
}
while (1) {
pkt->data = NULL;
pkt->size = 0;
ret = avcodec_send_packet(a_dec_ctx, pkt);
if (ret<0) {
NSLog(@"avcodec_send_packet出错");
break;
}
while (1) {
ret = avcodec_receive_frame(a_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
swr_convert(swr_ctx, &out_buffer, 44100*2, frame->data, frame->nb_samples);
int out_buffer_size = av_samples_get_buffer_size(NULL, av_get_channel_layout_nb_channels(out_ch_layout), frame->nb_samples, out_sample_fmt, 0);
NSData *outBufferData = [NSData dataWithBytes:out_buffer length:out_buffer_size];
[self.fileHandle writeData:outBufferData error:nil];
}
}
__FAIL:
if (ifmt_ctx) {
avformat_close_input(&ifmt_ctx);
avformat_free_context(ifmt_ctx);
}
if (out_buffer) {
av_free(out_buffer);
}
if (frame) {
av_frame_free(&frame);
}
if (pkt) {
av_packet_free(&pkt);
}
if (swr_ctx) {
swr_free(&swr_ctx);
}
if (a_dec_ctx) {
avcodec_close(a_dec_ctx);
avcodec_free_context(&a_dec_ctx);
}
这样咱们就把多媒体文件中的音频流取出来保存到本地了,通过下面命令就可以播放了
ffplay -f s16le -ar 44100 -ac 2 output.pcm
-f
指定pcm的存储格式
-ar
指定采样率
-ac
指定通道数