近年来,音频处理领域取得了显著的扩展,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中文網其他相關文章!

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)