英文:
I have this function that tells me the length of an audio but it tells me a wrong number
问题
from pydub import AudioSegment
import math
def get_audio_length(audio_file_path):
# 加载音频文件
audio_file = AudioSegment.from_file(audio_file_path)
# 获取音频的时长(以秒为单位)
duration_sec = len(audio_file) / 1000
# 将时长向上取整到最接近的整数
duration_sec = int(math.ceil(duration_sec))
return duration_sec
我尝试使用了一个波形模块,将我的MP3文件转换为WAV文件,然后尝试使用我的另一个程序,但返回的时间明显错误,并且不仅仅是差半秒。希望你可以帮助我。
英文:
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
I tried to use a wave module by coverting my mp3 file into a WAV file and trying to use my other programm that does the same but the same wrong answer is delivered. The seconds that are returned are clearly wrong and not just by half a second.
Hope you can help me.
答案1
得分: 0
你可以尝试使用 audioread
模块。
with audioread.audio_open(song) as song: length = int(song.duration)
这会返回歌曲的长度(秒),它以歌曲文件作为音频打开的参数。
英文:
You can try using the audioread
module.
with audioread.audio_open(song) as song:
length = int(song.duration)
This gives the length of the song in seconds. It takes a song file as the argument for audio open.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论