您的位置:首页 > 其它

ffmpeg tutorial3 杂音

2015-12-04 16:11 225 查看
开始接触学习ffmpeg,ffmpeg tutorial 是不错的入门教程,但ffmpeg tutorial3声音无法正常播放。

由于刚接触ffmpeg,无从下手,只能按以前的调试习惯,单独调试音频部分,发现播放wav文件时正常,播放mp3有杂音但不像播放电影文件时那样全是杂音。此时,心里隐隐感觉有点眉目,但又不清楚问题在哪里,后在网上查找资料,终于找到篇帖子便是对这个问题的说明:
http://bbs.csdn.net/topics/390700255?page=1
原来,ffmpeg新的版本,音频也像视频那样分为packed、plannar存储格式(描述可能不准确,大概是这种意思),于是便有了思路。

参考上面那篇帖子,对ffmpeg tutorial3进行了修改,果然声音正常了,特此做下笔记:

对audio_decode_frame做如下修改:

<pre name="code" class="cpp">if(got_frame)
{
data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(&frame),
frame.nb_samples,
frame.format, 0);
assert(data_size <= buf_size);

if(frame.channels > 0 && frame.channel_layout == 0)
frame.channel_layout =
av_get_default_channel_layout(frame.channels);
else if(frame.channels == 0 && frame.channel_layout > 0)
frame.channels =
av_get_channel_layout_nb_channels(frame.channel_layout);
{
if( swr_ctx )
{
swr_free(swr_ctx);
swr_ctx = NULL;
}

swr_ctx = swr_alloc_set_opts(NULL, wanted_frame.channel_layout,
wanted_frame.format, wanted_frame.sample_rate,
frame.channel_layout, frame.format,
frame.sample_rate, 0, NULL);

if( !swr_ctx || swr_init(swr_ctx) < 0)
{
fprintf(stderr, "swr_init() failed\n");
break;
}

int len2 = swr_convert(swr_ctx, &audio_buf, buf_size / wanted_frame.channels
/ av_get_bytes_per_sample(wanted_frame.format),
frame.data, frame.nb_samples);
if( len2 < 0 )
{
fprintf(stderr, "swr_convert failed\n");
break;
}

return wanted_frame.channels * len2 * av_get_bytes_per_sample(wanted_frame.format);
}
}



ffmpeg tutorial3 点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: