1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <stdio.h>
#include "libavcodec/avcodec.h" #include "libavfilter/avfilter.h" #include "libavformat/avformat.h" #include "libavutil/avutil.h" #include "libavutil/ffversion.h" #include "libswresample/swresample.h" #include "libswscale/swscale.h" #include "libpostproc/postprocess.h"
#define MAX_AUDIO_FRAME_SIZE 192000
int main() { const char inFileName[] = "/home/jackey/Music/test.mp3"; const char outFileName[] = "test.pcm"; FILE *file=fopen(outFileName,"w+b"); if(!file){ printf("Cannot open output file.\n"); return -1; }
AVFormatContext *fmtCtx =avformat_alloc_context(); AVCodecContext *codecCtx = NULL; AVPacket *pkt=av_packet_alloc(); AVFrame *frame = av_frame_alloc();
int aStreamIndex = -1;
do{ if(avformat_open_input(&fmtCtx,inFileName,NULL,NULL)<0){ printf("Cannot open input file.\n"); break; } if(avformat_find_stream_info(fmtCtx,NULL)<0){ printf("Cannot find any stream in file.\n"); break; }
av_dump_format(fmtCtx,0,inFileName,0);
for(size_t i=0;i<fmtCtx->nb_streams;i++){ if(fmtCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){ aStreamIndex=(int)i; break; } } if(aStreamIndex==-1){ printf("Cannot find audio stream.\n"); break; }
AVCodecParameters *aCodecPara = fmtCtx->streams[aStreamIndex]->codecpar; AVCodec *codec = avcodec_find_decoder(aCodecPara->codec_id); if(!codec){ printf("Cannot find any codec for audio.\n"); break; } codecCtx = avcodec_alloc_context3(codec); if(avcodec_parameters_to_context(codecCtx,aCodecPara)<0){ printf("Cannot alloc codec context.\n"); break; } codecCtx->pkt_timebase=fmtCtx->streams[aStreamIndex]->time_base;
if(avcodec_open2(codecCtx,codec,NULL)<0){ printf("Cannot open audio codec.\n"); break; }
uint64_t out_channel_layout = codecCtx->channel_layout; enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16; int out_sample_rate = codecCtx->sample_rate; int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);
uint8_t *audio_out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE*2);
SwrContext *swr_ctx = swr_alloc_set_opts(NULL, out_channel_layout, out_sample_fmt, out_sample_rate, codecCtx->channel_layout, codecCtx->sample_fmt, codecCtx->sample_rate, 0,NULL); swr_init(swr_ctx);
while(av_read_frame(fmtCtx,pkt)>=0){ if(pkt->stream_index==aStreamIndex){ if(avcodec_send_packet(codecCtx,pkt)>=0){ while(avcodec_receive_frame(codecCtx,frame)>=0){
if(av_sample_fmt_is_planar(codecCtx->sample_fmt)){ int len = swr_convert(swr_ctx, &audio_out_buffer, MAX_AUDIO_FRAME_SIZE*2, (const uint8_t**)frame->data, frame->nb_samples); if(len<=0){ continue; }
int dst_bufsize = av_samples_get_buffer_size(0, out_channels, len, out_sample_fmt, 1);
fwrite(audio_out_buffer,1,dst_bufsize,file);
} } } } av_packet_unref(pkt); } }while(0);
av_frame_free(&frame); av_packet_free(&pkt); avcodec_close(codecCtx); avcodec_free_context(&codecCtx); avformat_free_context(fmtCtx);
fclose(file);
return 0; }
|