Admin

Python使用ffmpeg对音频调速
2018年11月2日 10:38 1 0 0 0
  1. import os
  2. import subprocess
  3. def audio_speed(in_file: str, speed: float):
  4. """ change audio file speed
  5. :param in_file: input file path
  6. :param speed: speed
  7. :return: None
  8. sample command line:
  9. >>ffmpeg -i 0.mp3 -filter:a "atempo=0.9, atempo=0.9" -vn 0.speed.0.9.mp3
  10. """
  11. if not os.path.exists(in_file):
  12. raise Exception('input file not exist')
  13. name, ext = os.path.splitext(in_file)
  14. out_file = f'{name}.speed.{speed}.{ext}'
  15. if os.path.exists(out_file):
  16. os.remove(out_file)
  17. cmd = f'ffmpeg -i {in_file} -filter:a "atempo={speed}, atempo={speed}" -vn {out_file}'
  18. print(cmd)
  19. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  20. for line in p.stdout.readlines():
  21. # print(line.decode())
  22. continue
  23. if not os.path.exists(out_file):
  24. raise Exception(f'音频变速失败:{in_file}')
  25. return p.wait()
  26. if __name__ == '__main__':
  27. for i in range(90, 100):
  28. speed = i / 100
  29. audio_speed('0.mp3', speed)
发布内容,请遵守相关法律法规。
评论