Reads the metadata from inside an .mp4/.m4v file, updates the file name with "Title (Resolution)", and adds the resolution as a comment — allowing you to create a smart playlist in players which support it (e.g., iTunes). Also logs the status to the system log (for watching progress on large jobs).
#! /usr/bin/env bash
# The MIT License (MIT)
#
# Copyright (c) 2014 Ryan Parman <http://ryanparman.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# PREREQUISITES:
# 1. You're not afraid of the big bad Terminal.
# 2. You've installed the Xcode Command Line Tools.
# 3. You've installed the "mp4v2" package from either Homebrew or MacPorts.
# 4. You've installed "SublerCLI" into /usr/local/bin.
# 5. This will ONLY work on .mp4 or .m4v files. If you have another format, you need to convert it first.
# INSTRUCTIONS:
# 1. Launch Automator.
# 2. Create a new "Service".
# 3. Drag the "Get Specified Finder Items" action into the main action area.
# 4. Drag the "Run Shell Script" action into the main action area.
# 4a. Make sure the Shell dropdown says "/bin/bash".
# 4b. Copy-paste this entire script in as the contents.
# 5. Drag the "Hide Extensions" action into the main action area.
# 5a. Make sure "Include files inside folders" is checked.
# 6. Save. Choose a name like "Movie • Title (Resolution)".
# 7. Right-click on one or more .mp4/.m4v files, choose Services => Movie • Title (Resolution).
# 8. If the movie is already in iTunes, simply choose "Get Info", then click OK to reload the metadata.
# 8a. If you're updating several movies, select them, choose "Get Info", go to the "Options" tab,
# check/choose "Media Kind: Movie", and click OK. This will force iTunes to reload the metadata
# for all of the selected movies.
# 9. Create an iTunes smart playlist where the comment field contains "1080p", "720p" or "480p".
# BONUS:
# If you want to watch the progress of the movie processing, open Console.app, and filter by the word "Movies".
# The log messages will be written there as each movie file completes.
#------------------------------------------------------------------------------#
# syslog wrapper
function log() {
/usr/bin/syslog -s -C -l error "Movies: $1 ($2)"
}
# SublerCLI wrapper
function subler() {
/usr/local/bin/SublerCLI "$1" -t "{Comments:$2}"
}
for f in "$@"
do
# Where are we?
DIRNAME=`/usr/bin/dirname "$f"`
# What is the movie title (from the metadata)?
TITLE=`/opt/local/bin/mp4info "$f" | grep --max-count 1 "Name:" | awk -F"Name: " '{ print $2 }' | sed "s/: / - /" | sed "s/\//; /"`
# Let's do some work while we look up the resolution
RESOLUTION=$(
# 1080p
if /opt/local/bin/mp4info "$f" | grep -qs "19[0-9][0-9]x"; then
echo " (1080p)";
subler "$f" "1080p"
log "$TITLE" "1080p"
# 720p
elif /opt/local/bin/mp4info "$f" | grep -qs "12[0-9][0-9]x"; then
echo " (720p)";
subler "$f" "720p"
log "$TITLE" "720p"
# 480p or lower
else
echo " (480p)";
subler "$f" "480p"
log "$TITLE" "480p"
fi
)
# Rename the file
mv "$f" "$DIRNAME/$TITLE$RESOLUTION.mp4"
done