首頁 >後端開發 >Python教學 >如何在Python中取得音訊的持續時間?

如何在Python中取得音訊的持續時間?

WBOY
WBOY轉載
2023-09-02 18:53:051304瀏覽

如何在Python中取得音訊的持續時間?

近年来,音频处理领域取得了显著的扩展,Python已成为处理围绕音频操作的任务的常见选择。在处理音频时,常见的任务之一是确定音频文件的长度,这在各种应用中都非常有用,例如创建播放列表、音频数据分析或开发音频编辑工具。

Throughout this article, you will be guided through a variety of techniques, ranging from the basic to the advanced, in order to obtain the duration of audio using Python. Real code examples will be provided along the way. Before delving deeper into the subject matter, it is crucial to gain an understanding of the fundamental concepts and terminology that pertain to audio processing. This will give you the necessary foundation to implement the various approaches presented later in the article. Let's start with the definition of audio duration and then explore the syntax and algorithms for calculating it.

“音频时长”一词指的是音频文件播放的时间长度,通常以秒或分钟为单位进行测量。这个值受到一系列定义音频文件的特征的影响,包括样本数量、声道和采样率。对这些知识的全面掌握对于各种应用非常重要,包括但不限于转录、分析和音频编辑。

Syntax

Python提供了各种各样的库来管理音频文件处理。这些库包括wave、pydub和librosa,每个库都有自己独特的语法和函数,用于上传音频文件和测量它们的时间长度。确定音频文件持续时间的典型过程包括以下步骤:

  • Importing the mandatory libraries.

  • 读取音频文件。

  • Extracting the file's characteristics (such as the sample rate, quantity of samples, and channel quantity).

  • Calculating the duration utilizing the extracted characteristics.

算法

要在Python中获取音频文件的持续时间,可以实现以下算法 -

  • Implement the appropriate library to upload the audio file.

  • 提取音频文件的相关特征,包括采样率、通道数量和帧数。

  • Calculate the audio file's duration by dividing the number of frames by the sample rate.

  • 通过打印或返回它来输出持续时间值。

Approaches

我们现在将探讨在Python中确定音频文件持续时间的几种技术。将介绍以下方法 −

  • 通过利用波浪库。

  • By using the pydub library.

  • 使用librosa库。

  • By using the ffmpeg-python library.

方法一:使用wave库

波浪库是Python的内置模块,提供对WAV文件的支持。这是一个完整的代码示例,演示如何使用波浪库获取音频文件的持续时间 -

Example

import wave
def get_duration_wave(file_path):
   with wave.open(file_path, 'r') as audio_file:
      frame_rate = audio_file.getframerate()
      n_frames = audio_file.getnframes()
      duration = n_frames / float(frame_rate)
      return duration
file_path = 'example.wav'
duration = get_duration_wave(file_path)
print(f"Duration: {duration:.2f} seconds")

Output

Duration: 10.00 seconds

Approach 2: Using the pydub library

The pydub library stands as a commonly used and simple-to-utilize tool for the manipulation of audio. In order to make use of pydub, you must first install it via pip install pydub. Here's a code example to get the duration using pydub −

Example

from pydub import AudioSegment
def get_duration_pydub(file_path):
   audio_file = AudioSegment.from_file(file_path)
   duration = audio_file.duration_seconds
   return duration
file_path = 'example.wav'
duration = get_duration_pydub(file_path)
print(f"Duration: {duration:.2f} seconds")

Output

Duration: 10.00 seconds

Within this particular code snippet, we import the AudioSegment class, which hails from the pydub library, with the purpose of reading and making alterations to audio files. To load the audio file, we call the from_file function, and the duration_seconds attribute is employed to acquire the length of the audio file in seconds.

使用librosa库的方法三:

Librosa stands as yet another esteemed library for the processing of audio using Python, putting its emphasis mainly on the analysis of music and sound. By typing 'pip install librosa' in your terminal or command prompt, you will be able to easily and quickly install it. Here's a code example to get the duration using librosa −

Example

import librosa
def get_duration_librosa(file_path):
   audio_data, sample_rate = librosa.load(file_path)
   duration = librosa.get_duration(y=audio_data, sr=sample_rate)
   return duration
file_path = 'example.wav'
duration = get_duration_librosa(file_path)
print(f"Duration: {duration:.2f} seconds")

Output

Duration: 10.00 seconds

在这个例子中,使用librosa.load函数来读取音频文件并获取音频数据和采样率。然后,利用librosa.get_duration函数基于音频数据和采样率来计算持续时间。

Approach 4: Using the ffmpeg-python library

FFmpeg是在各种平台上常用的用于处理音频和视频的工具。ffmpeg-python库充当了FFmpeg命令行界面的Python包装器,并可以使用pip install ffmpeg-python进行安装。以下是一个示例代码,演示了如何使用ffmpeg-python获取音频文件的持续时间−

Example

import ffmpeg
def get_duration_ffmpeg(file_path):
   probe = ffmpeg.probe(file_path)
   stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'audio'), None)
   duration = float(stream['duration'])
   return duration
file_path = 'example.wav'
duration = get_duration_ffmpeg(file_path)
print(f"Duration: {duration:.2f} seconds")

Output

Duration: 10.00 seconds

在这个例子中,我们使用ffmpeg.probe函数来获取与音频文件相关的元数据。随后,我们从流列表中过滤出音频流,并从流字典中提取出'duration'字段中的持续时间。

结论

在本文中,我们深入探讨了使用wave、pydub、librosa和ffmpeg-python库在Python中获取音频文件时长的四种不同方法。每种方法都有其自身的优点和限制,库的选择取决于您个人的需求和偏好。这些代码示例旨在为您提供在Python项目中实现音频时长计算的坚实基础。

以上是如何在Python中取得音訊的持續時間?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除