Converting Files with Linux

The following tipps work under a Linux terminal and were tested with Ubuntu 10.04 LTS.

I guess they will also work with other systems, as the programs are available for them.

If you know some further file conversions, please let me know.

I am also very interested in Web based conversions.

Image Files

If you want to change image files via terminal, ImageMagick is a good choice.

Resize Images to a maximum resolution

convert "OldPicture.jpg" -resize 1600x1600 "NewPicture.jpg"

You can also do this for a whole folder. Just go into that folder and:

for i in *.jpg; do convert $i -resize 1600x1600 $i; done

In case you have just taken many photos of a document and you want to send it as a single PDF via email. Thats the way to go:

for i in *.JPG; do convert $i -resize 1200x1200 $i; done; convert *.JPG merged.pdf

Create a Black-and-white picture and compress it

djpeg "OldPicture.jpg" | ppmtopgm | cjpeg -qual 70 >"NewPicture.jpg"

Rename Pictures:

rename -n ’s/\.jpg`$/\.JPG/’ *.jpg

Audio Files

Give all mp3 songs the same sound level (it's called Audio normalization):

mp3gain -a *.mp3

Merge many audio files to one:

mp3wrap merged.mp3 one.mp3 two.mp3

Convert all *.wav-files in one folder two *.mp3-files and remove the *.wav-files:

for i in *.wav;do lame "$`i" "`${i%wav}mp3"; rm "$`i"; done

Video Files

For quite a lot purposes is the command line tool FFmpeg with its lots of options a good choice. For others might MEncoder be better. You might also want to install some codecs first:

sudo apt-get install libavcodec-extra-52 libavdevice-extra-52 libavformat-extra-52 libavutil-extra-50 libpostproc-extra-51 libswscale-extra-0 libavcodec-unstripped-52 ubuntu-restricted-extras

Merge many video files to one:

cat One.mpg Two.mpg Three.mpg | ffmpeg -f mpeg -i - -vcodec copy -acodec copy "Merged.mpg"

avi2mpg:

ffmpeg -i "Original.avi" "New.mpg"

mp42mpg:

ffmpeg -i "Original.mp4" -target ntsc-vcd "New.mpg"

mod2avi: ?

vcd2avi:

mencoder vcd://2 -o "New.avi" -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=2000

ogv2avi:

mencoder "Original.ogv" -ovc xvid -oac mp3lame -xvidencopts pass=1 -o "New.avi"

wmv2mpg: aspect=16/9 should eventually be changed to 4/3 or other aspects

mencoder -of avi -ofps 25 \
  -oac mp3lame -lameopts cbr:br=112:aq=3:mode=0:vol=0 \
  -vf hqdn3d,softskip,harddup \
  -ovc xvid \
  -xvidencopts bitrate=501:max_key_interval=37:aspect=16/9:turbo:nochroma_me:notrellis:max_bframes=0:vhq=0 \
  Original.wmv \
  -o New.avi

mkv2avi:

mencoder "Original.mkv" -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=6000 -oac mp3lame -lameopts vbr=3 -o "New.avi"

Converting Flash Videos flv to mpg

You might want to get the information of the video first:

ffmpeg -i inputVideo.flv

This is how you convert it:

ffmpeg -i inputVideo.flv -acodec libmp3lame -ab 64k -s 320x240 -r 24 outputVideo.mpg

-i input file -acodec audio codec -ab audio bitrate -s size -r fps where fps is the frame rate in Hz. The default value is 25Hz.

Converting Flash Videos flv to avi

ffmpeg -i inputVideo.flv -sameq -ab 128k outputVideo.avi

Shortcuts for Linux Console

I convert svg2png or pdf2png quite often for my articles. So I've created a command.

You can create a command in Linux very easy:

  1. Enter echo $PATH in your console
  2. Go to /usr/bin or any other path in your PATH
  3. Create a file with the name of your command (e.g. svg2png)
  4. Fill the fill (see below for some examples).
  5. Make it executable: chmod +x svg2png

My svg2png looks like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse

parser = argparse.ArgumentParser(description="convert a svg file to png")
parser.add_argument("-i", "--input", dest="input",
                  help="read svg file", metavar="FILE")
parser.add_argument("-o", "--output", dest="output",
                  help="output png file", metavar="FILE")
parser.add_argument("-w", "--width", dest="width", default=512, type=int, 
                  help="width of output png")

args = parser.parse_args()

import os
command = "inkscape " + args.input + \
          " -w " + str(args.width) + " --export-png=" + args.output
os.system(command)
print("Executed command: " + command)

My pdf2png looks like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse

parser = argparse.ArgumentParser(description="convert a svg file to png")
parser.add_argument("-i", "--input", dest="input",
                  help="read svg file", metavar="FILE")
parser.add_argument("-o", "--output", dest="output",
                  help="output png file", metavar="FILE")
parser.add_argument("-w", "--width", dest="width", default=512, type=int, 
                  help="width of output png")

args = parser.parse_args()

import os
commands=[]
commands.append("pdf2svg "+args.input+" ~"+args.input+".svg")
commands.append("inkscape ~"+args.input+".svg --export-plain-svg=~"+args.input+".svg")
commands.append("svg2png -i ~"+args.input+".svg -o "+args.output+" -w " + str(args.width))
commands.append("rm ~"+args.input+".svg")
for command in commands:
    os.system(command)
    print("Executed command: " + command)

args = parser.parse_args()

Leave a Reply

comments powered by Disqus