Quantcast
Channel: Recent Discussions — Plex Forums
Viewing all articles
Browse latest Browse all 151235

auto download missing trailers from idmb for all movies in collection

$
0
0

Took all night to script this, but here it is:

#!/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="/mnt/public/Movies/"; 
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'; \
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" | shasum | cut -d" " -f1 | 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  |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$")"; 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 "${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 -n "Looking for .aspx files to rename...";
files="$(find "${mpath}" -type f -name "*.aspx")";
echo "done";
if [ -z "${files}" ]; then 
  echo "Nothing To Rename"
else
  echo "Renaming MPEG v4 .aspx files to .mp4   ";
  for filename in $(file ${files} | grep 'MPEG v4' | sed s#"-trailer\.aspx.*$"#"-trailer"#g); do
    mv "${filename}.aspx" "${filename}.mp4"; 
  done;
  echo "done";
  echo "Renaming Macromedia Flash Video .aspx files to .flv   ";
  for filename in $(file ${files} | grep 'Macromedia Flash Video' | sed s#"-trailer\.aspx.*$"#"-trailer"#g); do 
    mv "${filename}.aspx" "${filename}.flv"; 
  done;
  echo "done";
fi;

 

Updated the script to cache the list of movies that have an IMDB ID , but no Video ID was found on the IMDB site, meaning there is no trailer for it.   Also added searching the trailers afterwords for .aspx files and rename them accordingly to .mp4 or .flv depending on what type of video they really are.  Many of the videos downloaded as .mp4, but some downloaded as .aspx and this is a small fix to rename the .aspx files.


Viewing all articles
Browse latest Browse all 151235

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>