from pydub import AudioSegment
import math
def get_audio_length(audio_file_path):
# Load the audio file
audio_file = AudioSegment.from_file(audio_file_path)
# Get the duration of the audio in seconds
duration_sec = len(audio_file) / 1000
# Round up the duration to the highest integer
duration_sec = int(math.ceil(duration_sec))
return duration_sec
我试图通过将我的 mp3 文件转换为 WAV 文件来使用 wave 模块,并尝试使用我的其他程序来做同样的事情,但给出了同样的错误答案。返回的秒数显然是错误的,而不仅仅是半秒。 希望你能帮助我。
您可以尝试使用
audioread
模块。
with audioread.audio_open(song) as song: length = int(song.duration)
这给出了歌曲的长度(以秒为单位)。它以歌曲文件作为音频打开的参数。