Last active
November 21, 2016 23:19
-
-
Save dmyersturnbull/f0ae8a871ba2a164eb81c5ba0ae438ba to your computer and use it in GitHub Desktop.
Run ffmpeg to generate a libx264 .mp4.
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
| # Douglas Myers-Turnbull wrote this for the Kokel Lab, which has released it under the Apache Software License, Version 2.0 | |
| # See the license file here: https://gist.github.com/dmyersturnbull/bfa1c3371e7449db553aaa1e7cd3cac1 | |
| # The list of copyright owners is unknown | |
| from plumbum import local | |
| import os | |
| def make_video_from_frames(input_dir: str, video_path: str, framerate: str='10/1', ffmpeg_path: str='ffmpeg', input_image_extension: str='.jpg') -> None: | |
| """Runs ffmpeg on all selected files in a directory to generates an x264-encoded video in an MP4 container. | |
| Warnings: | |
| - Make sure that the frames are sorted in the correct order. | |
| - Overwrites the output video file without warning. | |
| video_path: the full path of the video file, which MUST end in .mp4 | |
| framerate: a rational number in the format a/b (ex: 4/5 is 4 frames per every 5 seconds) | |
| input_image_extension: The extension of the images in the directory to use as input; must start with a dot (.) | |
| """ | |
| assert input_image_extension.startswith('.') | |
| assert video_path.endswith('.mp4') | |
| ffmpeg = local[ffmpeg_path] | |
| ffmpeg['-r', framerate, | |
| '-y', # overwrites | |
| '-pattern_type', 'glob', | |
| '-i', os.path.join(input_dir, '*' + input_image_extension), | |
| '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2', # needed because: https://stackoverflow.com/questions/24961127/ffmpeg-create-video-from-images | |
| '-c:v', 'libx264', | |
| '-pix_fmt', 'yuv420p', # needed because most video players can only use the 264 pixel format, but it's not default | |
| video_path]() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment