Archives for 16 May,2009

You are browsing the site archives by date.

Bash: mass image and video conversions

Linux has many useful command line programs that will convert from one file format to another.

  • convert – converts images (part of ImageMagick)
  • convert input.jpg -resize 500×500 output.jpg

    converts input.jpg to output.jpg resized to 500 by 500 pixels

  • mencoder – converts video files
  • mencoder -ovc lavc -oac mp3lame -o output.avi input.flv

    converts input.flv to output.avi

    Now lets look at some bash:

    for i in *.jpg; do echo $i; done

    This is a for loop which will assign and iterate over a variable i to every item it finds that matches *.jpg in the current directory. It will simple echo the name of the file to the console and then finish. Only really useful for testing the command works, now lets put a conversion command in instead of the echo.

    for i in *.jpg; do convert “$i” -resize 500×500 “x_$i”; done

    This will take every jpg in the current directory resize it to 500 by 500 pixels and then save it as with and x_ prefixing the filename. You can get it to overwrite the current file by making the input and output file names equal (in this case it simple means removing x_ from the above code)

    Read More