如何在Python获取音频的持续时间?
使用Python语言可以找到音频文件的持续时间,该语言具有丰富的库使用。一些库的使用,如mutagen 、 wave 、 audioread 、 等不仅限于提取音频文件的长度/持续时间,而且具有更多功能。
源音频文件:
现在让我们看看,我们如何使用Python获取任何音频文件的持续时间:
方法 1:使用Python库“Mutagen”
Mutagen 是一个Python模块,用于处理音频元数据。它支持各种格式的音频文件,如 wavpack、mp3、Ogg 等。以下是使用它计算音频文件时长的步骤:
第 1 步:安装诱变剂
由于 Mutagen 是一个外部Python库,因此首先需要使用 pip 命令安装它,如下所示:
pip install mutagen
第 2 步:导入诱变剂
安装后,需要使用以下命令将其导入到我们的脚本中,
import mutagen
在我们的程序中,我们将使用 Mutagen 库的一些内置函数,让我们稍微探索一下它们以更好地理解源代码:
1) 波()
Syntax: WAVE(file thing/filename)
Use: It simply creates a WAVE object of the filename being provided as a parameter.
Example: voice = WAVE(“sample.wav”)
2)信息
Syntax: variable.info
Use: It fetches the metadata of the audio file whose WAVE object has been created.
Example: voice_info = voice.info
3) 长度
Syntax: variable.length
Use: It returns audio length in seconds. The value returned is in float(by default).
Example: voice_length = voice_info.length
以下是记录任何音频文件的持续时间/长度的实际Python脚本:
Python3
import mutagen
from mutagen.wave import WAVE
# function to convert the information into
# some readable format
def audio_duration(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return hours, mins, seconds # returns the duration
# Create a WAVE object
# Specify the directory address of your wavpack file
# "alarm.wav" is the name of the audiofile
audio = WAVE("alarm.wav")
# contains all the metadata about the wavpack file
audio_info = audio.info
length = int(audio_info.length)
hours, mins, seconds = audio_duration(length)
print('Total Duration: {}:{}:{}'.format(hours, mins, seconds))
Python3
# METHOD 2
import audioread
# function to convert the information into
# some readable format
def duration_detector(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return hours, mins, seconds
# alarm.wav is the name of the audio file
# f is the fileobject being created
with audioread.audio_open('alarm.wav') as f:
# totalsec contains the length in float
totalsec = f.duration
hours, mins, seconds = duration_detector(int(totalsec))
print('Total Duration: {}:{}:{}'.format(hours, mins, seconds))
Python3
# Method 3
import scipy
from scipy.io import wavfile
# function to convert the information into
# some readable format
def output_duration(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return hours, mins, seconds
# sample_rate holds the sample rate of the wav file
# in (sample/sec) format
# data is the numpy array that consists
# of actual data read from the wav file
sample_rate, data = wavfile.read('alarm.wav')
len_data = len(data) # holds length of the numpy array
t = len_data / sample_rate # returns duration but in floats
hours, mins, seconds = output_duration(int(t))
print('Total Duration: {}:{}:{}'.format(hours, mins, seconds))
输出:
Total Duration: 0:0:2
方法 2:使用Python库“Audioread”
Audioread 是Python 的跨库音频解码。它使用任何可用的后端解码音频文件。以下是使用它计算音频文件时长的步骤:
第一步:安装audioread
由于audioread是一个外部Python库,因此首先需要使用pip命令安装它,如下所示:
pip install audioread
第 2 步:导入 audioread
安装后,需要使用以下命令将其导入到我们的脚本中,
import audioread
在我们的程序中,我们将使用 audioread 库的一些内置函数,让我们稍微探索一下它们以更好地理解源代码:
1)audio_open()
Syntax: audioread.audio_open(filename)
Use: It simply opens an audio file using a library available on the system
Example: with audioread.audio_open(‘example.wav’) as ex:
#statement 1…statement n
2) 持续时间
Syntax: fileobject.duration
Use: It returns the length of the audio in seconds (a float by default).
Example: variable= fptr.duration
以下是记录任何音频文件的持续时间/长度的实际Python脚本:
蟒蛇3
# METHOD 2
import audioread
# function to convert the information into
# some readable format
def duration_detector(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return hours, mins, seconds
# alarm.wav is the name of the audio file
# f is the fileobject being created
with audioread.audio_open('alarm.wav') as f:
# totalsec contains the length in float
totalsec = f.duration
hours, mins, seconds = duration_detector(int(totalsec))
print('Total Duration: {}:{}:{}'.format(hours, mins, seconds))
输出:
Total Duration: 0:0:2
方法 3:使用Python库“Scipy”
SciPy 有许多模块、类和函数可用于从各种文件格式(如 Wav 声音文件、MATLAB 文件等)读取数据和写入数据。以下是使用它计算音频文件的持续时间的步骤:
第 1 步:安装 Scipy
由于 Scipy 是一个外部Python库,因此首先需要使用 pip 命令安装它,如下所示:
pip install scipy
第 2 步:导入 Scipy
安装后,需要使用以下命令将其导入到我们的脚本中,
import scipy
from scipy.io import wavfile
在我们的程序中,我们将使用 Scipy 库的一些内置函数,让我们对它们进行一些探索以更好地理解源代码:
1) scipy.io.wavfile.read()
Syntax: scipy.io.wavfile.read(filename)
Use: It returns the sample rate (in samples/sec) and data from a WAV file. The file can be an open file or a filename. The returned sample rate is a Python integer. The data is returned as a NumPy array with a data-type determined from the file.
Example: variable1,variable2 = scipy.io.wavfile.read(‘example.wav’)
以下是记录任何音频文件的持续时间/长度的实际Python脚本:
蟒蛇3
# Method 3
import scipy
from scipy.io import wavfile
# function to convert the information into
# some readable format
def output_duration(length):
hours = length // 3600 # calculate in hours
length %= 3600
mins = length // 60 # calculate in minutes
length %= 60
seconds = length # calculate in seconds
return hours, mins, seconds
# sample_rate holds the sample rate of the wav file
# in (sample/sec) format
# data is the numpy array that consists
# of actual data read from the wav file
sample_rate, data = wavfile.read('alarm.wav')
len_data = len(data) # holds length of the numpy array
t = len_data / sample_rate # returns duration but in floats
hours, mins, seconds = output_duration(int(t))
print('Total Duration: {}:{}:{}'.format(hours, mins, seconds))
输出:
Total Duration: 0:0:2