Last active
November 23, 2021 23:40
Plex trailers
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
#!/opt/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 | |
export mpath="/volume1/video/Movies/"; | |
export pms="/volume1/Plex/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'; \ | |
function guid_from_filename() { | |
sqlite3 "${pms}Plug-in Support/Databases/com.plexapp.plugins.library.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"; | |
} | |
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 title-trailer | sed s#'^.*data-video="\(.*\)" data.*$'#'\1'#g; | |
} | |
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 | |
echo "Working on $filename"; | |
cd "$(dirname "${filename}")"; | |
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}$" ~/.no_trailers.txt)" = "" ]; then | |
guid="$(guid_from_filename "${filename}")"; | |
echo "Got GUID ${guid}" | |
[ -z "${guid}" ] && continue | |
imdbi="$(imdb_from_guid "${guid}")"; | |
echo "Got IMDB ID ${imdbi}" | |
[ -z "${imdbi}" ] && continue | |
imdbv="$(imdb_video_from_imdb "${imdbi}")"; | |
echo "Got IMDB Video ID ${imdbv}" | |
if [ -z "${imdbv}" ]; then | |
echo "Trying again..." | |
imdbv="$(imdb_video_from_imdb "${imdbi}")"; | |
echo "Got IMDB Video ID ${imdbv}" | |
fi; | |
if [ -z "${imdbv}" ]; then echo "${mname}" >> ~/.no_trailers.txt; continue; fi; | |
imdb_url="${imdb}video/imdb/${imdbv}/"; | |
echo "Got Youtube URL $imdb_url" | |
echo "youtube-dl -o \"${mname}-trailer.%(ext)s\" \"${imdb_url}\""; | |
youtube-dl -o "${mname}-trailer.%(ext)s" "${imdb_url}"; | |
else | |
echo "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