ffmpeg 解码mp3 保存成PCM数据
保存下来的pCM数据转换成WAV播放声音很奇怪,不知代码那里出了问题,大牛帮忙解决下吧 分不够在加
int main(int argc,char **argv)
{
if(argc < 3)
{
printf("argc number is error\n");
return 0;
}
avcodec_register_all();
av_register_all();
AVFormatContext *pFormatCtx;
AVCodec *codec;
AVCodecContext *c= NULL;
int len;
FILE *f, *outfile;
uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)];
AVPacket avpkt;
AVFrame *decoded_frame = NULL;
if(!(pFormatCtx = avformat_alloc_context()))
{
return -1;
}
if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
return -1;
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1;
//av_dump_format(pFormatCtx, 0, inputfilename, 0);
// int i,audio_index = -1;
// for(i=0; i<pFormatCtx->nb_streams; i++)
// {
// if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audio_index < 0)
// audio_index=i;
// }
//codec = avcodec_find_decoder(pFormatCtx->streams[audio_index]->codec->codec_id);
codec = avcodec_find_decoder(AV_CODEC_ID_MP3);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
av_init_packet(&avpkt);
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(argv[1], "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
outfile = fopen(argv[2], "a+");
if (!outfile) {
av_free(c);
exit(1);
}
if (!(decoded_frame = avcodec_alloc_frame())) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
avcodec_get_frame_defaults(decoded_frame);
while(av_read_frame(pFormatCtx, &avpkt)>=0)
{
int got_frame = 0, len = 0 , data_size = 0;
len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
if(len < 0)
{
continue;
}
if(got_frame <= 0) /* No data yet, get more frames */
continue;
data_size = av_samples_get_buffer_size(NULL, c->channels, decoded_frame->nb_samples, c->sample_fmt, 1);
//memcpy(audio_buf, decoded_frame->data[0], decoded_frame->linesize[0]);
if(data_size < 0)
{
continue;
}
fwrite(decoded_frame->data[0],1,data_size,outfile);
}
fclose(outfile);
fclose(f);
avcodec_close(c);
av_free(c);
avcodec_free_frame(&decoded_frame);
}
...全文
相关推荐
将数据转换成数组
将MP3转换为数组,主要涉及音频解码和数据提取过程。1. **音频解码**:首先,我们需要对MP3文件进行解码,这通常涉及到使用音频解码库,如Python中的`pydub`或FFmpeg。
c#基于FFMPEG的视频转码(视频转音频)
当进行视频转音频操作时,FFMPEG首先读取视频文件中的音频流,然后使用合适的解码器解码音频数据,最后用编码器将解码后的音频数据编码为目标音频格式,如MP3、AAC或WAV。
mp3转wav转换器
“mp3转wav转换器”是一个专门用于将MP3文件转换为WAV格式的工具,其工作原理主要是读取MP3文件的编码数据,然后解码成原始的PCM(Pulse Code Modulation)数据,再重新编码成
AAC.zip_aac_aac文件_aac读取_aac音频_ffmpeg
输出的PCM数据可以进一步处理或保存到文件。除了基本的读取和解码,FFmpeg还提供了许多高级功能,如AAC编码、音轨合并、音量调整等。
易语言内存播放MP3源码
内存播放MP3的实现主要涉及到以下几个关键知识点:1. **音频解码**:MP3是一种有损音频压缩格式,播放MP3文件前需要先进行解码,将压缩的位流转换成原始的PCM(脉冲编码调制)数据。


多媒体/流媒体开发
