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)

相关推荐
小小测试开发3 分钟前
pytest 库用法示例:Python 测试框架的高效实践
开发语言·python·pytest
BUG弄潮儿12 分钟前
go-swagger标准接口暴露
开发语言·后端·golang
至善迎风20 分钟前
把 Python 应用打包成 Mac 应用程序 — 完整指南
python·macos
数字化顾问34 分钟前
Flink ProcessFunction 与低层级 Join 实战手册:实时画像秒级更新系统
java·开发语言
qq_339191141 小时前
go win安装grpc-gen-go插件
开发语言·后端·golang
疯狂吧小飞牛1 小时前
Lua中,表、元表、对象、类的解析
开发语言·junit·lua
owCode1 小时前
3-C++中类大小影响因素
开发语言·c++
应用市场1 小时前
无人机编队飞行原理与Python仿真实现完整指南
python·无人机·cocos2d
兮动人1 小时前
Java 单元测试中的 Mockito 使用详解与实战指南
java·开发语言·单元测试
武子康2 小时前
Java-151 深入浅出 MongoDB 索引详解 性能优化:慢查询分析 索引调优 快速定位并解决慢查询
java·开发语言·数据库·sql·mongodb·性能优化·nosql