版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013699869/article/details/50470003
#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;
}