Script to download trailers missing from your library

Hello Everybody,

 

I have been waiting a long time for trailer support to become a reality in Plex.  Now that it is, I have come to realize how many of my movies I am missing local trailers for.  I started to download them manually, then I thought why not have a script do this for me.  So I spent more time trying to figure out this script (I am not good at writing code) than it would have taken to finish downloading the trailers myself.  All that to say, I have a script (WIP) that will do this for you.

 

1. Requires FileBot or a .nfo file in the movie folder

2. Requires youtube-dl

 

First the script will look in your BASE directory for any movie folders that do not contain a .nfo file. If it does not find one, it will run FileBot to update this folders files.  Then this script will search your BASE directory for folders that do NOT contain -trailer.ext.  It will download the trailer for the movie based on the YouTube URL in the .nfo file. 

 

This is not perfect but seems to work for me.  Please feel free to make this better.  As I stated earlier, I am not good with code. I am good at copy and paste.  This work is a bunch of examples I have found.

#!/bin/bash
#################################
#Base path to start search
BASE=/home/path/to/your/movies
#Quality to download. To get the accepted value run; youtube-dl -F parameter to get a list of formats
#Multiple formats as 22/18/12
Q=18
#Base Url for video download
YTB_URL="https://www.youtube.com/watch?v="
#################################

find BASE -mindepth 2 -maxdepth 2 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "*\.(nfo)"’ ‘;’ -print | while read dir
do
filebot -script fn:artwork.tmdb “$dir”
filebot -get-subtitles “$dir” --lang en --output srt --encoding utf8
done

find BASE -mindepth 2 -maxdepth 2 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "trailer\.(mp4|avi)"’ ‘;’ -print | while read dir
do
ID=$(awk -F “[><]” ‘/trailer/{print $3}’ “$dir/movie.nfo”)
youtube-dl -f $Q $YTB_URL$ID -o “$dir/%(title)s-trailer.%(ext)s”
done

I’m not sure if anyone has tried to use this script recently. I just tried it and found a few issues.

No found movies.
-mindepth 2 -> -mindepth 1
My movies folder is /NAS/VideoNAS4/Movies
This is what I set as my $BASE. With the script finds nothing unless I change -mindepth 2 to 1.

Metadata is downloading but not Trailers. No solution yet.
This issue seems to be with:
ID=$(awk -F “[><]” ‘/trailer/{print $3}’ “$dir/movie.nfo”)
The movie.nfo file doesn’t seem to contain an ID. The code results in an empty variable.

I’m hoping that this can be fixed because this script is suppose to do exactly what I want it to do.

@w1r3d90d said:
I’m not sure if anyone has tried to use this script recently. I just tried it and found a few issues.

No found movies.
-mindepth 2 → -mindepth 1
My movies folder is /NAS/VideoNAS4/Movies
This is what I set as my $BASE. With the script finds nothing unless I change -mindepth 2 to 1.

Metadata is downloading but not Trailers. No solution yet.
This issue seems to be with:
ID=$(awk -F “[><]” ‘/trailer/{print $3}’ “$dir/movie.nfo”)
The movie.nfo file doesn’t seem to contain an ID. The code results in an empty variable.

I’m hoping that this can be fixed because this script is suppose to do exactly what I want it to do.

It’s a 3 year old post so it seems like since you need it, Why not take the time and hash it out? :smiley:

@w1r3d90d said:
I’m not sure if anyone has tried to use this script recently. I just tried it and found a few issues.

No found movies.
-mindepth 2 → -mindepth 1
My movies folder is /NAS/VideoNAS4/Movies
This is what I set as my $BASE. With the script finds nothing unless I change -mindepth 2 to 1.

My paths for this configuration are:
/home/user/Media/Movies/MPAA_Rating/MovieName (Year)/Movie Name.ext
which my $BASE would be::
/home/user/Media/Movies/

If you are not using the PLEX suggested folder structure, then you may have to modify the -mindepth to your folder structure.
Plex Naming Movie Files

Metadata is downloading but not Trailers. No solution yet.
This issue seems to be with:
ID=$(awk -F “[><]” ‘/trailer/{print $3}’ “$dir/movie.nfo”)
The movie.nfo file doesn’t seem to contain an ID. The code results in an empty variable.
This script was originally dependent upon having Filebot to download the .nfo if it did not already exist in the movie folder. If the .nfo does not contain the TMDB movie ID, yes the variable will be empty.

I’m hoping that this can be fixed because this script is suppose to do exactly what I want it to do.

Try the following changes. This removes the dependency on Filebot to create the .nfo or even that the .nfo should exist. Still requires youtuble-dl and your own TMDB API KEY.

    #! /bin/bash
    #################################
    #Base path to start search
    BASE=/path/to/your/movies/
    #TMDB Base URL
    TMDB=https://api.themoviedb.org
    TMDBLANG=en-US
    #API Key
    API=Your_TMDB_API_KEY
    #Quality to download. To get the accepted value run; youtube-dl -F parameter to get a list of formats, multiple formats as 22/18/12
    Q=18
    #Base Url for video download
    YTB_URL="https://www.youtube.com/watch?v="
    #################################
find $BASE -mindepth 2 -maxdepth 2 -type d '!' -exec sh -c 'ls -1 "{}"|egrep -i -q "trailer\.(mp4|avi)$"' ';' -print | while read dir
do
	MOVNAME=$(basename "$dir")
	MOVNAMEMOD=$((basename "$dir" | cut -d "(" -f1) | sed -e 's/[[:space:]]*$//' | sed 's/ /%20/g')
	MOVYEAR=$(basename "$dir" |  cut -d "(" -f2 | cut -d ")" -f1)
	MOVID=$((curl -s --request GET --url "$TMDB/3/search/movie?year=$MOVYEAR&query=$MOVNAMEMOD&language=$TMDBLANG&api_key=$API" --header 'content-type: application/json' --data '{}' ) | cut -d ":" -f7 | cut -d ":" -f6 | cut -d "," -f1)
	TRID=$(curl -s --request GET $TMDB/3/movie/$MOVID/videos?api_key=$API | sed -e 's/,"/\n/g;s/"//g' | grep -o 'key:.*' | cut -f2- -d':')
	echo `date`
	echo "*****"$MOVNAME"*****"
	echo ""Downloading from YouTube: $YTB_URL$TRID""
	youtube-dl -f $Q $YTB_URL$TRID -o "$dir/${MOVNAME/%???????/}_%(autonumber)s-trailer.%(ext)s" --autonumber-size "1"
	echo "Trailer saved to: $dir"
	echo -en "\n"
done
    
    echo `date`": Search complete."

Thanks for your reply. I’ll try this out as soon as I get a chance and report back.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.