python - 如何获取Python中wave模块不支持的.WAV文件的持续时间?

标签 python duration wave

我正在编写一个程序来注释 .wav 文件,因此我需要播放它们并了解它们的持续时间。我可以使用 winsound 模块来播放(使用 SND_ASYNC),但我无法使用 wave 模块来读取文件,因为不支持我使用的文件的压缩。

我应该使用另一个模块来获取 .WAV 文件的持续时间,还是应该使用一个模块来播放和获取有关文件的信息?我应该使用哪些模块?

最佳答案

查看评论,这有效(我为了自己的可读性做了一些更改)。谢谢@Aya!

import os
path="c:\\windows\\system32\\loopymusic.wav"
f=open(path,"rb")

# read the ByteRate field from file (see the Microsoft RIFF WAVE file format)
# https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
# ByteRate is located at the first 28th byte
f.seek(28)
a = f.read(4)

# convert string a into integer/longint value
# a is little endian, so proper conversion is required
byteRate = 0
for i in range(4):
    byteRate += a[i] * pow(256, i)

# get the file size in bytes
fileSize = os.path.getsize(path)  

# the duration of the data, in milliseconds, is given by
ms = ((fileSize - 44) * 1000)) / byteRate

print "File duration in miliseconds : " % ms
print "File duration in H,M,S,mS : " % ms / (3600 * 1000) % "," % ms / (60 * 1000) % "," % ms / 1000 % "," ms % 1000
print "Actual sound data (in bytes) : " % fileSize - 44
f.close()

关于python - 如何获取Python中wave模块不支持的.WAV文件的持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298944/

相关文章:

php添加多个持续时间以获得总持续时间

ios - 如何在玩 Titanium 时返回 5 秒,单击按钮时应该会发生这种情况

android - 如何使用 AudioTrack 生成和播放 20Hz 方波?

python - 如何在python中读取.wav字节流

python - 用于命令行输入/输出的 GUI 界面

python - 按比例将图像裁剪到最大的中心部分

Python - 创建 aws lambda 部署包

python - Django Admin 同时使用 Django Reversion 和 Django-Import-Export

c# - 从时间跨度持续时间中删除分钟数

audio - 如何将音频 (.wav) 转换为卫星图像