Created
January 11, 2021 16:08
-
-
Save jsundram/c73c5d1f7265a95c3860a4efa87899fb to your computer and use it in GitHub Desktop.
turn a timelapse into a sliced timelapse
This file contains hidden or 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
from skimage import io | |
import argparse | |
import glob | |
import numpy as np | |
import os | |
def make_frames(files, outdir, n=8, prefix="composite"): | |
split = int(len(files) / n) | |
pathstart = os.path.join(outdir, prefix) | |
for i in range(split): | |
print("%d / %d ~ %2.3f%%" % (i+1, split, 100.0* (i+1) / split)) | |
imgs = files[i:len(files):split] | |
img = compose(imgs) | |
io.imsave("%s-%04d.png" % (pathstart, i), img) | |
def compose(filenames): | |
imgs = [io.imread(f) for f in filenames] | |
h, w, _ = imgs[0].shape # (2060, 3662, 3) | |
pxw = int(w / len(filenames)) | |
canvas = np.zeros_like(imgs[0]) | |
x = 0 | |
for i, img in enumerate(imgs): | |
canvas[:,x:x+pxw,:] = img[:,x:x+pxw,:] | |
x += pxw | |
return canvas | |
def main(indir, outdir): | |
"""assumes `ffmpeg -r 1 -i file.mp4 -r 1 "lapse/title-%04d.png` | |
has already been run to dump the frames out. | |
""" | |
os.makedirs(outdir, exist_ok=True) | |
files = sorted(glob.glob(indir + "/*")) | |
make_frames(files, outdir) | |
print("""turn this into a movie playable on iphone via: | |
ffmpeg -i out/composite-%d.png -s 1920x1080 -c:v libx264 -pix_fmt yuv420p -profile:v baseline -level 4.2 -crf 15 -movflags +faststart output.mp4 | |
""") | |
if __name__ == '__main__': | |
ap = argparse.ArgumentParser() | |
ap.add_argument("-i", "--input_directory", required=False, | |
default="/Users/jsundram/Downloads/lapse", | |
help="Path to lapse images") | |
ap.add_argument("-o", "--output_directory", required=False, | |
default="/Users/jsundram/Downloads/out", | |
help="Path to store output images") | |
args = vars(ap.parse_args()) | |
main(args['input_directory'], args['output_directory']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment