Skip to content

Instantly share code, notes, and snippets.

@jborbik
Last active July 22, 2021 05:38
Show Gist options
  • Select an option

  • Save jborbik/dc5e12ae73a8e769254a7803b2da7824 to your computer and use it in GitHub Desktop.

Select an option

Save jborbik/dc5e12ae73a8e769254a7803b2da7824 to your computer and use it in GitHub Desktop.
Speed up videos (x20)
'''
Files are saved in the same folder with suffix *_x<speedup_rate> and MP4 extension (this leads to faster processing)
'''
import os
import glob
import subprocess
from multiprocessing import Pool
import sys
speedup_rate = 20
path_pattern = os.path.expanduser(sys.argv[1] + "/**/*.webm")
def speed_up_file(file_path):
if f'x{speedup_rate}' in file_path:
return
file_name = os.path.basename(file_path)
folder_name = os.path.dirname(file_path)
new_file_path = os.path.join(folder_name, file_name.replace(".webm", f"_x{speedup_rate}.mp4"))
print('new_file_path', new_file_path)
subprocess.run(f"ffmpeg -i '{file_path}' -filter:v 'setpts={1/speedup_rate}*PTS' -filter:a 'atempo={speedup_rate}' '{new_file_path}'", shell=True, check=True)
file_list = glob.glob(path_pattern, recursive=True)
print(file_list)
pool = Pool(os.cpu_count())
pool.map(speed_up_file, file_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment