Skip to content

Instantly share code, notes, and snippets.

@thegabriele97
Last active July 23, 2022 13:53
Show Gist options
  • Save thegabriele97/9e9142aefbe01c85de76437f216932ad to your computer and use it in GitHub Desktop.
Save thegabriele97/9e9142aefbe01c85de76437f216932ad to your computer and use it in GitHub Desktop.
Script to compress all mp4 files in a folder
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <input_type> <output_type>"
exit 1
fi
tinput=$1
toutput=$2
# create a new directory for the output if it doesn't exist
if [ ! -d "output" ]; then
mkdir output
fi
# create a new directory for processed file, if it doesn't exist
if [ ! -d "processed" ]; then
mkdir processed
fi
# for each mp4 file in the current directory
for file in *.${tinput}
do
# get the file name without the extension
filename=$(basename "$file" .$tinput)
# apply conversion
echo "Working on $file .."
ffmpeg -i $file -movflags use_metadata_tags -vcodec libx265 -crf 30 ${filename}_compressed.$toutput
retVal=$?
if [ $retVal -ne 0 ]; then
echo "Error"
exit $retVal
fi
# cp $file ${filename}_compressed.mp4
mod_base=$(stat $file | head -n6 | tail -1)
date=$(echo $mod_base | cut -d ' ' -f2 | sed 's/-//g')
time_mod=$(echo $mod_base | cut -d ' ' -f3 | cut -d '.' -f1 | sed 's/://g')
time=$(echo ${time_mod:0:4})
touch -t $date$time golden.dat
exiftool -addTagsFromFile $file -All:All ${filename}_compressed.$toutput
touch -r golden.dat ${filename}_compressed.$toutput
rm golden.dat
# move the file to the processed directory
mv ${file} processed/
# move the file to the output directory
mv ${filename}_compressed.$toutput output/
# remove original file (exiftool's collateral output)
rm ${filename}_compressed.${toutput}_original
# break
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment