使用 FFMPEG 解码音频
我正在使用 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;
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);
在代码中,我尝试将 .wav 文件解码为 .avi 文件。输出文件(“salida.avi”)已创建,但无法播放。
I am using FFMPEG to decode an audio file, but I am not able to do it.
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);
In the code I am trying to decode an .wav file to a .avi file. The output file ("salida.avi") is created , but does not play.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论