Skip to content

Instantly share code, notes, and snippets.

@kiyoon
Created January 11, 2020 22:10
Show Gist options
  • Save kiyoon/f400b44f2535c75e8eecb3b2bf219f9b to your computer and use it in GitHub Desktop.
Save kiyoon/f400b44f2535c75e8eecb3b2bf219f9b to your computer and use it in GitHub Desktop.
Generate video augmentation data
#!/bin/bash
if [ $# -lt 2 ]
then
echo "usage: $0 [input_dir] [output_dir] [output_resolution=224x224]"
echo "Crops the video into 4 corners and 1 centre, and flips the video horizontally to do the same, resulting in 10 augmentation per video."
exit 1
fi
input_dir="$1"
output_dir="$2"
if [ $# -lt 3 ]
then
output_resolution=224x224
else
output_resolution="$3"
fi
output_width=$(echo "$output_resolution" | awk -F x '{print $1}')
output_height=$(echo "$output_resolution" | awk -F x '{print $2}')
mkdir -p "$output_dir"
videos=$(find "$input_dir" -type f -name "*.mp4")
while read line
do
output_file_base=$(echo "$line" | sed "s|$input_dir|$output_dir/|")
output_file_base="${output_file_base%.*}_aug"
output_folder=$(dirname "$output_file_base")
mkdir -p "$output_folder"
printf "$output_file_base"
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:0:0" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}000.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:in_w-$output_width:0" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}001.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:0:in_h-$output_height" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}002.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:in_w-$output_width:in_h-$output_height" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}003.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:(in_w-$output_width)/2:(in_h-$output_height)/2" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}004.mp4" < /dev/null 2> /dev/null
# Flip
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:0:0,hflip" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}005.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:in_w-$output_width:0,hflip" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}006.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:0:in_h-$output_height,hflip" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}007.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:in_w-$output_width:in_h-$output_height,hflip" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}008.mp4" < /dev/null 2> /dev/null
printf "#"
ffmpeg -i "$line" -vf "crop=$output_width:$output_height:(in_w-$output_width)/2:(in_h-$output_height)/2,hflip" -c:v libx264 -preset fast -crf 22 -c:a copy "${output_file_base}009.mp4" < /dev/null 2> /dev/null
echo
done <<< "$videos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment