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)

相关推荐
多米Domi0111 分钟前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
饺子大魔王的男人1 分钟前
Remote JVM Debug+cpolar 让 Java 远程调试超丝滑
java·开发语言·jvm
人工智能训练5 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
yaoming1686 小时前
python性能优化方案研究
python·性能优化
兩尛6 小时前
c++知识点2
开发语言·c++
fengfuyao9856 小时前
海浪PM谱及波形的Matlab仿真实现
开发语言·matlab
xiaoye-duck6 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl
码云数智-大飞7 小时前
使用 Python 高效提取 PDF 中的表格数据并导出为 TXT 或 Excel
python
Hx_Ma167 小时前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet
biuyyyxxx8 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化