简体   繁体   English

如何在不实际转换的情况下确定转换后音频文件的理论文件大小(即 mp3 到 wav)

[英]How to determine theoretical filesize of converted audio file without actually converting (i.e. mp3 to wav)

Background: I am writing a python script that will take in an audio file and modify it using pydub.背景:我正在编写一个 python 脚本,该脚本将接收音频文件并使用 pydub 对其进行修改。 Pydub seems to require converting audio input to a wav format though, which has a 4GB limit. Pydub 似乎需要将音频输入转换为 wav 格式,它有 4GB 的限制。 So I put in a 400MB.m4a file into pydub and get an error that the file is too large.所以我把一个 400MB.m4a 的文件放到 pydub 中,得到一个文件太大的错误。

Instead of having pydub run for a couple minutes, then throw an error if the converted decompressed size is too large, I would like to quickly calculate ahead of time what the decompressed filesize would be.我不想让 pydub 运行几分钟,然后如果转换后的解压缩大小太大会抛出错误,我想提前快速计算解压缩文件的大小。 If over 4GB, my script will chop the original audio and then run through pydub.如果超过 4GB,我的脚本将剪切原始音频,然后通过 pydub 运行。

Thanks.谢谢。

It's simple arithmetic to calculate the size of a theoretical.WAV file.计算一个理论.WAV 文件的大小是一种简单的算法。 The size, in bytes, is the bit depth divided by 8, multiplied by the sample rate, multiplied by the duration, multiplied by the number of channels.大小(以字节为单位)是位深度除以 8,乘以采样率,乘以持续时间,乘以通道数。

So if you had an audio clip that was 3:20 long, 44100Hz, 16-bit and stereo, the calculation would be:因此,如果您有一个长度为 3:20、44100Hz、16 位和立体声的音频剪辑,则计算将是:

sample_rate = 44100 # Hz/Samples per second - CD Quality
bit_depth = 16 # 2 bytes; CD quality
channels = 2 # stereo
duration = 200.0 # seconds

file_size = sample_rate * (bit_depth / 8) * channels * duration
        # = 44100 * (2) * 2 * 200
        # = 35280000 bytes
        # = 35.28 MB (megabytes)

I found this online audio file size calculator which you can also use to confirm your math: https://www.colincrawley.com/audio-file-size-calculator/我找到了这个在线音频文件大小计算器,您也可以用它来确认您的数学: https://www.colincrawley.com/audio-file-size-calculator/

If instead you wanted to figure out the other direction, ie the size of a theoretical compressed file, it depends on how you're doing the compression.相反,如果您想找出另一个方向,即理论压缩文件的大小,则取决于您如何进行压缩。 Typical compression, thankfully, uses just a fixed bitrate, meaning the math to figure out the resulting compressed file size is really simple.幸运的是,典型的压缩只使用固定的比特率,这意味着计算得到的压缩文件大小的数学非常简单。

So, if you had a 3:20 audio clip you wanted to convert to MP3, at a bitrate of 128kbps (kilobits per second, 128 is a common mid-range quality setting), the calculation would just be the bit rate, divided by 8 (bits per byte) multiplied by the duration:因此,如果您有一个 3:20 的音频片段要转换为 MP3,比特率为 128kbps(千比特每秒,128 是一种常见的中档质量设置),则计算只是比特率除以8(每字节位数)乘以持续时间:

bits_per_kb = 1000
bitrate_kbps = 128
bits_per_byte = 8
duration_seconds = 200
filesize_bytes = (bitrate_kbps * bits_per_kb / bits_per_byte) * duration_seconds
             # = (128000 / 8) * 200
             # = (16) * 200
             # = 3200000 bytes
             # = 3.2 MB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 .wav 文件转换为 .mp3 - Converting a .wav file to .mp3 将WAV音频文件上传到服务器后是否自动将其转换为MP3? - Converting WAV audio files to MP3 on a server automatically once uploaded? 如何使用 Python 将音频文件(.mp3 或 .wav 或任何其他文件)转换为唯一的音频 ID? - How to convert an audio file (.mp3 or .wav or any other) to an unique audio id using Python? 如何将“IPython.lib.display.Audio”文件导出为 mp3 或 wav 文件? - How to export an "IPython.lib.display.Audio" file as a mp3 or wav file? Python播放mp3 / wav文件音频(Pygame混音器) - Python play mp3/wav file audio(Pygame mixer) 用于单元测试的最小音频(wav或mp3)文件(以字节为单位) - Minimal audio (wav or mp3) file in bytes for unit testing 如何获取音频(.wav或.mp3)文件中每秒或毫秒的帧数(或样本数)? - How to get number of Frames(or Samples) per sec or ms in a audio (.wav or .mp3) file? 如何从 Python 在 Mac 上播放 WAV 或 MP3 音频文件触发? - How to trigger from Python playing of a WAV or MP3 audio file on a Mac? 如何使用python脚本停止/关闭音频文件(mp3 / .wav) - how to stop/close an audio file(mp3/.wav) using python script 将wav转换为mp3时如何使用pysox指定比特率 - How to use pysox to specify the bitrate when converting wav to mp3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM