C++ 用FFMPEG解码音频
我正在使用FFMPEG对音频文件进行解码,但我无法做到这一点C++ 用FFMPEG解码音频,c++,audio,ffmpeg,decoding,C++,Audio,Ffmpeg,Decoding,我正在使用FFMPEG对音频文件进行解码,但我无法做到这一点 AVFormatContext *pFormatCtx; AVCodec *pCodec_audio; AVCodecContext *aCodecCtx_audio = NULL; AVPacket packet; string outfilename = "salida.avi"; string filename = "john.wav"; int audioStream; int out_size, len, size;
AVFormatContext *pFormatCtx;
AVCodec *pCodec_audio;
AVCodecContext *aCodecCtx_audio = NULL;
AVPacket packet;
string outfilename = "salida.avi";
string filename = "john.wav";
int audioStream;
int out_size, len, size;
FILE *outfile;
int16_t *outbuf;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
avcodec_init();
/* register all the codecs */
av_register_all();
if(av_open_input_file(&pFormatCtx, filename.c_str(), NULL, 0, NULL) != 0) {
cerr << "Archivo no encontrado" << endl;
return -1; // Couldn't open file
}
if(av_find_stream_info(pFormatCtx) < 0) {
cerr << " No encontro el stream de info" << endl;
return -1;// Couldn't find stream information
}
dump_format(pFormatCtx, 0, filename.c_str(), 0);
for(unsigned int i=0; i < pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
audioStream = i;
aCodecCtx_audio = pFormatCtx->streams[audioStream]->codec;
pCodec_audio = avcodec_find_decoder(aCodecCtx_audio->codec_id);
if(pCodec_audio == NULL) {
cerr<< "Unsupported codec!" << endl;
return -1; // Codec not found
}
if(avcodec_open(aCodecCtx_audio, pCodec_audio) < 0) {
cerr << "No se pudo abrir el codec de audio" << endl;
return -1;
}
outbuf = (int16_t*) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
outfile = fopen(outfilename.c_str(), "wb");
if (!outfile) {
exit(1);
}
av_init_packet(&packet);
packet.data = inbuf;
while(av_read_frame(pFormatCtx, &packet) == 0) {
if(packet.stream_index == audioStream) {
size = packet.size;
if (size == 0) {
cerr << "Size = 0 " << endl;
break;
}
while(size > 0) {
out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
len = avcodec_decode_audio3(aCodecCtx_audio, outbuf, &out_size , &packet);
//av_free_packet(&packet);
cout << len << endl;
if(len == -1) {
cerr << "Error while decoding" << endl;
return 1;
}
if(out_size > 0) {
fwrite(outbuf, 1, out_size, outfile);
}
size -= len;
packet.data += len;
}
}
}
av_free_packet(&packet);
fclose(outfile);
free(outbuf);
cout << "END CODE" << endl;
avcodec_close(aCodecCtx_audio);
av_free(aCodecCtx_audio);
AVFormatContext*pFormatCtx;
AVCodec*pCodec_音频;
AVCodecContext*aCodecCtx_audio=NULL;
数据包;
字符串outfilename=“salida.avi”;
字符串filename=“john.wav”;
int音频流;
int out_size,len,size;
文件*输出文件;
int16_t*突发事件;
uint8_t inbuf[inbuf_SIZE+FF_INPUT_BUFFER_PADDING_SIZE];
avcodec_init();
/*注册所有编解码器*/
av_寄存器_all();
if(av_open_input_file(&pFormatCtx,filename.c_str(),NULL,0,NULL)!=0){
cerr流[i]->编解码器->编解码器类型==编解码器类型音频)
音频流=i;
aCodecCtx_audio=pFormatCtx->streams[audioStream]->编解码器;
pCodec_audio=avcodec_find_解码器(aCodecCtx_audio->codec_id);
if(pCodec_audio==NULL){
cerrI不理解您的问题。您的问题是什么?您的代码似乎将原始解码帧写入文件。您需要使用FFMpeg编码功能的帮助吗?我想将音频文件(在本例中为.wav;)解码为.mp3文件,但无法复制。为什么?或者我必须将音频文件编码为原始数据,然后解码为mp3?您想播放声音吗?您必须将波形的内容发送到某个可以播放PCM原始数据的设备。@bob2是正确的。您正在将波形音频解码到PCM中,但没有将该PCM重新编码为mp3(或任何其他格式)。您只完成了所需工作的一半。