Skip to content

Instantly share code, notes, and snippets.

@itsthejb
Forked from jdpace/plex-trailiers.sh
Last active January 29, 2024 14:39
Show Gist options
  • Save itsthejb/a3b0a5727aaeeac86c161c259969aa0c to your computer and use it in GitHub Desktop.
Save itsthejb/a3b0a5727aaeeac86c161c259969aa0c to your computer and use it in GitHub Desktop.
Plex trailers
#!/bin/bash
# downloads all missing trailers - it goes through all your movies and matchs them up with an entry
# in plex, grabs the imdb id from plex, and then parses the trailer url from the imdb site, then passes
# that to youtube-dl to download the trailer, it skips entries that dont have a matching imdb entry
# or if the trailer already exists
# must have 'sqlite3' and 'youtube-dl' packages (apt-get install sqlite3 youtube-dl)
# set 'mpath' and 'pms' accordingly
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <media path>"
exit 1
fi
if [ ! -d $1 ]; then
echo "Path $1 not valid"
exit 1
fi
export mpath="$1";
export pms="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/"; \
export imdb="http://www.imdb.com/"; \
export agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'; \
export notr="/tmp/plex_no_trailers.txt"; \
export db="${pms}Plug-in Support/Databases/com.plexapp.plugins.library.db"
function guid_from_filename() {
sqlite3 "${db}" \
"select guid from media_parts, media_items, metadata_items where media_parts.media_item_id=media_items.id and
media_items.metadata_item_id=metadata_items.id and media_parts.file=\"$1\";";
}
function dirhash_from_guid() {
echo -n "$1" | openssl dgst -sha1 | cut -d" " -f2 | sed -e 's/^\(.\{1\}\)/\1\//' -e 's/ .*//';
}
function imdb_xml_from_guid() {
cat "${pms}Metadata/Movies/$(dirhash_from_guid "$1").bundle/Contents/com.plexapp.agents.imdb/Info.xml" 2> /dev/null;
}
function imdb_from_guid() {
imdb_xml_from_guid "$1" | grep 'Movie id' | cut -d\" -f2 |grep -v "^$";
}
function imdb_video_from_imdb() {
curl "${imdb}title/$1/?ref_=fn_al_tt_1" -s -A "${agent}" --referer "${imdb}" | \
grep "itemprop='trailer'" | sed -e "s/^.*data-video='\(.*\)' data-context.*$/\1/";
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo "Database: ${db}..."
if [ ! -f "${db}" ] ; then
echo "Can't open database at ${db}"
exit 1
fi
IFS="
";
echo -n "Building Movies List..."; files="$(find "${mpath}" -type f | sort | grep -vi -e "\-trailer\.*" \
-e "\.ass$" -e "\.srt$" -e "\.idx$" -e "\.sub$" -e "\.ssa$" -e "\.alt$" -e "\.smi$" -e "\.txt$" \
-e "\.nfo$" -e "\.jpg$" -e "\.png$" -e "\.jpeg$" -e "\.DS_Store" -e "@Syno")"; echo done;
for filename in $files; do
printf "\n---\n"
echo "Working on: $filename";
cd "$(dirname "${filename}")";
echo "Directory: `pwd`"
justfile="$(basename "${filename}")";
mname="$(echo "${PWD}" | sed s#"^.*/"#""#g)";
echo "Just File: $justfile";
echo "Movie Name: $mname";
if [ ! -e "$(dirname "${filename}")/${mname}-trailer."* ] && [ "$(grep "^${mname}$" ${notr})" &> /dev/null = "" ]; then
echo "Querying: \"${db}\""
echo "For: \"${filename}\""
guid="$(guid_from_filename "${filename}")";
[ -z "${guid}" ] && continue
echo "Got GUID: \"${guid}\""
imdbi="$(imdb_from_guid "${guid}")";
[ -z "${imdbi}" ] && continue
echo "Got IMDB ID: \"${imdbi}\""
imdbv="$(imdb_video_from_imdb "${imdbi}")";
echo "Got Video ID: \"${imdbv}\""
if [ -z "${imdbv}" ]; then
echo "Trying again..."
imdbv="$(imdb_video_from_imdb "${imdbi}")";
echo "Got Video ID: \"${imdbv}\""
fi;
if [ -z "${imdbv}" ]; then echo "${mname}" >> ${notr}; continue; fi;
imdb_url="${imdb}video/imdb/${imdbv}/";
echo "Got URL: \"${imdb_url}\""
prefix=`echo ${mname} | sed -e 's/^\(.*\) (.*).*/\1/'`
echo "[download] youtube-dl -o \"${prefix}-trailer.%(ext)s\" \"${imdb_url}\"";
DL_OUT=`youtube-dl --no-progress -o "${prefix}-trailer.%(ext)s" "${imdb_url}"`;
echo $DL_OUT | sed 's/ \[/\n\[/g'
if [[ $DL_OUT == *"has already been downloaded"* ]]; then
echo "[result] Trailer for ${mname} already downloaded"
else
find . -name "*-trailer.*" -exec chown usenet:share {} \;
find . -name "*-trailer.*" -exec chmod 774 {} \;
echo "[result] Downloaded trailer for ${mname}"
fi
else
echo "[result] Already have trailer for ${mname} or there is none for it on IMDB";
fi;
done;
echo "Looking for .aspx files to rename...";
find "${mpath}" -type f -name "*.aspx" | while read line; do
$(ffprobe -v error -show_format $line | grep 'major_brand' | grep -q 'mp4')
if [ $? -eq 0 ]; then
fn=$(echo $line | sed s#"-trailer\.aspx.*$"#"-trailer"#g);
set -v
mv ${fn}.aspx ${fn}.mp4
set +v
continue
fi
$(ffprobe -v error -show_format $line | grep 'major_brand' | grep -q 'flv')
if [ $? -eq 0 ]; then
fn=$(echo $line | sed s#"-trailer\.aspx.*$"#"-trailer"#g);
set -v
mv ${fn}.aspx ${fn}.flv
set +v
else
echo "Could not determine codec of ${line}"
fi
done;
# Test edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment