Created
June 20, 2017 23:24
-
-
Save TimothyRHuertas/b22e1a252447ab97aa0f8de7c65f96b8 to your computer and use it in GitHub Desktop.
Use ffmpeg to center crop and scale an image to 1:1
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
#Given a video of any aspect ratio this script will extract center cropped thumbnails at 299x299. | |
#Useful for gathering image training data. | |
#Assumes ffmpeg 3.3.2 | |
#If you get an error about exact that means you are using an older version of ffmpeg | |
#Simply remove the :exact=1. | |
#This will make it work, but may cause the output size to be off by a pixel | |
#Script assumes you are downsampling. | |
#!/bin/bash | |
PATH_TO_VID=myvideo.mov | |
PATH_TO_OUT_DIR=out | |
ffmpeg -i "$PATH_TO_VID" -y -an -q 0 -vf scale="'if(gt(iw,ih),-1,299):if(gt(iw,ih),299,-1)', crop=299:299:exact=1" $PATH_TO_OUT_DIR/%06d.jpg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That should be what the script does. Just change the OUTPUT_WIDTH to 1920. To be clear that will cause some upscaling since the extra 280 (1080-800) pixels (height) have to come from somewhere. If you don’t want to scale your only other option is letterboxing.