Admin
import os
import subprocess
def audio_speed(in_file: str, speed: float):
""" change audio file speed
:param in_file: input file path
:param speed: speed
:return: None
sample command line:
>>ffmpeg -i 0.mp3 -filter:a "atempo=0.9, atempo=0.9" -vn 0.speed.0.9.mp3
"""
if not os.path.exists(in_file):
raise Exception('input file not exist')
name, ext = os.path.splitext(in_file)
out_file = f'{name}.speed.{speed}.{ext}'
if os.path.exists(out_file):
os.remove(out_file)
cmd = f'ffmpeg -i {in_file} -filter:a "atempo={speed}, atempo={speed}" -vn {out_file}'
print(cmd)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
# print(line.decode())
continue
if not os.path.exists(out_file):
raise Exception(f'音频变速失败:{in_file}')
return p.wait()
if __name__ == '__main__':
for i in range(90, 100):
speed = i / 100
audio_speed('0.mp3', speed)