ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
来源:互联网 发布:淘宝怎么写评论 编辑:程序博客网 时间:2024/06/05 19:58
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
/* initialize the 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);
return dst_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
- Java API javax.swing.JTable
- 算法与追mm:算法导论之SSM PKU版
- 各种检索的关系
- vmware安装opensuse
- cocos2d-x ccsprite 使用 shader生成自己想要的颜色的图片
- ffmpeg AV_SAMPLE_FMT_FLTP to AV_SAMPLE_FMT_S16
- mysql中常用的函数
- spring的RabbitTemplate 接收Message源码导读
- 连载《一个程序员的成长历程》-11.80后结婚,房子是悬在每个人心中的一块石头
- Annotation 使用笔记(二) 注解生成SQL脚本
- A program to check if a binary tree is BST or not
- Linux磁盘分区
- POJ 3281 Dining
- 简单的模态对话框ModalDialog -- 源码分享