Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2014 19:33
Show Gist options
  • Save anonymous/090fad48a548bbc9549c to your computer and use it in GitHub Desktop.
Save anonymous/090fad48a548bbc9549c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# This is a script to rename movies files
# from: Edge of Tomorrow [2014 BluRay].mp4
# to: Edge of Tomorrow [2014 720p].mp4
##############
# EDIT THESE #
##############
FILE_EXTENSIONS=(axv dl dif dv fli gl mpeg mpg mpe ts qt mov ogv webm mxu flv lsf lsx mng asf asx wm wmv wmx wvx avi movie mpv mkv)
PATH_TO_MOVIES="/mnt/media/Movies"
##############################
# This function turns an integer resolution
# into a human-readable 720p or 1080p
height_to_res() {
height=$1
if (( $height > 900 )); then
echo "1080p"
elif (( $height > 500 )); then
echo "720p"
fi
}
# Using the above extension list, we build an expression
# to find files with those endings
find_exp='-iname "*.'
find_exp=${find_exp}${FILE_EXTENSIONS[0]}'"'
for ext in ${FILE_EXTENSIONS[@]:1}; do
find_exp=${find_exp}' -o -iname "*.'${ext}'"'
done
eval find "$PATH_TO_MOVIES" -type f '\(' $find_exp '\)' | while read fullfile; do
# Use `mediainfo` to find height in pixels
height=$( mediainfo --Inform="Video;%Height%" "$fullfile" )
# Use function above to convert this integer to 720p/1080p
res=$( height_to_res $height )
# extract original directory name
dir=$( dirname "$fullfile" )
# extract original filename
orig_filename=$( basename "$fullfile" )
# extract original extension
ext="${orig_filename##*.}"
# look int the original filename for a year
# i.e. something that starts with 1 or 2, followed by 3 more digits
year=$( echo "$orig_filename" | grep -oE '[1,2][0-9]{3}' | tail -n 1 )
# find the name of the movie
# i.e. everything up to a [ or . character
name=$( echo "$orig_filename" | grep -oE '^[^[\.]+' )
# Trim tailing whitespace
name=${name%%+( )}
new_path="$name"
if [ -n "$year" ] && [ -n "$res" ]; then
new_path="${new_path} [${year} ${res}]"
elif [ -n "$year" ]; then
new_path="${new_path} [${year}]"
elif [ -n "$res" ]; then
new_path="${new_path} [${res}]"
fi
# add extenstion
new_path="${new_path}.${ext}"
# add directory
new_path="${dir}/${new_path}"
echo "----------------------"
echo " Found movie file"
echo "----------------------"
echo "Full path: $fullfile"
echo "In directory: $dir"
echo "Extension: $ext"
echo ""
echo "Movie name: ${name:-NOT FOUND!!}"
echo "Year: ${year:-None}"
echo "Resolution: ${res:-None}"
echo ""
echo "Will rename to:"
echo "$new_path"
echo "----------------------"
echo ""
echo ""
############################################
## UNCOMMENT THE FOLLOWING TO RENAME FILES #
## THIS IS NOT A DRILL #
############################################
#mv "$fullfile" "$new_path"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment