将音频流解码为pcm(with avformat)

#include "stdafx.h"  

extern "C"
{
#include "libavformat\avformat.h"
};

int main(int argc, char* argv[])
{
	AVFormatContext *fmt_ctx;
	AVStream *stream;
	AVCodecContext *codec_ctx;
	AVCodec *codec;
	AVPacket pkt;
	AVFrame *frame = NULL;
	int ret, stream_index, got_frame;
	FILE *infile = NULL, *outfile = NULL;
	
	const char *infilename = "上和下.aac";
	const char *outfilename = "aac2pcm.pcm";

	av_register_all();
	avformat_network_init();

	fmt_ctx = avformat_alloc_context();
	if ((ret = avformat_open_input(&fmt_ctx, infilename, NULL, NULL)) < 0){
		printf("Couldn't open input stream.\n");
		return ret;
	}
	if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0){
		printf("Couldn't find stream information.\n");
		return ret;
	}
	ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
	if (ret < 0) {
		fprintf(stderr, "Could not find %s stream in input file '%s'\n",
			av_get_media_type_string(AVMEDIA_TYPE_AUDIO), infilename);
		return ret;
	}
	else {
		stream_index = ret;
		stream = fmt_ctx->streams[stream_index];

		/* find decoder for the stream */
		codec_ctx = stream->codec;
		codec = avcodec_find_decoder(codec_ctx->codec_id);
		if (!codec) {
			fprintf(stderr, "Failed to find %s codec\n",
				av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
			goto end;
			return AVERROR(EINVAL);
		}

		if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
			fprintf(stderr, "Failed to open %s codec\n",
				av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
			goto end;
			return ret;
		}
	}
	
	printf("Decode audio file %s to %s\n", infilename, outfilename);
        infile = fopen(infilename, "rb");
	if (!infile) {
		fprintf(stderr, "Could not open %s\n", infilename);
		goto end;
		exit(1);
	}
	outfile = fopen(outfilename, "wb");
	if (!outfile) {
		goto end;
		exit(1);
	}
	
	av_init_packet(&pkt);
	while (av_read_frame(fmt_ctx, &pkt) >= 0){

		if (pkt.stream_index == stream_index){
			if (!frame) {
				if (!(frame = av_frame_alloc())) {
					fprintf(stderr, "Could not allocate audio frame\n");
					goto end;
					exit(1);
				}
			}

			ret = avcodec_decode_audio4(codec_ctx, frame, &got_frame, &pkt);
			if (ret < 0) {
				printf("Error in decoding audio frame.\n");
				goto end;
				return -1;
			}
			if (got_frame > 0){
				/* if a frame has been decoded, output it */
				int data_size = av_get_bytes_per_sample(codec_ctx->sample_fmt);
				if (data_size < 0) {
					/* This should not occur, checking just for paranoia */
					fprintf(stderr, "Failed to calculate data size\n");
					goto end;
					exit(1);
				}
				for (int i = 0; i < frame->nb_samples; i++)
					for (int ch = 0; ch < codec_ctx->channels; ch++)
						fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
			}
		}
		av_free_packet(&pkt);
	}

end:
	if (outfile)
		fclose(outfile);
	if (infile)
		fclose(infile);
	avcodec_close(codec_ctx);
	avformat_close_input(&fmt_ctx);
	if (frame)
		av_frame_free(&frame);
	
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要把音频解码后的AVFrame保存为PCM格式的音频文件,你可以使用FFmpeg提供的API进行处理。具体步骤如下: 1. 打开音频文件并获取音频信息: ``` AVFormatContext *formatCtx = avformat_alloc_context(); if (avformat_open_input(&formatCtx, inputPath, NULL, NULL) != 0) { // 打开音频文件失败 return; } if (avformat_find_stream_info(formatCtx, NULL) < 0) { // 获取音频信息失败 return; } int audioStreamIndex = -1; for (int i = 0; i < formatCtx->nb_streams; i++) { if (formatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audioStreamIndex = i; break; } } if (audioStreamIndex == -1) { // 找不到音频 return; } AVCodecParameters *audioCodecParams = formatCtx->streams[audioStreamIndex]->codecpar; ``` 2. 获取音频解码器并打开解码器: ``` AVCodec *audioCodec = avcodec_find_decoder(audioCodecParams->codec_id); AVCodecContext *audioCodecCtx = avcodec_alloc_context3(audioCodec); avcodec_parameters_to_context(audioCodecCtx, audioCodecParams); if (avcodec_open2(audioCodecCtx, audioCodec, NULL) < 0) { // 打开音频解码器失败 return; } ``` 3. 循环读取音频帧并解码: ``` AVPacket pkt; av_init_packet(&pkt); AVFrame *frame = av_frame_alloc(); while (av_read_frame(formatCtx, &pkt) >= 0) { if (pkt.stream_index == audioStreamIndex) { int ret = avcodec_send_packet(audioCodecCtx, &pkt); if (ret < 0) { // 发送音频数据包到解码器失败 break; } while (ret >= 0) { ret = avcodec_receive_frame(audioCodecCtx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { // 无法从解码器中接收更多音频帧 break; } else if (ret < 0) { // 解码音频帧失败 break; } // 将音频帧保存为PCM格式的文件 // 这里省略将AVFrame保存为PCM格式文件的代码 } } av_packet_unref(&pkt); } av_frame_free(&frame); ``` 4. 关闭解码器和音频文件: ``` avcodec_free_context(&audioCodecCtx); avformat_close_input(&formatCtx); ``` 在将AVFrame保存为PCM格式文件时,你需要将AVFrame中的音频数据读取出来,并将其保存为PCM格式的文件。具体的方法可以参考FFmpeg的文档和示例代码。另外,需要注意的是,在将音频帧保存为PCM格式的文件时,你还需要指定音频的采样率、采样格式、声道布局等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值