Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created November 13, 2021 08:05
Show Gist options
  • Save oberhamsi/377f0ea4910db62363e0d7d6d22744af to your computer and use it in GitHub Desktop.
Save oberhamsi/377f0ea4910db62363e0d7d6d22744af to your computer and use it in GitHub Desktop.
# convert video files in directory for chromecast classic
# if the existing video or audio format is not playable,
# the streams are converted to h.264 and aac
## make sure output is mp4 container or missing timestamps in avi (!) are a problem
SEARCHPATH="/home/simon/nas/movies/"
MOVIEREGEX=".*\.(mkv|avi|mp4)$"
BACKUP="/home/simon/backup-movies/"
readarray -d $'\0' movies < <(find "${SEARCHPATH}" -regextype egrep -iregex "${MOVIEREGEX}" -print0)
for movie in "${movies[@]}"; do
echo "checking ${movie}"
info=`ffprobe "${movie}" 2>&1`
videoC=`echo "${info}" | egrep -Eo Stream.*Video.* | grep -c h264`
audioC=`echo ${info} | egrep -Eo Stream.*Audio.* | grep -Ec "(aac|mp3|ac3)"`
if [ "${videoC}" == "0" ] || [ "${audioC}" == "0" ]; then
echo "converting ${movie}, video ok: ${videoC}, audio ok: ${audioC}"
extension="${movie##*.}"
convertedmovie="${movie}.converted.mp4"
if [ "${videoC}" == "0" ] && [ "${audioC}" == "0" ]; then
time ffmpeg -nostdin -loglevel quiet -i "$movie" -c:v libx264 -preset slow -c:a aac "${convertedmovie}"
elif [ "${videoC}" == "0" ]; then
time ffmpeg -nostdin -loglevel quiet -i "$movie" -c:v libx264 -preset slow -c:a copy "${convertedmovie}"
elif [ "${audioC}" == "0" ]; then
time ffmpeg -nostdin -loglevel quiet -i "$movie" -c:v copy -preset slow -c:a aac "${convertedmovie}"
fi
status=$?
if [ $status -eq 0 ]; then
touch -r "$movie" "$convertedmovie"
moviefile=${movie##*/}
mv "$movie" "${BACKUP}${moviefile}"
status=$?
if [ $status -eq 0 ]; then
mv "${convertedmovie}" "${movie}"
else
echo "error moving file to backup; did not overwrite on nas"
fi
else
echo "error"
if test -f "$convertedmovie"; then
rm "${convertedmovie}"
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment