#!/bin/bash -e if ! ffmpeg -version | head -n1 then echo Missing ffmpeg exit 1 fi if ! test -f "$1" then echo Missing RAW file to encode to MP4 exit fi input="$1" eval $(ffprobe -v 0 -of flat=s=_ -show_format "$input") # neat trick to get video variables into shell eval $(ffprobe -v 0 -of flat=s=_ -show_streams "$input") if test "$format_tags_creation_time" # try get date from video's metadata then today=$(echo $format_tags_creation_time | awk '{print $1}' | cut -c-10) fi output=~/Documents/$(date +%Y-%m-%d)/$(basename "${input%.*}").mp4 if test -f "${output}" then echo 🎥 ${output} already exists exit fi mkdir -p "$(dirname $output)" &>/dev/null || true if ! FFREPORT=file=/tmp/htmlvideo.log:level=32 ffmpeg -y -i "${input}" -movflags +faststart -pix_fmt yuv420p -c:v libx264 -acodec aac "${output}" &> /dev/null then rm -f "${output}" echo Error encoding "${input}", see /tmp/htmlvideo.log for details exit 1 fi test "$format_duration" && printf "🎥 Duration: %.1f seconds\n" $format_duration function size { echo $(du -h "${1}" | tr '\n\t' ' ') } echo -n "🎥 Input: $(size "${input}") Output: $(size "${output}")" # Space savings https://en.wikipedia.org/wiki/Data_compression_ratio printf " Space savings: %.0f%%\n" $(bc <<< "scale=2; (1 - $(wc -c < "${output}") / $(wc -c < "${input}") ) * 100")