Created
March 22, 2020 03:22
-
-
Save leggitta/f889ed6a1179aff322c776627e3c2cf1 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import cv2\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"infile = '/Users/misterbrainley/Movies/clucky.mov'\n", | |
"outfile = 'clucky.gif'\n", | |
"prefix = 'tmp_makegif'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"shrink = 6\n", | |
"frame_rate = 7\n", | |
"start_frame = 3\n", | |
"stop_frame = 57" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# get file dimensions\n", | |
"vf = cv2.VideoCapture(infile)\n", | |
"h = vf.get(cv2.CAP_PROP_FRAME_HEIGHT)\n", | |
"w = vf.get(cv2.CAP_PROP_FRAME_WIDTH)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# create an image stack using ffmpeg\n", | |
"cmd = 'ffmpeg -i %s -s %dx%d -r %d -f image2 %s_%%04d.png'\n", | |
"os.system(cmd % (infile, w//shrink, h//shrink, frame_rate, prefix))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# determine number of output images\n", | |
"files = os.listdir('.')\n", | |
"n_files = len(list(filter(lambda s: prefix in s, files))) + 1\n", | |
"if stop_frame is None:\n", | |
" stop_frame = n_files" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# create gif using image magick\n", | |
"cmd = 'convert -delay 1x%d `seq -f %s_%%04g.png %d 1 %d` -coalesce -layers OptimizeTransparency %s'\n", | |
"os.system(cmd % (frame_rate, prefix, start_frame, stop_frame, outfile))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# cleanup png files\n", | |
"files = os.listdir('.')\n", | |
"for f in filter(lambda s: prefix in s, files):\n", | |
" os.remove(f)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment