Video Processing script

Content of the forums on sabnzbdplus.SourceForge.net

Video Processing script

Postby shypike » February 1st, 2008, 5:49 pm

Delete By: auskento (auskento) - 2007-12-22 06:33
This is the current video processing script i use.
Initially developed to handle my movie downloads, but expanded to handle XXX downloads.

This script requires the Newzbin Group directory function be enabled - As per popcornarsonists original script that i learnt from and modified

Remember to change various directory settings as listed below

MOVIE_FOLDER Settings for Movies / XXX
TV Folder

How is Video Processed?

Firstly, a check is done to determine the file extension of the largest file in the Downloaded folder - i cant think of too many times when the largest file wouldnt be the media file

Once it determines the type of media file as AVI, MPG, VOB, ISO, IMG, WMV it then processes it as follows

1 - Determines if the media file is based from a website (only if it is XXX) if this is the case, the file gets processed into a folder based on the website name. (this check only checks for ".com" in the folder name - it can be expanded to cover other domain suffixes)

2 - Processes based on file type
AVI - Joins up to 9 files > 40MB into 1 destination file
MPG - Moves file singularly or as a folder (if more than 1 mpg) to the destination folder
WMV - Moves file singularly or as a folder (if more than 1 wmv) to the destination folder
IMG - Checks that the file is in ISO format then moves/renames to .ISO to destination folder or movie folder if more than one img file
ISO - Moves file singularly or as a folder (if more than 1 ISO) to the destination folder
VOB - Checks for existence of VIDEO_TS folder - if there is, it builds a .ISO file in destination folder, 
other wise makes the VIDEO_TS and moves IFO, BUP, VOB to folder then builds .ISO file

TS and MKV will be added in the future

Please test this out and let me know how you find it, or how it can be improved
NOTE* There are multiple echo lines in place for logging - 1 to a static log file, the other text with no redirect will appear in the post processing email that Sabnzbd plus sends if you have it enabled.

popcornarsonists original TV processing code is still within this scrip

---- CUT BELOW for full script -----

#! /bin/bash

set -u

LOG_PATH="/var/log/sabnzbd/postproc.log"

echo "" >> $LOG_PATH
echo "----- Started Processing: $(date +"%b-%d-%Y %H:%I") -----" >> $LOG_PATH
echo "----- Started Processing: $(date +"%b-%d-%Y %H:%I") -----"


CATEGORY_NAME=`echo $1 | awk -F [/] '{print $(NF-1)}'`

echo $2 >> $LOG_PATH
echo $2

echo "Category: $CATEGORY_NAME" >> $LOG_PATH
echo "Category: $CATEGORY_NAME"


FOLDER_PATH="$1"
FOLDER_NAME=`echo $FOLDER_PATH | awk -F [/] '{print $NF}'`

REMOVE=YES

echo "Folder Path: $FOLDER_PATH" >> $LOG_PATH
echo "Folder Path: $FOLDER_PATH"

cd "$FOLDER_PATH"

####################
# Movie Processing #
####################
#
#if [ $CATEGORY_NAME = "Movies" ]
#then
# # Where do you want to put the movies that this script processes?
# MOVIE_DESTINATION_DIRECTORY="MOVIE FOLDER"
# #
#
# MOVIE_NAME=`echo $FOLDER_NAME | sed 's/ (.*)//g'`
# echo "Movie Name: $MOVIE_NAME" >> $LOG_PATH
#
#
# if [ `find . -size +629145600c | wc -l` -eq 2 ]
# then
# FILEZ[1]="$FOLDER_PATH`find . -size +629145600c | sort | sed -n '1p' | sed 's/^\.//'`"
# FILEZ[2]="$FOLDER_PATH`find . -size +629145600c | sort | sed -n '2p' | sed 's/^\.//'`"
# echo "Joining Files..." >> $LOG_PATH
# mencoder -forceidx -ovc copy -oac copy -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi "${FILEZ[1]}" "${FILEZ[2]}"
# echo "Removing Folder: $FOLDER_NAME..." >> $LOG_PATH
# rm -r "$FOLDER_PATH"
# else
# if [ `find . -size +629145600c` -eq 1 ]
# then
# FILEZ[1]="$FOLDER_PATH`find . -size +629145600c | sed 's/^\.//'`"
# echo "Moving File..." >> $LOG_PATH
# mv "${FILEZ[1]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME.avi"
# rm -r "$FOLDER_PATH"
# echo "Removing Folder..." >> $LOG_PATH
# else
# echo "Couldn't copy(too many files or no files?)" >> $LOG_PATH
# fi
# fi
#
#fi

#################
# TV Processing #
#################

if [ $CATEGORY_NAME = "TV" ]
then

echo "Beginning TV Processing..." >> $LOG_PATH
echo "Beginning TV Processing..."

##Where is your root TV directory? This script will create folders in it, i.e. ROOT_TV/Seinfeld/Season 8/...
TV_DESTINATION_DIRECTORY="TV FOLDER"
#

if [ `echo $FOLDER_NAME | awk -F " - " '{print NF}'` -ne 3 ]
then
echo "Folder name incorrect, quitting..." >> $LOG_PATH
echo "Folder name incorrect, quitting..."
exit 0
fi
OPT_NAME=`echo $FOLDER_NAME | sed 's/ (.*)//g'`

SHOW_NAME=`echo $OPT_NAME | awk -F " - " '{print $1}'`
echo "Show Name: $SHOW_NAME" >> $LOG_PATH
echo "Show Name: $SHOW_NAME"

SEASON_NUMBER=`echo $OPT_NAME | awk -F " - " '{print $2}' | awk -F "x" '{print $1}'`
echo "Season Number: $SEASON_NUMBER" >> $LOG_PATH
echo "Season Number: $SEASON_NUMBER"
EPISODE_NUMBER=`echo $OPT_NAME | awk -F " - " '{print $2}' | awk -F "x" '{print $2}'`

FILE_NM=`echo $OPT_NAME | awk -F " - " '{print $2" "$3}'`
echo "End File Name: $FILE_NM.avi" >> $LOG_PATH
echo "End File Name: $FILE_NM.avi"

FILE_COUNTER=1
USABLE_FILE_COUNTER=0
NUM_OF_FILES=`ls -1 | wc -l`
while [ $FILE_COUNTER -le $NUM_OF_FILES ]
do
if [ `ls -l | grep -v "total [0-9]*" |sed -n ''$FILE_COUNTER'p'|awk '{print $5}'` -gt 73400320 ]
then
if [ `ls -1 |sed -n ''$FILE_COUNTER'p' | awk -F [.] '{print $NF}'` = "avi" ]
then
((USABLE_FILE_COUNTER++))
FILEZ[$USABLE_FILE_COUNTER]=`ls -1 | sed -n ''$FILE_COUNTER'p'`
echo "Usable File: ${FILEZ[$USABLE_FILE_COUNTER]}" >> $LOG_PATH
echo "Usable File: ${FILEZ[$USABLE_FILE_COUNTER]}"
fi
fi
((FILE_COUNTER++))
done

if [ $USABLE_FILE_COUNTER -eq 1 ]
then
echo "One file found, making directories..." >> $LOG_PATH
echo "One file found, making directories..."
mkdir "$TV_DESTINATION_DIRECTORY/$SHOW_NAME"
mkdir "$TV_DESTINATION_DIRECTORY/$SHOW_NAME/Season $SEASON_NUMBER"
echo "Moving File..." >> $LOG_PATH
mv "${FILEZ[1]}" "$TV_DESTINATION_DIRECTORY/$SHOW_NAME/Season $SEASON_NUMBER/$FILE_NM.avi"
echo "Removing Folder: $FOLDER_PATH" >> $LOG_PATH
echo "Removing Folder: $FOLDER_PATH"
rm -r "$FOLDER_PATH"
else
echo "Couldn't copy(too many files or no files?)" >> $LOG_PATH
fi


echo "Done!" >> $LOG_PATH
echo "Done!"
fi

####################
# Music Processing #
####################

# if [ $CATEGORY_NAME = "Music" ]
# then
# FILE_COUNTER=1
# NUM_OF_FILES=`ls -1 | wc -l`
# while [ $FILE_COUNTER -le $NUM_OF_FILES ]
# do
# if [ `ls -1 |sed -n ''$FILE_COUNTER'p' | awk -F [.] '{print $NF}'` = "mp3" ]
# then
# FILENAME=`ls -1 |sed -n ''$FILE_COUNTER'p'`
# APPLESCRIPT_PATH=`echo $FOLDER_PATH/$FILENAME | sed 's/\//Macintosh HD:/' | sed 's/\//:/g'`
# osascript -e "tell application \"iTunes\" to add file \"$APPLESCRIPT_PATH\""
# fi
# ((FILE_COUNTER++))
# done
# growlnotify -i mp3 -m "Added $FOLDER_NAME mp3 files to iTunes"
# mv "$FOLDER_PATH" "$USENET_COMPLETE/$CATEGORY_NAME/A_$FOLDER_NAME"
# fi
#
# exit 0

####################
# Video Processing #
####################

if [ $CATEGORY_NAME = "Movies" -o $CATEGORY_NAME = "XXX" ]
then
case $CATEGORY_NAME in
Movies) MOVIE_DESTINATION_DIRECTORY="MOVIE FOLDER" ;;
XXX) MOVIE_DESTINATION_DIRECTORY="ADULT FOLDER" ;;
esac

cd "$1"

# Assumes that the largest file in the folder is the media file.

MOVIETYPE=`find . -printf '%s %p\n' | sort -nr | sed -n '1p' | awk -F. '{print $NF}'`
MOVIETYPE=`echo "$MOVIETYPE" | tr [a-z] [A-Z]`

echo -e "Movie is in the $MOVIETYPE format\n" >> $LOG_PATH
echo "Movie is in the $MOVIETYPE format"


#Determine how many parts there are comprising the media

PARTS=`find . -iname "*.$MOVIETYPE" | wc -l`


# Determine (aka guess) if the content is media from a particular website
# Skip this if content is a Movie


if [ $CATEGORY_NAME = "XXX" ]
then
SITECONTENT=`echo $FOLDER_NAME | grep ".com"`

if [ "$SITECONTENT" != "" ]
then
echo -e "Site:`echo "$SITECONTENT" | awk -F " - " '{print $1}' | tr [A-Z] [a-z]`\n" >> $LOG_PATH
echo "Site:`echo "$SITECONTENT" | awk -F " - " '{print $1}' | tr [A-Z] [a-z]`"

MOVIE_NAME=`echo "$SITECONTENT" | awk -F " - " '{print $2" "$3}'`
echo -e "Clip:$MOVIE_NAME\n" >> $LOG_PATH
echo "Clip:$MOVIE_NAME"

MOVIE_DESTINATION_DIRECTORY=$MOVIE_DESTINATION_DIRECTORY/"`echo "$SITECONTENT" | awk -F " - " '{print $1}' | tr [A-Z] [a-z]`"
mkdir "$MOVIE_DESTINATION_DIRECTORY"
else
MOVIE_NAME=`echo $FOLDER_NAME | sed 's/ (.*)//g'`
echo -e "Movie Name: $MOVIE_NAME\n" >> $LOG_PATH
echo "Movie Name: $MOVIE_NAME"
fi
else
MOVIE_NAME=`echo $FOLDER_NAME | sed 's/ (.*)//g'`
echo -e "Movie Name: $MOVIE_NAME\n" >> $LOG_PATH
echo "Movie Name: $MOVIE_NAME"
fi

# In processing the media files, it will be important to NOT join files if there is more than 1 part, and instead just move the
# folder



if [ "$MOVIETYPE" = "AVI" ]
then
cd "$1"
echo "Movie is in $PARTS parts" >> $LOG_PATH
echo "Movie is in $PARTS parts"
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]=$FOLDER_PATH`find . -iname "*.$MOVIETYPE" -size +40M | sort | sed -n ''$i'p' | sed 's/^\.//'`
((i++))
done

case "$PARTS" in
1) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" ;;
2) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" ;;
3) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" ;;
4) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" ;;
5) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" "${FILEZ[5]}" ;;
6) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" "${FILEZ[5]}" "${FILEZ[6]}" ;;
7) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" "${FILEZ[5]}" "${FILEZ[6]}" "${FILEZ[7]}" ;;
8) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" "${FILEZ[5]}" "${FILEZ[6]}" "${FILEZ[7]}" "${FILEZ[8]}" ;;
9) avimerge -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi -i "${FILEZ[1]}" "${FILEZ[2]}" "${FILEZ[3]}" "${FILEZ[4]}" "${FILEZ[5]}" "${FILEZ[6]}" "${FILEZ[7]}" "${FILEZ[8]}" "${FILEZ[9]}" ;;
*) echo "Too many files" >> $LOG_PATH;;
esac
DURATION=`tcprobe -i "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".avi | grep duration= | awk -Fduration= '{print $2}'`
echo "Movie Duration=$DURATION" >> $LOG_PATH
echo "Movie Duration=$DURATION"
fi


if [ "$MOVIETYPE" = "WMV" ]
then
cd "$1"
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]=$FOLDER_PATH`find . -iname "*.$MOVIETYPE" -size +40M | sort | sed -n ''$i'p' | sed 's/^\.//'`
((i++))
done

# Instead of moving here, you could set the system to reencode the file to a different format
# If the folder is in multiple parts, it is safest to move the whole source folder and not re-encode
# The code below will need to be changed from using CASE though


if [ $PARTS -gt 1 ]
then
echo "Folder has $PARTS WMV Files" >> $LOG_PATH
echo "Folder has $PARTS WMV Files"
mkdir "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"

i=1
while [ $i -le $PARTS ]; do
mv "${FILEZ[$i]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"
((i++))
done

else
mv "${FILEZ[1]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".wmv
fi
fi

if [ "$MOVIETYPE" = "MPG" ]
then
cd "$1"
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]=$FOLDER_PATH`find . -iname "*.$MOVIETYPE" -size +40M | sort | sed -n ''$i'p' | sed 's/^\.//'`
((i++))
done

case "$PARTS" in
1) mv "${FILEZ[1]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".mpg ;;
*) ;;
esac
fi

if [ "$MOVIETYPE" = "VOB" ]
then
case $CATEGORY_NAME in
Movies) MOVIE_DESTINATION_DIRECTORY="MOVIE ISO FOLDER" ;;
XXX) MOVIE_DESTINATION_DIRECTORY="ADULT ISO FOLDER" ;;
esac

cd "$1"

#Check to see if there is a VIDEO_TS folder if it does, set the variable to the directory the VIDEO_TS folder is in
VTS="`find . -type d -name VIDEO_TS -printf %h`"

if [ "$VTS" != "" ]
then
echo "VIDEO_TS Folder located" >> $LOG_PATH
echo "VIDEO_TS Folder located"

cd "$VTS"
mkisofs -dvd-video -udf -quiet -V "$MOVIE_NAME" -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".ISO .
else
# Video files are not under a VIDEO_TS File - Need to create the VIDEO_TS folder and move IFO, BUP and VOB files to it

mkdir "VIDEO_TS"

PARTS=`find . -type f | wc -l`
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]=$FOLDER_PATH`find . -type f | sort | sed -n ''$i'p' | sed 's/^\.//' | tr [a-z] [A-Z]`
((i++))
done

i=1
while [ $i -le $PARTS ]; do
FILEXT=`echo ${FILEZ[$i]} | awk -F. '{print $NF}' | tr [a-z] [A-Z]`
if [ "$FILEXT" = "IFO" -o "$FILEXT" = "BUP" -o "$FILEXT" = "VOB" ]
then
mv "${FILEZ[$i]}" "$FOLDER_PATH/VIDEO_TS"
fi
((i++))
done

mkisofs -dvd-video -udf -quiet -V "$MOVIE_NAME" -o "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".ISO .
fi
fi

if [ "$MOVIETYPE" = "ISO" ]
then
# Set DVD Folder
case $CATEGORY_NAME in
Movies) MOVIE_DESTINATION_DIRECTORY="MOVIE ISO FOLDER" ;;
XXX) MOVIE_DESTINATION_DIRECTORY="ADULT ISO FOLDER" ;;
esac

# With ISO images, I like to keep the year of release (if present) in the title

MOVIE_NAME="$FOLDER_NAME"

cd "$1"
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]="$FOLDER_PATH`find . -iname "*.$MOVIETYPE" | sort | sed -n ''$i'p' | sed 's/^\.//'`"
((i++))
done

if [ $PARTS -gt 1 ]
then
echo "There are $PARTS ISO files within folder" >> $LOG_PATH
echo "There are $PARTS ISO files within folder"

mkdir "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"

i=1
while [ $i -le $PARTS ]; do
# Make sure the file ext is .ISO
mv "${FILEZ[$i]}" "`echo "${FILEZ[$i]}" | awk -F. '{print $1}'`".ISO
((i++))
done

# Move ISO Files

mv *.ISO "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"

else

mv "${FILEZ[1]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".ISO
fi
fi

if [ "$MOVIETYPE" = "IMG" ]
then
# Set DVD Folder
case $CATEGORY_NAME in
Movies) MOVIE_DESTINATION_DIRECTORY="MOVIE ISO FOLDER" ;;
XXX) MOVIE_DESTINATION_DIRECTORY="ADULT ISO FOLDER" ;;
esac

# With ISO images, I like to keep the year of release (if present) in the title

MOVIE_NAME="$FOLDER_NAME"

cd "$1"
i=1
while [ $i -le $PARTS ]; do
FILEZ[$i]="$FOLDER_PATH`find . -iname "*.$MOVIETYPE" | sort | sed -n ''$i'p' | sed 's/^\.//'`"
((i++))
done

if [ $PARTS -gt 1 ]
then
echo "There are $PARTS image files within folder" >> $LOG_PATH
echo "There are $PARTS image files within folder"
# If there is more than one IMG File, make the destination folder and rename the IMG files to ISO

mkdir "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"

i=1
while [ $i -le $PARTS ]; do
if [ "`file "${FILEZ[$i]}" | grep ISO`" != "" ]
then
echo "${FILEZ[$i]} is in ISO format" >> $LOG_PATH
echo "${FILEZ[$i]} is in ISO format"
mv "${FILEZ[$i]}" "`echo "${FILEZ[$i]}" | awk -F. '{print $1}'`".ISO
else
echo "${FILEZ[$i]} is not in ISO format - aborting processing" >> $LOG_PATH
echo "${FILEZ[$i]} is not in ISO format - aborting processing"
exit
fi
((i++))
done

# Move ISO Files

mv *.ISO "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME"

else
if [ "`file "${FILEZ[1]}" | grep ISO`" != "" ]
then
echo "${FILEZ[1]} is in ISO format" >> $LOG_PATH
echo "${FILEZ[1]} is in ISO format"
mv "${FILEZ[1]}" "$MOVIE_DESTINATION_DIRECTORY/$MOVIE_NAME".ISO
else
echo "${FILEZ[1]} is not in ISO format - aborting processing" >> $LOG_PATH
echo "${FILEZ[1]} is not in ISO format - aborting processing"
exit
fi
fi
fi




if [ "$REMOVE" = "YES" ]
then
echo "Removing Folder: $FOLDER_NAME..." >> $LOG_PATH
echo "Removing Folder: $FOLDER_NAME..."
rm -r "$FOLDER_PATH"
fi

echo "Done!" >> $LOG_PATH
echo "Done!"

fi

exit

------ END SCRIPT -------
User avatar
shypike
Administrator
Administrator
 
Posts: 20998
Joined: January 18th, 2008, 12:49 pm

Re: Video Processing script

Postby shypike » February 1st, 2008, 5:49 pm

      RE: Video Processing script
      Delete By: rascalli (rascalli) - 2007-12-22 07:54
      nice script ...... but why handle xxx ? does that have to go into a folder that the wife & kids cannot acces ;-Þ

      RE: Video Processing script
      Delete By: auskento (auskento) - 2007-12-22 15:58
      pretty much yes, Newzbin also has an XXX category, so anything added from there goes to its own category folder

      RE: Video Processing script
      Delete By: qrb123 (qrb123) - 2008-01-04 03:09
      I like this!! I am going to try to get it working for me.
     
      Would it be possible to get it to encode the avi files to mpeg, and then make an iso? I know nothing about scripting, and was wondering if that is something that would be possible to add?

      RE: Video Processing script
      Delete By: auskento (auskento) - 2008-01-05 03:08
      I have plans on doing auto conversion to MPG, it is possible to go to ISO form there too since there are command line tools to do it.
      The more you do, the longer your post processing can take.
      The great thing about sabnzbd is it only tries to process 1 thing at a time.
     
      To do encoding though you will want a blisteringly fast PC, otherwise you could end up with very large post processing queues.
     
      An average 90 min movie takes about 3 hours to convert on my server, which is why i havent included anything other than joining of AVI's in the post processing.

      RE: Video Processing script
      Delete By: Nobody/Anonymous (nobody) - 2008-01-05 13:08
      Adding *.ts support to join them up on download would be awesome!
User avatar
shypike
Administrator
Administrator
 
Posts: 20998
Joined: January 18th, 2008, 12:49 pm

Re: Video Processing script

Postby th3joker » February 28th, 2009, 2:46 pm

errors with: line 14: $2: unbound variable
th3joker
Jr. Member
Jr. Member
 
Posts: 64
Joined: January 25th, 2008, 8:15 am

Re: Video Processing script

Postby switch » February 28th, 2009, 2:53 pm

This topic is simply a backup of conversations from the old board up at SF.net, it is not meant for new conversations.

Please try this topic instead: https://forums.sabnzbd.org/index.php?topic=49.0
User avatar
switch
Moderator
Moderator
 
Posts: 1513
Joined: January 17th, 2008, 3:55 pm
Location: UK


Return to Old SF.net forum