Last active
March 25, 2020 17:08
-
-
Save jakabo27/1599dc144cdae6e43a6b4c8a44621854 to your computer and use it in GitHub Desktop.
Extract frames between X and Y from an MP4 using FFMPEG
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
#!/bin/bash -e | |
# To run: mp4_to_jpgs_frames.sh 00000123 "generated/SpaghettiJPEGS" 5 25 | |
if [ -z "$2" ]; then | |
output_dir=generated/unlabelled_jpgs | |
else | |
output_dir=$2 | |
fi | |
mkdir -p $output_dir | |
if [ -z "$3" ]; then | |
ffmpeg -y -i generated/mp4s/$1.mp4 -qscale:v 2 $output_dir/%5d.jpg > /dev/null 2>&1 < /dev/null | |
else | |
startFrame=$(($3)) | |
endFrame=$(($4)) | |
#Input -1 to extract to the end of the video. Make 10,000 a bigger number if you have longer videos. | |
if (( $endFrame < 0)); then | |
endFrame=10000 | |
fi | |
#echo $startFrame | |
#echo $endFrame | |
#Build the select statement because putting them in the command wasn't working | |
a="between(n\\," | |
b="\\," | |
c=")" | |
selectStatement=${a}$startFrame${b}$endFrame${c} | |
echo "Selecting "$selectStatement | |
ffmpeg -y -i generated/mp4s/$1.mp4 -qscale:v 2 -vf select=$selectStatement -vsync 0 $output_dir/$1" - "%5d.jpg > /dev/null 2>&1 < /dev/null | |
fi | |
echo "Frames extracted successfully" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment