python 将 aac 转为 mp3,保持原有目录结构

需要提前安装 FFmpeg

python 复制代码
import os
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

def convert_file(input_path, output_path):
    command = [
        'ffmpeg',
        '-y',  # 自动覆盖现有文件
        '-i', input_path,
        '-acodec', 'libmp3lame',
        '-b:a', '192k',
        output_path
    ]
    try:
        subprocess.run(command, check=True, stderr=subprocess.PIPE, timeout=300)  # 5分钟超时
        return f"Converted: {output_path}"
    except subprocess.CalledProcessError as e:
        return f"Error converting {input_path}: {e.stderr.decode()}"
    except subprocess.TimeoutExpired:
        return f"Timeout converting {input_path}"

def convert_aac_to_mp3(input_dir, output_dir):
    start_time = time.time()
    total_files = 0
    processed_files = 0
    converted_files = 0

    with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
        futures = []

        for root, _, files in os.walk(input_dir):
            for filename in files:
                if filename.lower().endswith('.aac'):
                    total_files += 1
                    input_path = os.path.join(root, filename)
                    rel_path = os.path.relpath(root, input_dir)
                    output_filename = os.path.splitext(filename)[0] + '.mp3'
                    output_path = os.path.join(output_dir, rel_path, output_filename)
                    
                    os.makedirs(os.path.dirname(output_path), exist_ok=True)
                    
                    futures.append(executor.submit(convert_file, input_path, output_path))

        for future in as_completed(futures):
            result = future.result()
            print(result)
            processed_files += 1
            if "Converted" in result:
                converted_files += 1
            print(f"Progress: {processed_files}/{total_files} files processed")

    end_time = time.time()
    print(f"\nConversion completed.")
    print(f"Total files: {total_files}")
    print(f"Converted files: {converted_files}")
    print(f"Failed conversions: {total_files - converted_files}")
    print(f"Total time: {end_time - start_time:.2f} seconds")

使用脚本

input_dir = input("请输入包含 AAC 文件的目录路径: ")

output_dir = input("请输入 MP3 文件的输出目录路径: ")

convert_aac_to_mp3(input_dir, output_dir)

相关推荐
故事和你913 分钟前
sdut-python-实验四-python序列结构(21-27)
大数据·开发语言·数据结构·python·算法
SuperEugene7 分钟前
TypeScript+Vue 实战:告别 any 滥用,统一接口 / Props / 表单类型,实现类型安全|编码语法规范篇
开发语言·前端·javascript·vue.js·安全·typescript
chushiyunen10 分钟前
pycharm注意力残差示例
ide·python·pycharm
liuyao_xianhui11 分钟前
优选算法_模拟_提莫攻击_C++
开发语言·c++·算法·动态规划·哈希算法·散列表
2301_7938046918 分钟前
用Python和Twilio构建短信通知系统
jvm·数据库·python
B站_计算机毕业设计之家21 分钟前
计算机毕业设计:Python当当网图书数据全链路处理平台 Django框架 爬虫 Pandas 可视化 大数据 大模型 书籍(建议收藏)✅
爬虫·python·机器学习·django·flask·pandas·课程设计
不要秃头的小孩29 分钟前
力扣刷题——111.二叉树的最小深度
数据结构·python·算法·leetcode
.select.30 分钟前
c++ 移动赋值/移动构造函数
开发语言·c++
我是鶸41 分钟前
secml-malware python library 源码分析及实践
开发语言·python
setmoon2141 小时前
C++代码规范化工具
开发语言·c++·算法