Files
petshy/filter_audio.py
2025-10-08 20:39:09 +08:00

59 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import librosa # 用于获取音频时长
from pathlib import Path
def get_audio_duration(file_path):
"""获取音频文件的时长(秒)"""
try:
# 加载音频文件并获取时长(不加载音频数据,仅获取元信息)
duration = librosa.get_duration(path=file_path)
return duration
except Exception as e:
print(f"无法处理文件 {file_path}{str(e)}")
return None
def filter_short_audios(folder_path, max_seconds=3):
"""筛选出目录中时长小于指定秒数的音频文件"""
# 支持的音频格式(可根据需要扩展)
audio_extensions = ('.wav', '.mp3', '.flac', '.ogg', '.m4a')
# 存储符合条件的文件路径
short_audios = []
# 遍历目录中的所有文件
for root, dirs, files in os.walk(folder_path):
for file in files:
# 检查文件扩展名是否为音频格式
if file.lower().endswith(audio_extensions):
file_path = os.path.join(root, file)
duration = get_audio_duration(file_path)
if duration is not None and duration < max_seconds:
short_audios.append({
'path': file_path,
'duration': round(duration, 2) # 保留两位小数
})
return short_audios
if __name__ == "__main__":
# 替换为你的音频文件目录
audio_folder = "/Users/linhong/Desktop/a_PythonProjects/cat_translator_v2/cat_intents/emotions/等待喂食"
# 检查目录是否存在
if not os.path.isdir(audio_folder):
print(f"错误:目录 {audio_folder} 不存在")
else:
# 筛选出低于3秒的音频
short_files = filter_short_audios(audio_folder, max_seconds=3)
if short_files:
print(f"共找到 {len(short_files)} 个低于3秒的音频文件")
for item in short_files:
print(f"{item['path']} (时长:{item['duration']}秒)")
else:
print("未找到低于3秒的音频文件")