• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Converting Files with Linux

Contents

  • Converting Files with Linux
    • PDF
    • Image Files
    • Audio Files
    • Video Files
      • Converting Flash Videos flv to mpg
    • Converting Flash Videos flv to avi
    • Shortcuts for Linux Console

The following tips 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.

PDF

Convert a folder of PDF slides into text files of the same name. This is nice for using grep:

$ for file in *.pdf;do pdftotext "$file"; done

Convert PS to PDF with ghostscript (source):

$ ps2pdf input.ps output.pdf

Merge multiple PDFs into one (source)

$ pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf

pdf2jpg: Imagemagick does the trick

$ convert -density 300 in.pdf -quality 90 out.jpg

Convert png files to one PDF in A4 format:

i=300; convert soca-trail-1.png soca-trail-2.png soca-trail-3.png soca-trail-complete.png -compress jpeg -quality 100 \
      -density ${i}x${i} -units PixelsPerInch \
      -resize $((i*827/100))x$((i*1169/100)) \
      -gravity center \
      -extent $((i*827/100))x$((i*1169/100)) multipage.pdf

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. That's 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"

webp2png: First install webp, then

$ dwebp OldPicture.webp -o NewPicture.png

png2webp: First install webp, then

$ cwebp OldPicture.png -o NewPicture.webp

Rename Pictures:

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

Animations to series of images

Note that you might have to adjust the %02d if your animation has more than 100 frames (two digits):

$ convert -coalesce animated.gif single-image-%02d.png

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

Convert an mp3 file to a PCM wav file:

$ mplayer -ao pcm:fast:waveheader:file=output.wav -vo null -vc null input.mp3

Minimize file size:

$ lame -b 32 input.mp3 output.mp3

Split MP3 by silence:

$ mp3splt -s -p th=-40,min=3,rm input.mp3

Parameters (thank you, Victor!):

  • -s: silence mode
  • -p: specify arguments for the silence mode
  • th: threshold level in dB to be considered silence
  • min: minimum number of seconds to be considered as splitpoint
  • rm: remove silence from splitted files

Split MP3 by time:

$ mp3splt input.mp3 -t 10.00

splits the mp3 after every MM.SS, in this case after every 10 minutes and 0 seconds. So a 53 minutes MP3 would get splitted 5 times and hence result in 6 new files.

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"

mp42ogv (without sound; e.g. for Wikimedia Commons):

$ ffmpeg -i input.mp4 -c:v libtheora -q:v 7 -an output.ogv

mp42mp3:

$ ffmpeg -i Original.mp4 -f mp3 -ab 192000 -vn New.mp3

mod2avi: ?

mts2avi (MTS format info):

$ ffmpeg -i 00008.MTS -acodec copy -vcodec libx264 -crf 21 -r 30000/1001 -deinterlace -y -threads 0 output_file.avi

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()

Published

Sep 21, 2011
by Martin Thoma

Category

Code

Tags

  • cheat sheet 5
  • Command Line 7
  • conversion 1
  • Linux 17
  • PDF 4
  • Ubuntu 7

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor