python如何將aac轉(zhuǎn)為mp3,保持原有目錄結(jié)構(gòu)
更新時(shí)間:2024年11月07日 09:29:42 作者:nongcunqq
使用Python腳本實(shí)現(xiàn)AAC格式轉(zhuǎn)MP3格式的方法介紹,需要用戶輸入AAC文件所在目錄路徑和MP3輸出目錄路徑,通過(guò)調(diào)用FFmpeg工具實(shí)現(xiàn)格式轉(zhuǎn)換,該腳本簡(jiǎn)單易懂,適合需要批量處理音頻文件的用戶,使用前需確保已安裝FFmpeg環(huán)境
將aac轉(zhuǎn)為mp3,保持原有目錄結(jié)構(gòu)
需要提前安裝FFmpeg
import os
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
def convert_file(input_path, output_path):
command = [
'ffmpeg',
'-y', # 自動(dòng)覆蓋現(xiàn)有文件
'-i', input_path,
'-acodec', 'libmp3lame',
'-b:a', '192k',
output_path
]
try:
subprocess.run(command, check=True, stderr=subprocess.PIPE, timeout=300) # 5分鐘超時(shí)
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("請(qǐng)輸入包含 AAC 文件的目錄路徑: ")
output_dir = input("請(qǐng)輸入 MP3 文件的輸出目錄路徑: ")
convert_aac_to_mp3(input_dir, output_dir)總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
用Python做個(gè)個(gè)性的動(dòng)畫(huà)掛件讓桌面不單調(diào)
這篇文章主要介紹了如何用Python做個(gè)個(gè)性的動(dòng)畫(huà)掛件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Scrapy爬蟲(chóng)多線程導(dǎo)致抓取錯(cuò)亂的問(wèn)題解決
本文針對(duì)Scrapy爬蟲(chóng)多線程導(dǎo)致抓取錯(cuò)亂的問(wèn)題進(jìn)行了深入分析,并提出了相應(yīng)的解決方案,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
Blender Python編程創(chuàng)建發(fā)光材質(zhì)示例詳解
這篇文章主要為大家介紹了Blender Python編程創(chuàng)建發(fā)光材質(zhì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
python requests抓取one推送文字和圖片代碼實(shí)例
這篇文章主要介紹了python requests抓取one推送文字和圖片代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python安裝requests庫(kù)的實(shí)例代碼
在本篇文章中小編給大家分享了關(guān)于python怎么安裝requests庫(kù)的知識(shí)點(diǎn)以及代碼內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-06-06
python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

