FFmpeg:如何估算音频流中的样本数量? [英] FFmpeg: How to estimate number of samples in audio stream?

查看:516
本文介绍了FFmpeg:如何估算音频流中的样本数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个小型应用程序,该应用程序使用FFmpeg库来解码C ++中的音频文件(尤其是avformat和swresample).

I'm currently writing a small application that's making use of the FFmpeg library in order to decode audio files (especially avformat and swresample) in C++.

现在,我需要音频流中的样本总数.我知道确切的数字只能通过实际解码所有帧才能找到,我只需要一个估计即可. 这里首选的方法是什么?如何找出文件的持续时间?

Now I need the total number of samples in an audio stream. I know that the exact number can only be found out by actually decoding all the frames, I just need an estimation. What is the preferred method here? How can I find out the duration of a file?

推荐答案

我相信您需要的是AUDIORATE/FRAMERATE公式.例如,如果ar = 48000,并且视频的帧速率为50fps,则您需要每帧48000/50 = 960个样本.

I believe what you need is the formula that is AUDIORATE / FRAMERATE. For instance, if ar=48000, and frame rate of video is let's say 50fps then 48000/50 = 960 samples per frame you need.

缓冲区计算随后以samples_per_frame * nChannels * (audiobit/8)的形式出现. AudioBit通常为16位(也可以为24位或32位).因此,对于16位48Khz的8通​​道音频,每个音频帧需要960 * 8 * 2 = 15360字节. 进行此最后一次计算的官方方法是使用:

Buffer calculation comes later as samples_per_frame * nChannels * (audiobit/8). AudioBit is usually 16bit (24 or 32bits also possible). So for 8 channels audio at 16bit 48Khz, you'll need 960 * 8 * 2 = 15360 bytes per audio frame. Offical way to do this last calculation is to use :

av_samples_get_buffer_size(NULL, nChannels, SamplesPerFrame, audio_st->codec->sample_fmt, 0) 功能.

av_samples_get_buffer_size(NULL, 8, 960, audio_st->codec->sample_fmt, 0)

也将返回15360(对于专家:是的,我假设格式为pcm_s16le). 因此,这回答了您问题的第一部分.希望有帮助.

will return also 15360 (For experts: yes I'm assuming format is pcm_s16le). So this answers first part of your question. Hope that helps.

这篇关于FFmpeg:如何估算音频流中的样本数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆