# -*- coding:utf-8 -*- """ 拖单个音频倍速 """ import os import re import subprocess import sys def get_files(path): res = [] r = os.listdir(path) # 列出文件夹下所有的目录与文件 for i in r: file_path = os.path.join(path, i) if os.path.isfile(file_path) and file_path.endswith('mp3'): if re.match(r'.*x[\d\.]+\.mp3$', file_path): continue res.append(file_path) return res def a_speed(input_file, speed, out_file): try: cmd = "ffmpeg.exe -y -i %s -filter_complex \"atempo=tempo=%s\" %s" % (input_file, speed, out_file) res = subprocess.call(cmd, shell=True) if res != 0: return False return True except Exception: return False def change_speed(file, x, error_info): res = a_speed(file, x, re.sub(r'(\.mp3$)', f'_x{x}.mp3', file)) if not res: error_info.append(f'{file} to {x} error') def handler_file(file): error_info = [] change_speed(file, '1.5', error_info) change_speed(file, '3', error_info) print('*' * 20) for i in error_info: print(i) def handler_dir(root_dir): files = get_files(root_dir) error_info = [] for file in files: change_speed(file, '1.5', error_info) change_speed(file, '3', error_info) print('*' * 20) for i in error_info: print(i) def run(path): if os.path.isfile(path): handler_file(path) else: handler_dir(path) if __name__ == "__main__": path = sys.argv[1] run(path)