添加mp3转flac文件脚本

This commit is contained in:
yulinling 2025-08-25 22:40:47 +08:00
commit 5527384183
7 changed files with 105 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.venv
.idea

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/script_library.iml" filepath="$PROJECT_DIR$/.idea/script_library.iml" />
</modules>
</component>
</project>

10
.idea/script_library.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.13 (script_library)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,70 @@
import os
import argparse
from pydub import AudioSegment # 依赖 audioop-lts
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
def convert_mp3_to_flac(mp3_path: str, delete_original: bool = False):
filename = os.path.basename(mp3_path)
flac_path = mp3_path.replace(".mp3", ".flac")
# 如果目标文件已存在,跳过
if os.path.exists(flac_path):
return "skip", f"已存在,跳过: {flac_path}"
try:
audio = AudioSegment.from_mp3(mp3_path)
audio.export(flac_path, format="flac")
if delete_original:
os.remove(mp3_path)
return "success", f"转换完成并删除原文件: {flac_path}"
else:
return "success", f"转换完成: {flac_path}"
except Exception as e:
return "fail", f"转换失败: {mp3_path}, 错误: {e}"
def main():
parser = argparse.ArgumentParser(description="批量将 MP3 转换为 FLAC")
parser.add_argument("input_dir", help="输入的 mp3 文件夹路径")
parser.add_argument("-t", "--threads", type=int, default=4, help="线程数 (默认: 4)")
parser.add_argument("--delete", action="store_true", help="转换后删除原 mp3 文件")
args = parser.parse_args()
input_dir = args.input_dir
threads = args.threads
delete_original = args.delete
mp3_files = [
os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.lower().endswith(".mp3")
]
if not mp3_files:
print("未找到任何 mp3 文件。")
return
stats = {"success": 0, "skip": 0, "fail": 0}
with ThreadPoolExecutor(max_workers=threads) as executor:
futures = {
executor.submit(convert_mp3_to_flac, f, delete_original): f for f in mp3_files
}
for future in tqdm(as_completed(futures), total=len(futures), desc="转换进度", ncols=80):
status, msg = future.result()
print(msg)
if status in stats:
stats[status] += 1
# 统计总结
print("\n====== 转换总结 ======")
print(f"总文件数: {len(mp3_files)}")
print(f"成功: {stats['success']}")
print(f"跳过: {stats['skip']}")
print(f"失败: {stats['fail']}")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,3 @@
pydub~=0.25.1
tqdm~=4.67.1
audioop-lts~=0.2.2