-
-
Save zarmstrong/e407e0f4ae37cc0aa286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/path" | |
############################## | |
# This function turns an integer resolution | |
# into a human-readable 720p or 1080p | |
height_to_res() { | |
height=$1 | |
if (( $height == 1080 )); then | |
echo "1080" | |
elif (( $height == 720 )); then | |
echo "720" | |
elif (( $height > 1000 )); then | |
echo "1k HDTV" | |
elif (( $height > 500 )); then | |
echo "HDTV" | |
else | |
echo "unknown" | |
fi | |
} | |
scanType() { | |
scan=$1 | |
if (( $scan == "Progressive" )); then | |
echo "p" | |
else | |
echo "i" | |
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 ) | |
scan=$( mediainfo --Inform="Video;%ScanType%" "$fullfile" ) | |
scanType=$(scanType $scan) | |
# 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}${scanType}]" | |
elif [ -n "$year" ]; then | |
new_path="${new_path} [${year}]" | |
elif [ -n "$res" ]; then | |
new_path="${new_path} [${res}${scanType}]" | |
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 "Scan Type: ${scanType:-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