35 lines
820 B
Python
35 lines
820 B
Python
|
|
"""
|
|||
|
|
音频文件解析工具
|
|||
|
|
|
|||
|
|
用于解析音频文件的元数据信息,如时长、采样率、编码格式等
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from mutagen import File as MutagenFile
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_audio_duration(file_path: str) -> int:
|
|||
|
|
"""
|
|||
|
|
获取音频文件时长(秒)
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
file_path: 音频文件的完整路径
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
音频时长(秒),如果解析失败返回0
|
|||
|
|
|
|||
|
|
支持格式:
|
|||
|
|
- MP3 (.mp3)
|
|||
|
|
- M4A (.m4a)
|
|||
|
|
- MP4 (.mp4)
|
|||
|
|
- WAV (.wav)
|
|||
|
|
- 以及mutagen支持的其他格式
|
|||
|
|
"""
|
|||
|
|
try:
|
|||
|
|
audio = MutagenFile(file_path)
|
|||
|
|
if audio is not None and hasattr(audio.info, 'length'):
|
|||
|
|
return int(audio.info.length)
|
|||
|
|
return 0
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"获取音频时长失败 ({file_path}): {e}")
|
|||
|
|
return 0
|