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)

相关推荐
mosquito_lover15 分钟前
使用Python SciPy库来计算矩阵的RCS特征值并生成极坐标图
python·矩阵·scipy
weixin_535854226 分钟前
快手,蓝禾,优博讯,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮,高途教育25届春招内推
java·前端·python·算法·硬件工程
程序员的世界你不懂13 分钟前
页面对象实现自动化测试,playwright框架
python·测试工具·microsoft·单元测试
PythonFun24 分钟前
Python如何制作并查询sql数据库
数据库·python·sql
落落落sss36 分钟前
分布式日志和责任链路
java·运维·开发语言·后端·jenkins
C_V_Better36 分钟前
Java 导出 PDF 文件:从入门到实战
java·开发语言·算法·pdf
Sinsa_SI44 分钟前
2024年12月中国电子学会青少年软件编程(Python)等级考试试卷(六级)答案 + 解析
python·等级考试·电子学会·真题·答案·六级
zjoy_22331 小时前
【数据结构】什么是栈||栈的经典应用||分治递归||斐波那契问题和归并算法||递归实现||顺序栈和链栈的区分
java·c语言·开发语言·数据结构·c++·算法·排序算法
kfepiza1 小时前
Python的循环和条件判断 笔记250303
开发语言·笔记·python
川石课堂软件测试2 小时前
涨薪技术|持续集成Git使用详解
开发语言·javascript·git·python·功能测试·ci/cd·单元测试