添加3s至10s的音频过滤

This commit is contained in:
Downupanddownup 2024-04-25 16:45:42 +08:00
parent 441ab54889
commit f61a723bab

View File

@ -1,8 +1,27 @@
import os import os
import shutil import shutil
import random import random
import librosa
def check_audio_duration(path, min_duration=3, max_duration=10):
try:
# 加载音频文件
audio, sample_rate = librosa.load(path)
# 计算音频的时长(单位:秒)
duration = librosa.get_duration(y=audio, sr=sample_rate)
# 判断时长是否在3s至10s之间
if min_duration <= duration <= max_duration:
return True
else:
return False
except Exception as e:
print(f"无法打开或处理音频文件:{e}")
return None
def convert_from_list(list_file, output_dir): def convert_from_list(list_file, output_dir):
# 创建输出目录,如果它不存在的话 # 创建输出目录,如果它不存在的话
@ -38,9 +57,13 @@ def convert_from_list(list_file, output_dir):
print(f"Audio file does not exist: {audio_path}") print(f"Audio file does not exist: {audio_path}")
continue continue
# 复制音频文件到output目录并重命名 if check_audio_duration(audio_path):
shutil.copy2(audio_path, new_path) # 复制音频文件到output目录并重命名
print(f"File copied and renamed to: {new_path}") shutil.copy2(audio_path, new_path)
print(f"File copied and renamed to: {new_path}")
else:
print(f"File skipped due to duration: {audio_path}")
except Exception as e: except Exception as e:
print(f"An error occurred while processing: {audio_path}") print(f"An error occurred while processing: {audio_path}")
print(e) print(e)
@ -134,4 +157,4 @@ def copy_and_move(output_audio_directory, similarity_scores):
if __name__ == '__main__': if __name__ == '__main__':
similarity_list = parse_similarity_file("D:/tt/similarity/啊,除了伊甸和樱,竟然还有其他人会提起我?.txt") similarity_list = parse_similarity_file("D:/tt/similarity/啊,除了伊甸和樱,竟然还有其他人会提起我?.txt")
sample('D:/tt/similarity/output', similarity_list, 10, 4) sample('D:/tt/similarity/output', similarity_list, 10, 4)