Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Redirect STDOUT/STDERR to a separate file so we can see errors
- exec >> /tmp/plex-post.err
- exec 2>&1
- # Set the GroupMe Bot ID
- . /etc/groupme.conf
- logfile=/tmp/plex-post.log
- # Set DEBUG to 0 for no debugging, 1 for log only, 2 for full alerting
- DEBUG=2
- # I placed these in my home directory for easy access, make sure the "plex"
- # user has read access to them. We expect the following files:
- # safe_to_cut: We don't care about backups.
- # chapter_only: We want to set chapter breaks instead of cutting.
- # skip_processing: We don't want to process at all.
- # backup_and_cut: We want the original and a second file without commercials.
- #
- # In the default config, backup_and_cut is the default behavior but this can be
- # changed.
- config_path="/home/jdbower/plex_post"
- function log () {
- logline="$1"
- # Log it locally.
- echo "$logline" >> $logfile
- # JSON expects \n to be a literal '\n' and doesn't like double quotes.
- logline=$(echo "$logline" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' | sed 's/\"/'\''/g')
- # This posts to a GroupMe based logging mechanism, it can be swapped for
- # Twitter, an email client, etc., as you see fit.
- curl --silent -X POST -d '{"bot_id": "'$bot_id'", "text": "'"$logline"'"}' -H 'Content-Type: application/json' https://api.groupme.com/v3/bots/post
- }
- function debug () {
- case $DEBUG in
- 1)
- # Just log locally.
- echo "$1" >> $logfile
- ;;
- 2)
- # Run the full logging function.
- log "$1"
- ;;
- esac
- }
- # To get the appropriate scripts for commercial cutting use:
- # git clone git://github.com/BrettSheleski/comchap
- # https://www.ebower.com/wiki/Plex#Commercial_Detection_and_Removal
- #
- # cut_commercials=2: backup the original and try to skip the commercials.
- # cut_commercials=1: skip the commercials.
- # cut_commercials=0: create chapter marks for the commercials.
- # cut_commercials=-1: don't post process the commercials
- cut_commercials=2
- # The full path filename
- input=$1
- # Just the input filename.
- filename=$(basename "$input")
- # Remove the extension for the full show+episode name.
- title=${filename%.*}
- # The path to the library
- library_path=$(echo "$input" | awk -F'/.grab/' '{print $1}')
- # The directory in the library path where the show will live, also the show name
- dir=$(echo "$title" | awk -F') - ' '{print $1")"}')
- # For movies we need to strip the last )
- dir=$(echo "$dir" | sed 's/))/)/')
- # The season plus episode, don't use this directly as movies or odd values may
- # produce unexpected results.
- season_episode=$(echo "$title" | awk -F') - ' '{print $2")"}' | awk '{print $1}')
- # Split the season from the episode number.
- if [[ "$season_episode" =~ ^S[0-9]+E[0-9]+$ ]]; then
- debug "Found normal season of the format SnEn for '$season_episode'"
- season=$(echo $season_episode | awk -FE '{print $1}' | sed 's/^S//')
- episode=$(echo $season_episode | awk -FE '{print $2}')
- elif [[ "$season_episode" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
- debug "Found date season of the format yyyy-mm-dd for '$season_episode'"
- season=$(echo $season_episode | awk -F\- '{print $1}')
- episode=$(echo $season_episode | awk -F\- '{print $2"-"$3}')
- else
- debug "No season found for '$season_episode'"
- season=""
- episode=""
- fi
- # The format of the following files is one show per line, we search for:
- # *Show Name* across the show name, excluding episode name.
- # When a show appears in multiple files, the least likely to cause content
- # loss will win. For example, we'd rather skip processing than set chapter
- # boundaries. Default behavior is to backup and cut, increasing storage costs.
- for filename in backup_and_cut safe_to_cut chapter_only skip_processing; do
- if [[ -f "${config_path}/$filename" ]]; then
- while IFS='' read -r curr_title; do
- if [[ "$curr_title" != "" ]]; then
- if [[ "$dir" == *"$curr_title"* ]]; then
- debug "Changing processing for '$dir' to $filename"
- case $filename in
- "backup_and_cut")
- cut_commercials=2
- ;;
- "safe_to_cut")
- cut_commercials=1
- ;;
- "chapter_only")
- cut_commercials=0
- ;;
- "skip_processing")
- cut_commercials=-1
- ;;
- esac
- fi
- fi
- done < "${config_path}/$filename"
- else
- debug "'${config_path}/$filename' not found"
- fi
- done
- # Do a variable dump to the debug log.
- debug "input: '$@'
- filename: '$filename'
- title: '$title'
- library_path: '$library_path'
- dir: '$dir'
- season_episode: '$season_episode'
- season: '$season'
- episode: '$episode'
- cut_commercials: '$cut_commercials'"
- # Some shows have false positives and skip content instead of commercials
- function chap_skip () {
- log "Setting chapter boundaries for commercials on '$title'"
- comchap "$input"
- }
- # Some shows don't need processing
- function do_nothing () {
- log "Skipping commercial cuts on '$title'"
- }
- # Remove commercials
- function cut_comm () {
- log "Cutting commercials on '$title'"
- comcut "$input"
- }
- # Make a backup for shows we're not certain of
- function backup_and_cut () {
- # If the season variable is an integer use a season folder, otherwise don't
- if [[ "$season" =~ ^[0-9]+$ ]]; then
- dest_path="${library_path}/${dir}/Season ${season}"
- else
- dest_path="${library_path}/${dir}"
- fi
- log "Backing up ${title} to '${dest_path}/${title}-orig.ts'"
- if [ ! -d "$dest_path" ]; then
- debug "Making '$dest_path'"
- mkdir -p "$dest_path"
- fi
- cp "$input" "${dest_path}/${title}-orig.ts"
- cut_comm
- }
- case $cut_commercials in
- -1)
- do_nothing
- ;;
- 0)
- chap_skip
- ;;
- 2)
- backup_and_cut
- ;;
- 1)
- cut_comm
- ;;
- esac
- # You can uncomment these to change the permissions on the files
- # debug "Setting permissions in '${library_path}/${dir}'"
- #find ${library_path}/${dir} -type f -exec chmod 664 {} \;
- #find ${library_path}/${dir} -type d -exec chmod 775 {} \;
- # You can uncomment these to scan the library automatically
- # export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/lib/plexmediaserver"
- # debug "Rescanning Library"
- # /usr/lib/plexmediaserver/Plex\ Media\ Scanner -s &
- debug "Finished processing '$title'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement