How To: Convert Avi To Mpg On The Linux CLI

Here’s a script to convert a batch of avi files to mpg. I’ve commented out a version of the command which specifies the bitrate, and am using sameq (same quality); you can experiment with the commented out one if that suits your needs better.


#!/bin/sh

for file in *.AVI *.avi
do
echo "Transcoding $file to MPG..."
# ffmpeg -r 30 -b 1600 -i $file $file.mpg
ffmpeg -sameq -i $file $file.mpg
done

Comments (7)

  1. Doug wrote::

    This is not working for me?
    It simply spits out the help text for ffmpeg.
    Something changed maybe?

    thanks

    Sunday, March 18, 2007 at 3:04 pm #
  2. Derek Cordeiro wrote::

    Something is wrong with the CSS of this page. Please delete the above comment

    Sunday, April 29, 2007 at 3:45 am #
  3. Reuben wrote::

    Thanks. Actually I enabled wiki formatting, so the #’s were being converted to bullets. It’s working now tho.

    Tuesday, May 1, 2007 at 8:43 am #
  4. toby wrote::

    Interesting, but unable to get to work, same results as first commenter. Just prints the help file, along with a statement that the avi file is corrupt or truncated. Any Ideas???

    ONe other thing I noticed once the script finished; created new empty file *.avi.mpg Anyway to get rid of the .avi between the file and .mpg?

    Saturday, February 2, 2008 at 5:45 pm #
  5. Eric wrote::

    pass $file thru sed to strip the .avi extension.

    1. !/bin/sh

    for file in *.avi
    do
    outfile=`echo $file | sed ’s/.avi//’`
    ffmpeg -sameq -i $file $outfile.mpg
    done

    Tuesday, March 3, 2009 at 9:29 pm #
  6. Peter Eric Williams wrote::

    Hi All.

    I am a newbie at script writing and I am lost !!!

    I understand how to use a text editor and to copy and paste the script into a new file. I don’t understand these things:

    1) what do I name the file

    2) where do I save the file so that my system (Ubuntu 10.04 LTS) will find it without me needing to type in the full path? Or should I simply save it to my /home folder?

    3) what is the command to run the file? I don’t understand how to run .sh files?

    Btw — I am old school and I am 48 and been using computers since I was 9 years old. I guess that there is a learning curve when it comes to finding out how to write and run shell scripts. Yes, I am familiar with how batch files work with MS-DOS

    Peter
    pew@iinet.net.au

    All comments gladly welcomed
    Hobart, Austraia

    Saturday, June 19, 2010 at 2:58 am #
  7. Reuben wrote::

    Hello,

    You can name the file anything you like. Naming them .sh is just a convention, but is not necessary.

    The magic is to make the file executable. You can do this via the file manager UI (I’m guessing nautilus, since you are on Gnome) – right click on the file, permissions, allow execute. You can also do this from a terminal – type: chmod +x myscript (where myscript is the name of the file); you can then run it by typing: ./myscript. See http://en.wikipedia.org/wiki/Chmod for more information.

    HTH

    Monday, June 21, 2010 at 6:41 pm #