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輸出目錄路徑,通過調(diào)用FFmpeg工具實(shí)現(xiàn)格式轉(zhuǎ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)文章
解決import tensorflow as tf 出錯(cuò)的原因
這篇文章主要介紹了解決import tensorflow as tf 出錯(cuò)的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
python用tkinter實(shí)現(xiàn)一個(gè)簡易能進(jìn)行隨機(jī)點(diǎn)名的界面
這篇文章主要介紹了python用tkinter實(shí)現(xiàn)一個(gè)簡易能進(jìn)行隨機(jī)點(diǎn)名的界面,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
linux 下實(shí)現(xiàn)python多版本安裝實(shí)踐
這篇文章主要介紹了linux 下實(shí)現(xiàn)python多版本安裝實(shí)踐,需要的朋友可以參考下2014-11-11
python3 os進(jìn)行嵌套操作的實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于python3 os進(jìn)行嵌套操作的實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-11-11
Python?數(shù)據(jù)可視化超詳細(xì)講解折線圖的實(shí)現(xiàn)
數(shù)據(jù)可以幫助我們描述這個(gè)世界、闡釋自己的想法和展示自己的成果,但如果只有單調(diào)乏味的文本和數(shù)字,我們卻往往能難抓住觀眾的眼球。而很多時(shí)候,一張漂亮的可視化圖表就足以勝過千言萬語,讓我們來用Python實(shí)現(xiàn)一個(gè)可視化的折線圖2022-03-03
python3 QT5 端口轉(zhuǎn)發(fā)工具兩種場(chǎng)景分析
這篇文章主要介紹了python3 QT5 端口轉(zhuǎn)發(fā)工具,功能是打開本機(jī)端口,映射到指定IP的端口,接下來通過兩種場(chǎng)景給大家詳細(xì)介紹,感興趣的朋友一起看看吧2022-01-01
Python利用Selenium實(shí)現(xiàn)簡單的中英互譯功能
Selenium 是一個(gè)用于 Web 應(yīng)用程序測(cè)試的工具,最初是為網(wǎng)站自動(dòng)化測(cè)試而開發(fā)的,可以直接運(yùn)行在瀏覽器上,是 Python 的一個(gè)第三方庫,對(duì)外提供的接口能夠操作瀏覽器,從而讓瀏覽器完成自動(dòng)化的操作,本文介紹了如何利用Python中的Selenium實(shí)現(xiàn)簡單的中英互譯2024-08-08

