ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
来源:互联网 发布:电脑淘宝怎么开店 编辑:程序博客网 时间:2024/06/06 14:41
ffmpeg 2.0 音频解码出来的数据格式不符合Android音频格式
问题原因:
ffmpeg2.0最新的解码出来的数据是sample_fmts = AV_SAMPLE_FMT_FLTP
android需要的音频格式:sample_fmts (AV_SAMPLE_FMT_S8, AV_SAMPLE_FMT_S16)
解决方法:
1, 创建转换对象
void audio_swr_resampling_audio_init(SwrContext**swr_ctx,TargetAudioParams *targetAudioParams,AVCodecContext *codec){
if(codec->sample_fmt == AV_SAMPLE_FMT_S16 || codec->sample_fmt ==AV_SAMPLE_FMT_S32 ||codec->sample_fmt == AV_SAMPLE_FMT_U8){
LOGE("codec->sample_fmt:%d",codec->sample_fmt);
if(*swr_ctx){
swr_free(swr_ctx);
*swr_ctx = NULL;
}
return;
}
if(*swr_ctx){
swr_free(swr_ctx);
}
*swr_ctx =swr_alloc();
if(!*swr_ctx){
LOGE("swr_alloc failed");
return;
}
/* set options*/
av_opt_set_int(*swr_ctx, "in_channel_layout", codec->channel_layout, 0);
av_opt_set_int(*swr_ctx, "in_sample_rate", codec->sample_rate, 0);
av_opt_set_sample_fmt(*swr_ctx,"in_sample_fmt", codec->sample_fmt, 0);
av_opt_set_int(*swr_ctx, "out_channel_layout", targetAudioParams->channel_layout, 0);
av_opt_set_int(*swr_ctx, "out_sample_rate", targetAudioParams->sample_rate, 0);
av_opt_set_sample_fmt(*swr_ctx, "out_sample_fmt",targetAudioParams->sample_fmt, 0);// AV_SAMPLE_FMT_S16
/* initializethe resampling context */
int ret = 0;
if ((ret =swr_init(*swr_ctx)) < 0) {
LOGE("Failed to initialize the resampling context\n");
if(*swr_ctx){
swr_free(swr_ctx);
*swr_ctx = NULL;
}
return;
}
}
2, 转换(AV_SAMPLE_FMT_FLTPàAV_SAMPLE_FMT_S16)
int audio_swr_resampling_audio(SwrContext*swr_ctx,TargetAudioParams *targetAudioParams,AVFrame *audioFrame,uint8_t**targetData){
int len =swr_convert(swr_ctx,targetData,audioFrame->nb_samples,audioFrame->extended_data,audioFrame->nb_samples);
if(len < 0){
LOGE("error swr_convert");
goto end;
}
int dst_bufsize= len * targetAudioParams->channels *av_get_bytes_per_sample(targetAudioParams->sample_fmt);
LOGI("dst_bufsize:%d",dst_bufsize);
returndst_bufsize;
end:
return -1;
}
3, 销毁
最后处理完成销毁对象
void audio_swr_resampling_audio_destory(SwrContext**swr_ctx){
if(*swr_ctx){
swr_free(swr_ctx);
*swr_ctx =NULL;
}
}
参考:http://stackoverflow.com/questions/14989397/how-to-convert-sample-rate-from-av-sample-fmt-fltp-to-av-sample-fmt-s16
- ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
- ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
- convert sample rate from AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
- FFmpeg学习—ffmpeg 利用 swr_convert 函数将AV_SAMPLE_FMT_S16 转 AV_SAMPLE_FMT_FLTP
- ffmpeg avcodec_decode_audio4解码 swr_convert 转换为AV_SAMPLE_FMT_S16格式
- FFmpeg学习—解决ffmpeg 进行Audio 编码 采用AV_SAMPLE_FMT_S16 格式报 Input contains NaN/+-Inf 错误的问题
- AV_SAMPLE_FMT_S16P and AV_SAMPLE_FMT_S16 的区别
- how to use ffmpeg
- Port Libmad to FFmpeg
- Introduction to FFmpeg
- swr_convert音频重采样成AV_SAMPLE_FMT_FLTP
- ffmpeg video to opengl texture
- x264 to FFmpeg option mapping
- ffmpeg 命令 rgb565 to YUV420p
- ffmpeg push h264 to rtp
- FFmpeg Understanding(3. How to compile FFMPEG in Windows?)
- FFmpeg Understanding(5. How to install GSM lib for FFMPEG?)
- HOWTO add new codec to ffmpeg
- Expression Blend实例中文教程(12) - 样式和模板快速入门Style,Template
- usbnet驱动深入分析-usb虚拟网卡host端
- 单片机联网需求攀升 全硬件TCP/IP技术有招
- 不朽传奇-云计算技术背后的那些天才程序员:Qemu的作者法布里斯贝拉
- structs2 接受参数的三种方法(转载)
- ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
- 基于Android/IOS视频语音通话商业产品源码转让
- 逆转链表的一部分 Reverse Linked List II
- Java中GC机制
- eclipse 界面设置
- Expression Blend实例中文教程(11) - 视觉管理器快速入门Visual State Manager(VSM)
- STM32F103VCT6+W5500 UDP通信成功
- windows app store 应用推荐
- Expression Blend实例中文教程(10) - 缓冲动画快速入门Easing