bash script to scan for repeated episode numbers, append episode modifier

96 Views Asked by At

I use youtube-dl to archive specific blogs. I use a custom bash script (called tvify) to help me organize my content into Plex-ready filenames for later replay via my home Plex server.

Archiving the content works fine, unless a blogger posts more than one video on the same date - if that happens my script creates more than one file for a given month/date and plex sees a duplicate episode. In the plex app, it stuffs them together as distinct 'versions' of the same episode. The result is that the description of the video no longer matches its contents, and only one 'version' appears unless I access an additional sub menu.

The videos get downloaded by you tube-dl kicked off from a cron-job, and that downloader script runs the following to help format their filenames and stuff them into appropriate folders for 'seasons'.

The season is the year when the video was released, and the episode is the combination of the month and date in MMDD format.

Below is my 'tvify' script, which helps perform the filename manipulation and stuffs the file into the proper folder for the season.

#!/bin/bash
mySuff="$1"
echo mySuff="$mySuff"

if [ -z "$1" ]; then
        mySuff="*.mp4"
fi

for i in $mySuff
do
        prb=`ffprobe -- "$i" 2>&1`
        myDate=`echo "$prb" | grep -E 'date\s+:' | cut -d ':' -f 2`
        myartist=`echo "$prb" | grep -E 'artist\s+:' | cut -d ':' -f 2`
        myTitle=`echo "$prb" | grep -E 'title\s+:' | cut -d ':' -f 2 | sed 's/\//_/g'`
        cwd_stub=`pwd | awk -F'/' '{print $NF}'`
        if [ -d "s${myDate:1:4}" ]; then echo "Directory found" > /dev/null; else mkdir "s${myDate:1:4}"; fi
        [ -d "s${myDate:1:4}" ] && mv -- "$i" "s${myDate:1:4}/${myartist[@]:1} - s${myDate:1:4}e${myDate:5:8} - ${myTitle[@]:1:40} _$i" || mv -- "$i" "${myartist[@]:1} - s${myDate:1:4}e${myDate:5:8} - ${myTitle[@]:1:40} _$i"
done

How can I modify that script to identify if a conflicting year/MMDD file exists, and if so, append an appropriate suffix to the episode number so that plex will interpret them as distinct episodes?

1

There are 1 best solutions below

0
On BEST ANSWER

I ended up implementing an array, counting the number of elements in the array, and using that to append the integer:

#!/bin/bash
mySuff="$1"
echo mySuff="$mySuff"

if [ -z "$1" ]; then
        mySuff="*.mp4"
fi

for i in $mySuff
do
        prb=`ffprobe -- "$i" 2>&1`
        myDate=`echo "$prb" | grep -E 'date\s+:' | cut -d ':' -f 2`
        myartist=`echo "$prb" | grep -E 'artist\s+:' | cut -d ':' -f 2`
        myTitle=`echo "$prb" | grep -E 'title\s+:' | cut -d ':' -f 2 | sed 's/\//_/g'`
        cwd_stub=`pwd | awk -F'/' '{print $NF}'`
        readarray -t conflicts < <(find . -maxdepth 2 -iname "*s${myDate:1:4}e${myDate:5:8}*" -type f -printf '%P\n')
        [ ${#conflicts[@]} -gt 0 ] && _inc=${#conflicts[@]} || _inc=
        if [ -d "s${myDate:1:4}" ]; then echo "Directory found" > /dev/null; else mkdir "s${myDate:1:4}"; fi
        [ -d "s${myDate:1:4}" ] 
            && mv -- "$i" "s${myDate:1:4}/${myartist[@]:1} - s${myDate:1:4}e${myDate:5:8}$_inc - ${myTitle[@]:1:40} _$i" 
            || mv -- "$i" "${myartist[@]:1} - s${myDate:1:4}e${myDate:5:8}$_inc - ${myTitle[@]:1:40} _$i"
done