Created
May 31, 2020 13:50
-
-
Save sybrex/357c4c1c2ffff86314bc71f307dc4a71 to your computer and use it in GitHub Desktop.
Generate timelapse video from images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import sys | |
import subprocess | |
import shutil | |
TEMP_DIR = './temp' | |
OUTPUT = 'output.mp4' | |
FRAME_RATE = 10 | |
if len(sys.argv) < 3: | |
print('Specify source dir') | |
print('Specify images extension') | |
sys.exit() | |
SOURCE_DIR = sys.argv[1] | |
EXT = sys.argv[2] | |
if os.path.exists(TEMP_DIR): | |
shutil.rmtree(TEMP_DIR) | |
if os.path.exists(OUTPUT): | |
os.remove(OUTPUT) | |
print('===== Preparing images ======') | |
os.mkdir(TEMP_DIR) | |
images = sorted(list(set(os.listdir(SOURCE_DIR)))) # order list without duplicates | |
for index, image in enumerate(images, start=1): | |
if image.split('.')[-1] == EXT: | |
shutil.copyfile(f'{SOURCE_DIR}/{image}', f'{TEMP_DIR}/{index}.{EXT}') | |
print('===== Generating video =====') | |
# ffmpeg -framerate 25 -i image-%05d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4 | |
subprocess.check_call([ | |
'ffmpeg', | |
'-framerate', str(FRAME_RATE), | |
'-i', f'{TEMP_DIR}/%000d.{EXT}', | |
'-c:v', 'libx264', | |
'-profile:v', 'high', | |
'-crf', '20', | |
'-pix_fmt', 'yuv420p', | |
f'./{OUTPUT}' | |
]) | |
print('===== Cleanup =====') | |
shutil.rmtree(TEMP_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment