我有这个函数,它告诉我音频的长度,但给出了一个错误的数字。

huangapple go评论63阅读模式
英文:

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 audioreadmodule.

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.

huangapple
  • 本文由 发表于 2023年5月17日 22:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定