-   Linux - Software (
-   -   Script to automatically convert video files using ffmpeg (

jroyce 01-03-2008 01:11 PM

Script to automatically convert video files using ffmpeg
I have a Linux box which resides on the same network as my TiVo. I spend a lot of time converting video from AVI and other formats to MPEG-2 so that I can view it on my TV via the TiVo. The conversion is done via ffmpeg.

Recently, I started thinking that it would be cool to write a script which would run daily and check the contents of a particular directory for new files. If any new files exist, it would then run the ffmpeg command to convert them and place the resulting MPEG-2 in my TiVo Recordings folder.

I imagine the algorithm would look like this:

Read contents of directory (e.g. /home/user/videos/queued video
Count number of files and parse file names into an array called INPUT_NAME
Run a FOR loop which contains the following commands:

ffmpeg -i INPUT_NAME(n) -target ntsc-dvd /home/user/videos/TiVo Recordings/INPUT_NAME(n).MPG
mv INPUT_NAME(n) /home/user/videos/originals

Is it possible to do this in a perl script?

I could easily write this in Visual Basic, but I am not familiar enough with Perl to know how to handle the variables. (Is there a good tutorial?)

Also, is there a good way to schedule Perl scripts to run daily?

Thanks in advance!

i_grok 01-03-2008 02:12 PM

Honestly, this is simple enough to do in a bash shell script. Shell scripts are just files with a list of commands that you would normally enter into a terminal manually.

That way, you can use the find utility to determine which files are less than 24 hours old and convert them.

You'll have something like:

# Convert files for Tivo


for i in `find $DIR -type f -mmin -1440`; do

  ffmpeg -i $i -target ntsc-dvd /home/user/videos/TiVo\ Recordings/$i.MPG
  mv $i /home/user/videos/originals


Then create a symlink in /etc/cron.daily/ to the shell script (which will need to be executable).

jroyce 01-04-2008 12:58 PM

Thanks i_grok!
This is exactly what I was looking to do. One more question: Is it fair to assume that if I change the code to read:


# Convert files for Tivo


for i in `find $DIR -type f *`; do

  ffmpeg -i $i -target ntsc-dvd /media/TiVo/GoBack/$i.MPG
  mv $i /media/TiVo/original


that the script will convert ALL files in the 'DIR' directory (not just those modified in the last 24 hours)?

Also, any tips on a good Bash Shell Script tutorial online?

i_grok 01-05-2008 12:17 AM

You'll want the for line to read:

for i in `find $DIR -type f`; do
You don't need to use the asterisk with find - you just give it the directory to search in. Note that, by default, this will be recursive and find every file in that directory.

You can run the find command outside of the script to see what files it will return.

As far as bash tutorials, I pretty much just search for "bash tutorial" on Google whenever I need to check something. There's not a particular resource to use, except maybe for the bash manpage ;)

All times are GMT -5. The time now is 03:15 AM.