当前位置: 首页 > news >正文

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

需要提前安装 FFmpeg


import os
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor, as_completeddef 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 = 0processed_files = 0converted_files = 0with 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 += 1input_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 += 1if "Converted" in result:converted_files += 1print(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)

http://www.lryc.cn/news/444879.html

相关文章:

  • 考研数据结构——C语言实现折半查找
  • 【游戏引擎】C++自制游戏引擎 Lunar Game Engine
  • 使用【Sa-Token】实现Http Basic 认证
  • layui table中的checkbox禁用问题
  • 102.SAPUI5 sap.ndc.BarcodeScannerButton调用摄像头时,localhost访问正常,使用IP访问失败
  • 20240923软考架构-------软考186-190答案解析
  • 基于Spring Boot的宠物咖啡馆平台【附源码】
  • C++模拟实现list:list、list类的初始化和尾插、list的迭代器的基本实现、list的完整实现、测试、整个list类等的介绍
  • Offer60:n个骰子的点数
  • 几种常见的索引类型扫描
  • 苹果CMS插件:优化蜘蛛访问内容,提升百度收录率
  • 后端开发刷题 | 没有重复项数字的全排列
  • Python中的“打开与关闭文件”:从入门到精通
  • 9.23 My_string.cpp
  • 【android10】【binder】【3.向servicemanager注册服务】
  • Java — LeetCode 面试经典150题(一)
  • Python酷玩之旅_mysql-connector
  • 7.搭建个人金融数据库之快速获取股票列表和基本信息!
  • Nginx基础详解1(单体部署与集群部署、负载均衡、正反代理、nginx安装)
  • 等保一体机如何帮你应对网络攻击
  • CVE-2024-1112 Resource Hacker 缓冲区溢出分析
  • WebGL渲染与创建2D内容
  • ArcGIS Desktop使用入门(三)图层右键工具——拓扑(下篇:地理数据库拓扑)
  • LeetCode题练习与总结:二叉树的最近公共祖先--236
  • uni-app 多环境配置
  • 【d48】【Java】【力扣】LCR 123. 图书整理 I
  • 【MySQL】InnoDB 索引为什么使用B+树而不用跳表?
  • 【学习笔记】TLS/SSL握手之Records
  • 【MySQL】创建新账号新数据库并授权
  • Nginx反向代理简介,作用及配置;Nginx负载均衡简介,作用及配置;