FFmpeg:如何估算音频流中的样本数量?

编程入门 行业动态 更新时间:2024-10-09 01:14:22
本文介绍了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:如何估算音频流中的样本数量?

本文发布于:2023-06-03 07:03:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/471205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样本   音频   数量   FFmpeg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!