Automated FFmpeg Convert Bash Script

I need a bash script that will.
So here is what I made so far.

  1. Convert video files to iPhone format
  2. MV converted video to new directory (with domain.com attached to it)
  3. Copy a NFO file (from another directory) and add some conversion information
  4. Delete old directory
    torrent stuff (rtorrent)
  5. Make a torrent file out of new converted video directory
#!/bin/bash
# ffmpeg and mencoder script
# Grab thumb from avi, start encoding to ITU h264 using mencoder, ffmpeg is doing thumb processing

# Bash script for operating system Ubuntu 8.10 
# packages used : FFMPEG, MENCODER ,MPLAYER ENCODING ENGINE
# VIDEO CODEC ITU H264 AUDIO MP3


# Written by FakeOutdoorsman and updated by mysoogal and modified by Domzz
# Attribution-Noncommercial 3.0 Unported
# http://creativecommons.org/licenses/by-nc/3.0/deed.en
# trackback http://ubuntuforums.org/showthread.php?t=960627

# Location of source videos READ this !!! add your user name !
sourcelocation="/var/www/videos/"
# Extension of source videos
sourceext="avi,mpg,mpeg,mov,rmvb,rm,ogm,4v

#http://thomer.com/howtos/ipod_video.html
find ${sourcelocation} -iname "*${sourceext}" -exec mp4ize {}.mp4
# Check to see if videos were encoded, then delete source vids and shutdown
if [ -e "${sourcelocation}/*.avi" ] && [ -e "${sourcelocation}/*.mp4]; then
    # Delete videos    
    rm ${sourcelocation}/*.avi
    rm ${sourcelocation}/*.mpeg
    rm ${sourcelocation}/*.mov
    rm ${sourcelocation}/*.mpg
    rm ${sourcelocation}/*.txt
    rm ${sourcelocation}/*.nfo
    #copys a txt file from my directory to sources directory
    cp /home/admin/file.txt ${sourcelocation}
    #moves directory
    mv ${sourcelocation} /home/admin/${sourcelocation}_domain.com
    #builds the torrent file using: http://claudiusmaximus.goto10.org/cm/torrent.html
    #http://www.torrent-invites.com/tutorials/52022-putty-complete-tutorial.html
    cd /home/admin
    mktorrent -v -p -a http://tracker.url -o ${sourcelocation}_domain.com.torrent ${sourcelocation}_domain.com
else
    echo "Encoding FAILED"
fi

exit

I am not sure if I should use:

    rm ${sourcelocation}/*.avi
    rm ${sourcelocation}/*.mpeg
    rm ${sourcelocation}/*.mov
    rm ${sourcelocation}/*.mpg
    rm ${sourcelocation}/*.txt
    rm ${sourcelocation}/*.nfo

or if there is a better way.

I noticed a missing quote at the end of sourceext... line, then also a missing quote in the "if..." line between mp4 and the closing square bracket and maybe a missing whitespace between the mentioned quote and closing square bracket.

    rm ${sourcelocation}/*.avi
    rm ${sourcelocation}/*.mpeg
    rm ${sourcelocation}/*.mov
    rm ${sourcelocation}/*.mpg
    rm ${sourcelocation}/*.txt
    rm ${sourcelocation}/*.nfo

could probably be reduced to

for ext in avi mpeg mov mpg txt nfo; do rm ${sourcelocation}/*.$ext; done

HTH

ok thanks for that, I also updated the bash script to check if ffmpeg or handbrakecli is running. This is my first bash script.

I know {sourcelocation} is the directory, but I want the script to check for and {sourcelocation} subdirectory.

#!/bin/bash
# ffmpeg and mencoder script
# Grab thumb from avi, start encoding to ITU h264 using mencoder, ffmpeg is doing thumb processing

# Bash script for operating system Ubuntu 8.10 
# packages used : FFMPEG, MENCODER ,MPLAYER ENCODING ENGINE
# VIDEO CODEC ITU H264 AUDIO MP3


# Written by FakeOutdoorsman and updated by mysoogal and modified by Domzz
# Attribution-Noncommercial 3.0 Unported
# http://creativecommons.org/licenses/by-nc/3.0/deed.en
# trackback http://ubuntuforums.org/showthread.php?t=960627

# Location of source videos READ this !!! add your user name !
sourcelocation="/home/domz/torrents/"
# Extension of source videos
sourceext="avi,mpg,mpeg,mov,rmvb,rm,ogm,4v"
# Wait while any other ffmpeg processes are running
while [ -n "$(ps -ef | egrep "ffmpeg|HandBrakeCLI" | grep -v grep)" ];
do
        echo -e "\n[$(date +%b\ %d\ %Y:\ %H:%M:%S)]\nFound another instance of HandBrake or ffmpeg running, pausing 5 minutes..."
        sleep 300
done
#http://thomer.com/howtos/ipod_video.html
find ${sourcelocation} -iname "*${sourceext}" -exec HandBrakeCLI -i {} -o ${}.mp4  -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1
# Check to see if videos were encoded, then delete source vids and shutdown
if [ -e "${sourcelocation}/*.avi" ] && [ -e "${sourcelocation}/*.mp4]; then
	# Delete videos	
	for ext in avi mpeg mov mpg txt nfo; do rm ${sourcelocation}/*.$ext; done	
	#copys a txt file from my directory to sources directory
	cp /home/domz/file.txt ${sourcelocation}
	#moves directory
	mv ${sourcelocation} /home/admin/${sourcelocation}_domain.com
	#builds the torrent file using: http://claudiusmaximus.goto10.org/cm/torrent.html
	#http://www.torrent-invites.com/tutorials/52022-putty-complete-tutorial.html
	cd /home/domz
	mktorrent -v -p -a http://tracker.url -o {sourcelocation}_domain.com.torrent /home/domz/torrents
else
	echo "Encoding FAILED"
fi

exit

Find command with -exec will be used as:

find find ${sourcelocation} -iname "*${sourceext}" -exec blabal {} blabla \;

Seems your find command missed the last part.

And for this part, you need add a space after mp4:

if [ -e "${sourcelocation}/*.avi" ] && [ -e "${sourcelocation}/*.mp4 ]; then

Thanks, I figured I need the \; till later, but so far I got it to convert, it won't do the other functions.

---------- Post updated at 10:19 PM ---------- Previous update was at 09:54 PM ----------

Whenever I run the script, I get:

(handbrake correctly converts)

Rip done!
HandBrake has exited. 
/home/domz/everything: line 50: unexpected EOF while looking for matching `"'
/home/domz/everything: line 54: syntax error: unexpected end of file   

run the script by

/bin/bash -x yourscript

It will give you more messages to analyze.

thanks checking it now, what should I put in to delete the .avi from my .avi.mp4
cuz when it converts, it saves as full name with avi extension.:

blahblahblah.avi ----> blahblahblah.avi.mp4

---------- Post updated at 11:01 PM ---------- Previous update was at 10:38 PM ----------

Ok, so far I get...

(HandBrakeCli works fine)...

Rip done!
HandBrake has exited.
+ '[' -e '/home/domz/torrents/*.avi' ']'
+ echo 'Encoding FAILED'
Encoding FAILED
+ exit