Log In | Users | Register
All about IT at IAC
spacer
Edit | Attach | New | Raw | Delete | History | Diff | Print | Pdf | | Tools
You are here: IT » LinuxConvertFiles

Converting Files under Linux

Convert Images

  • Use the powerful command convert
    convert file.jpg file.gif
    convert file.tiff file.jpg
    
    The command convert can convert between several formats and has a lot of options, see man convert

  • Resizing an image
    convert -resize 50%       file.jpg file_small.jpg
    convert -resize 200x200   file.jpg file_small.jpg      # ratio will be kept
    convert -resize 1024x768! file.jpg file_desktop.jpg    # ! = force the new size
    

  • Rotating an image
    convert -rotate 90 file.jpg file_new.jpg
    


Convert between Windows/Mac and Linux text files

  • Use dos2unix and unix2dos
    dos2unix win.txt linux.txt
    mac2unix mac.txt linux.txt
    unix2dos linux.txt win_mac.txt
    


Convert text files between different character encodings

  • Use iconv to convert a text file
    iconv --from-code=UTF-8 --to-code=ISO-8859-1 text_in_utf-8.txt > text_in_iso-8859-1.txt
    


Convert PS and EPS Files

There is normally more than one way to convert files. Just try out which tool produces the best results in your case.

  • Convert PS to PDF
    ps2pdf file.ps
    ps2pdf -sPAPERSIZE=a4 -dOptimize=true -dEmbedAllFonts=true file.ps
    pstoedit file.ps file.pdf
    

  • Convert EPS to PDF
    epstopdf file.eps
    pstoedit file.eps file.pdf
    

  • Convert PS to EPS
    ps2eps file.ps
    

  • Convert to a "better" PS
    ps2ps   file.ps  new.ps    
    eps2eps file.eps new.eps   
    ps2ps2  file.[ps|eps|pdf] new.ps    # converts to PS level 2
    

  • Extract text from PS
    ps2ascii file.ps file.txt
    

  • Convert a postscript file to png
    gs -r300 -dNOPAUSE -dUseCropBox -dBATCH -sDEVICE=pngalpha -sOutputFile=file.png file.ps    # r300 = resolution of 300 dpi
    

  • Convert a postscript file to jpg
    gs -r300 -dNOPAUSE -dUseCropBox -dBATCH -sDEVICE=jpeg -sOutputFile=file.jpg file.ps    # r300 = resolution of 300 dpi
    

  • Convert a transparent eps (in cmyk) to a png file
    convert -colorspace rgb file.eps file.png
    


Convert GRIB into NETCDF files

  • Use NCL to convert. First load ncl module
    module load ncl
    

  • Rename your GRIB file. The name should have .grb as extension
    mv mygribfile mygribfile.grb
    

  • Convert to NETCDF
    ncl_convert2nc mygribfile.grb
    
    The resulting NETCDF file will be mygribfile.nc


Convert PDF Files

  • Use pdftops to convert PDF to PS
    pdftops file.pdf
    
    Note: pdf2ps does also convert PDF to PS, but normally the result is worse.

  • Extract text from PDF
    pdftotext file.pdf
    # or use ps2ascii
    pdftops file.pdf
    ps2ascii file.ps file.txt
    

  • Convert a PDF file to images (one image per page)
    pdftoppm file.pdf image
    
    Convert page 2 (-f 2) to page 5 (-l 5) with a resoltion of 300 dpi (-r 300) to five PPM image files
    pdftoppm -f 2 -l 5 -r 300 file.pdf image
    

  • Extract images from PDF files
    pdfimages file.pdf image
    pdfimages -j file.pdf image    # write images if possible as JPEG files
    

  • Open the PDF File with an application and save it in a different format. Programs that can open PDF files:
    abiword
    oowriter
    


Manipulating PDF Files

  • Use pdfedit to edit PDF Files
    pdfedit file.pdf
    

  • Joining PDF files
    pdfunite file1.pdf file2.pdf file-new.pdf
    pdfjoin file1.pdf file2.pdf                # alternative
    

  • Splitting a PDF file into pages
    qpdf --split-pages file.pdf %d-out.pdf
    

  • Extract certain pages from a PDF. For example page 22-36
    qpdf input.pdf --pages . 1-10 -- output.pdf
    pdftops input.pdf - | psselect -p22-36 | ps2pdf - output_p22-p36.pdf
    

  • Extract images from PDF files
    pdfimages -j file.pdf image
    

  • Rotating PDF file
    pdf90 file.pdf
    

  • Put several pages on one page:
    pdfnup --nup 2x1 file.pdf     # 2 pages side by side
    pdfnup --nup 2x2 file.pdf     # 4 pages on one page
    


Convert Matlab EPS Files

  • Convert Matlab eps file in a pixel graphic like png, jpg: First convert the eps file into a pdf file. Afterwards convert the pdf file into a png or jpg file
    epstopdf matlab_plot.eps 
    convert -density 100 matlab_plot.pdf matlab_plot.png
    
    • Choose a higher number than 100 to get a higher resolution of the image
    • Instead of png you can convert to matlab_plot.jpg

  • Convert Matlab eps file into a .emf file (Enhanced Meta File format). emf is still a vector based format and can be imported by openoffice.
    pstoedit -pta matlab_plot.eps matlab_plot.emf
    
    The crucial option is -pta, which fixes the ugly looking font when you directly import the eps file into openofffice (-pta sets correct inter-letter spacing).
    With the above command the boundary box will be still broken (too large). If you first convert it into a pdf file it will be ok.
    epstopdf matlab_plot.eps
    pstoedit -pta matlab_plot.pdf matlab_plot.emf
    


Convert tar files into zip files

  • Windows users may not be able to open tar or tar.gz files. The following commands "convert" these files to zip files
    tar xfv  file.tar    | zip file.zip "-@"
    tar xfvz file.tar.gz | zip file.zip "-@"
    


Convert MS Excel and MS Word files

  • Convert xls (Excel) files to csv (comma-separated values) files:
    xls2csv -x file.xls -c file.cvs -a UTF-8
    

  • Convert MS Word to plain text:
    abiword --to=txt file.doc
    

  • Alternatively open the file with LibreOffice.


Create a Movie out of Images

Programs

Create animated gif with convert

  • Animated gif file created from a series of png or gif files ( see also man convert):
    convert -delay 30 -loop 0 *.png animated.gif
    convert -delay 30 -loop 0 *.gif animated.gif
    

mencoder takes jpg or png files as imput. Note: mencoder needs png with 8bit color depth, use option -depth 8

  • Convert eps to png files
    mogrify -format png -depth 8 -alpha off -density 600 -resample 150 *.eps
    
  • Convert jpg tp png files and keep the quality
    mogrify -format png -depth 8 -quality 100 *.jpg
    

Create movies with mencoder (see also man mencoder)

  • Create AVI movie file with MPEG4 codec (fps = frames per second):
    mencoder mf://*.jpg -mf fps=5 -ovc lavc -o output_mpeg4.avi
    mencoder mf://*.png -mf fps=5 -ovc lavc -o output_mpeg4.avi
    
  • Create AVI file with XVID codec
    mencoder mf://*.png -mf fps=5 -ovc xvid -xvidencopts fixed_quant=4  -o output_xvid.avi
    
  • Create AVI file with X264 codec (use 2 pass mode: pass=1 will analyze the frames, pass=2 will create the movie)
    mencoder mf://*.png -mf fps=5 -o /dev/null       -ovc x264 -x264encopts pass=1:turbo:bitrate=1600:bframes=1:me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300
    mencoder mf://*.png -mf fps=5 -o output_x264.avi -ovc x264 -x264encopts pass=2:turbo:bitrate=1600:bframes=1:me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300
    
  • X264 and XVID codecs create smaller and qualitative better files than MPEG4 codec, where has X264 creates smaller files than XVID.
  • Note: For mencoder the file should not be too large. 1000x1000 is still fine


Convert Movie/Animation Files

  • Convert MPEG4 movie to Quicktime (qtrle) movie
    ffmpeg -i output_mpeg4.avi -f mov -vcodec qtrle output.mov 
    

  • Convert animated GIF into MPEG4 movie. First extract individual pictures out of the animated gif and save them as png files
    convert animation.gif animation%02d.png
    
    Now create out of the png files a MPEG4 movie
    mencoder mf://*.png -mf fps=5 -ovc lavc -o output_mpeg4.avi
    

  • Converting Flash movie
    ffmpeg -i myvideo.flv -f avi -vcodec mpeg4 myvideo_mpeg4.avi
    


Convert Audio Files

  • Convert WAV to MP3. Set the bit rate with option -b
    lame -b 320 file.wav file.mp3
    

  • Convert WAV to OGG. Set quality between -1 (very low) and 10 (very high) with option -q:
    oggenc -q 8 file.wav 
    

  • Convert audio files using sox
    sox file.mp3 file.ogg
    sox file.ogg file.mp3
    

  • Convert media files using mplayer. Mplayer can convert every file format, which he can play, to .wav
    mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader:file=output.wav input.xxx
    

  • Converting a bunch of files using a for loop (works in bash shell only!)
    for f in *.wav;  do lame -b 320 "$f" "${f%.wav}.mp3"; done
    for f in *.flac; do flac -cd "$f" | lame -b 320 - "${f%.flac}.mp3"; done
    for f in *.ogg;  do sox "$f" "${f%.ogg}.mp3"; done
    for f in *.ogg;  do oggdec -o - "$f" | lame -b 256 - "${f%.ogg}.mp3"; done
    for f in *.wma ; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader "$f" && lame -b 256 audiodump.wav -o "${f%.wma}.mp3"; done; rm -f audiodump.wav 
    


Extract Audio from a Video File

  • Extract MP3 audio from MP4 video
    for f in *.mp4; do ffmpeg -i "$f" -vn -ar 44100 -ac 2 -ab 320k -f mp3 "${f%.mp4}.mp3"; done
    
  • Extract MP3 audio from MKV video
    for f in *.mkv; do ffmpeg -i "$f" -vn -ar 44100 -ac 2 -ab 320k -f mp3 "${f%.mkv}.mp3"; done
    

spacer

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Wiki? Send feedback
Syndicate this site RSS ATOM