MP3 watch folder using avconv and monit - Linux

Create a watch folder with Monit - copy media files into the IN folder, and MP3 versions show up in the OUT folder. More than giving hints, this is a complete guide to creating a working watch folder.

Make some simple changes and you can transcode to many different audio and video formats. Create a watch folder for each type of file you need to transcode to. If you do not mind working with a command line, you can create your own transcoding watch folders similar to what you get with expensive Windows based software.

Also use this guide to set up any script to run on a regular basis. This is similar to a cron job, but monit will not run additional instances of a script that is still running.

Prerequisites: Access to a computer running Linux OS, ability to create files using an editor such as Gedit, joe, vi, etc, and ability to type commands in the terminal. These scripts and instructions work with Ubuntu 12 LTS, and should work with many other versions of Linux.

The first step is to create a main script. Copy the contents of the boxes to the clipboard and paste them into your editor. You could place the script anywhere, perhaps /home/yourusername.

#!/bin/bash

# Transcodes files to MP3 Audio
# When used with a wrapper for Monit, creates a watch folder

# Variables
INPATH=/WATCH/MP3-Audio/IN 
OUTPATH=/WATCH/MP3-Audio/OUT

# For monit - write the PID to a file
PIDD=$$
echo $PIDD > /var/run/MP3Watch.pid;

# Finished if folder is empty 
if [ "$(ls -A $INPATH)" ]
then

# Process every file in source folder
for i in $INPATH/$i*
do
    
# Rename Input File - substitute underscores for spaces
OLDNAME=$i
NEWNAME=${i// /_}
mv "$OLDNAME" $NEWNAME
BASENAME1=`basename $NEWNAME`
# strip extension
OUTNAME=${BASENAME1%.*}
OUTNAMEXT=$OUTNAME.mp3

# Transcode file to mp3 

avconv -i $NEWNAME -y -b 128k $OUTPATH/$OUTNAMEXT

# delete original file
rm $NEWNAME

done 

else
# pause this script so that it shows up in Monit sometimes
echo "Nothing to do - taking a break"
 sleep 2m
fi

echo "Nothing else to do, will check back a bit later."
echo BYE

Save this script as /home/yourusername/MP3Watch.sh where yourusername is a folder that exists in /home.
Don't forget to make it executable.

chmod +x MP3Watch.sh

You may have to prefix commands with sudo.

sudo chmod +x MP3Watch.sh

Create the IN and OUT folders.

sudo mkdir /watch
cd /watch
sudo mkdir IN
cd ../
sudo mkdir OUT

Optional: If you want everyone on your local network to have access to your watch folder, you need to share it (search for Linux Samba file sharing), and you need to change permissions.

sudo chmod 777 -R watch

Now test the script by copying at least one media file to the IN folder and running the script.

cd  /home/yourusername
sudo ./MP3Watch.sh

You should have an MP3 file in the OUT folder when the script finishes.

The next step is to create a wrapper script that monit uses to start and stop the main script.

#!/bin/bash

# monit wrapper

case $1 in
start)
exec /home/yourusername/MP3Watch.sh 
;;
stop)
kill `cat /var/run/MP3Watch.pid`
;;
*)
echo "usage: ./mwrapMP3 {start|stop}"
;;
esac
exit 0

Save this script as /usr/bin/mwrapMP3.sh and don't forget to make it executable.

chmod +x mwrapMP3.sh

If you save this in your home folder, monit probably will not be able to run it, so save it where I suggest.

Test the wrapper script:

cd /usr/bin
sudo ./mwrapMP3.sh start

You should see the main script run.

Install monit if you have not already done so.

sudo apt-get install monit

Open /etc/monit/monitrc and add the following to the end of the file:

check process MP3Watch with pidfile /var/run/MP3Watch.pid
       start = "/usr/bin/mwrapMP3.sh start"
       stop = "/usr/bin/mwrapMP3.sh stop"

Save the file, and run a monit check.

sudo monit -t

If you do not see any errors, reload monit.

sudo monit reload

Your watch folder should now be active. Try it out, and be patient because the default monit settings may delay startup for a minute or two.

For a status report:

sudo monit status

Note: the "Does not exist" status is normal. It just means that the main script is not running at the moment. If you included the "sleep" command in the main script, then the status will show "running" sometimes. When the script has nothing to do it will stop, and monit will restart it after awhile.

Test your watch folder by putting different types files into the IN folder. The script will output audio MP3 files from most media files -- even video files. The watch folder uses avconv for transcoding, and ffmpeg works also.


Now that monit is running your watch folder, learn how to do other neat things such as viewing your monit status via a web page. See more detailed documentation on monit here.


Tip for Windows users - copy clip board into terminal:

Did you know that if you use Putty on a Windows computer to open a terminal session (SSH) on your Linux computer, a simple right click in your Putty window will paste the clipboard contents to your terminal session? You can also copy and paste between Putty windows.


Charles Young likes figuring out how to do useful things with computers, is amazed at the amount of free stuff for Linux, but has noticed that many Linux instructions assume that the audience already knows much and may not need complete working examples. chdyoung@gmail.com

Home