From f61a723babbf5580b59cf5652009509639d9c6ca Mon Sep 17 00:00:00 2001 From: Downupanddownup Date: Thu, 25 Apr 2024 16:45:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A03s=E8=87=B310s=E7=9A=84?= =?UTF-8?q?=E9=9F=B3=E9=A2=91=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ref_Audio_Selector/tool/audio_similarity.py | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Ref_Audio_Selector/tool/audio_similarity.py b/Ref_Audio_Selector/tool/audio_similarity.py index 962f85d..afb6a52 100644 --- a/Ref_Audio_Selector/tool/audio_similarity.py +++ b/Ref_Audio_Selector/tool/audio_similarity.py @@ -1,8 +1,27 @@ import os import shutil 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): # 创建输出目录,如果它不存在的话 @@ -38,9 +57,13 @@ def convert_from_list(list_file, output_dir): print(f"Audio file does not exist: {audio_path}") continue - # 复制音频文件到output目录并重命名 - shutil.copy2(audio_path, new_path) - print(f"File copied and renamed to: {new_path}") + if check_audio_duration(audio_path): + # 复制音频文件到output目录并重命名 + 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: print(f"An error occurred while processing: {audio_path}") print(e) @@ -134,4 +157,4 @@ def copy_and_move(output_audio_directory, similarity_scores): if __name__ == '__main__': similarity_list = parse_similarity_file("D:/tt/similarity/啊,除了伊甸和樱,竟然还有其他人会提起我?.txt") - sample('D:/tt/similarity/output', similarity_list, 10, 4) \ No newline at end of file + sample('D:/tt/similarity/output', similarity_list, 10, 4)