Python+ffmpeg批量将m4a转mp3

from pathlib import Path
import subprocess

input_format = "m4a"
output_format = "mp3"

curPath = Path(__file__).parent

input_files = list(curPath.glob(f"*.{input_format}"))


def conversion(input_file: Path):
    output_filename = f"{input_file.stem}.{output_format}"
    print(f"[{input_file}] --> [{output_filename}]")
    process = subprocess.run(
        [
            'ffmpeg',
            '-y',
            '-i',
            input_file,
            curPath / output_filename,
            '-loglevel',
            'quiet',
        ]
    )
    return True if process.returncode == 0 else False


if __name__ == '__main__':
    for file in input_files:
        if conversion(file):
            print("done")
        else:
            print("fail!")
    print("all done")

系统中要有ffmpeg

理论上可以修改input_format和output_format来转换别的格式

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注