This article keeps collecting useful command-line tools in Linux or Mac OS X (via homebrew). It will be continuously updated.

It includes the following functions:

  • tools for downloading videos or webpages;

  • useful tools on dealing with documents (.txt, .pdf, .djvu, etc.);

  • tricks on image operations, like resize, format conversion, etc.; and

  • basic video operations.

Download

YouTube videos

To check the available formats:

1
  youtube-dl -F VIDEO_URL

After downloading, trim the suffixed random ID strings:

1
  prename "s/-[A-z0-9-_]*.mp4/.mp4/" *.mp4

To download the best video with MP4 format:

1
  youtube-dl -f 'best[ext=mp4]' URL

To download the best MP4 with quality equal or less to 480p:

1
  youtube-dl -f 'best[ext=mp4][height<=480]' URL

To download the best MP4 with quality <= 480p and the best audio in M4A:

1
  youtube-dl -f 'bestvideo[ext=mp4][height<=480]+bestaudio[ext=m4a]' URL

To download items (e.g., 1, 2, …, 10, 12, 18):

1
  youtube-dl --playlist-items 1-10,12,18 PLAYLIST_URL

When the default file name is too long (by default, it uses the video title in Youtube), you may specify a file name by a random ID string plus the extension:

1
  youtube-dl -o "./%(id)s.%(ext)s" URL

If you encounter the following error due to Youtube age restriction,

  ERROR: Content Warning
  This video may be inappropriate for some users.
         Sign in to confirm your age

you may handle by explicitly specifying cookies

  • use Firefox adds-on Export cookies to export the webpage (which video you want to download) cookies to a place, e.g., ~/Downloads/cookies.txt;

  • pass this cookies file to youtube-dl as follows:

1
  youtube-dl --cookies ~/Downloads/cookies.txt -f 'best' URL

Web pages

Download all children webpages: wget -r -np -R "index.html*" URL

Documents

Plain Text

Convert non-unicode Chinese texts to UTF-8:

1
  iconv -f gbk -t utf-8 IN.txt > OUT.txt

The Chinese encoding majorly used include:

  • gbk

  • gb18030

  • gb2312

  • big5 for traditional Chinese

DjVu

Installation:

  • djvu encoding/decoding library support: brew install djvulibre

  • djvu default viewer: brew install djview (or brew install djview4 for its stable version 4)

  • tools to convert djvu to pdf:

    • use ddjvu command from djvulibre, or

    • install the tool: brew install djvu2pdf

Usages:

  • Break Djvu into separate pages:

    1
    
      $ djvmcvt -i input.djvu /path/to/out/dir output-index.djvu
  • Convert Djvu pages into images:

    1
    
      $ ddjvu --format=tiff page.djvu page.tiff
  • Convert Djvu pages into PDF:

    1
    
      $ ddjvu --format=pdf -quality=85 inputfile.djvu ouputfile.pdf

    or, use djvu2pdf tool:

    1
    
      $ djvu2pdf in.djvu
  • You can also use –page to export specific pages:

    1
    
      $ ddjvu --format=tiff --page=1-10 input.djvu output.tiff

    This will convert pages from 1 to 10 into one tiff file.

PDF

Add margins to PDF files:

  • use pdfcrop provided by TexLive

    1
    
      pdfcrop --margin '29 0 29 0' input.pdf output.pdf

    The four digits represent 'LEFT TOP RIGHT BOTTOM', respectively.

  • use pdfjam provided by TexLive to do the offset trick for the right and left pages

    1
    
      pdfjam --twoside --offset '1cm 0cm' file.pdf

Create a blank PDF file in A4:

1
  convert xc:none -page A4 blank.pdf

or, with a specific size:

1
  convert xc:none -page 842x595 blank.pdf

Dictionary

This article shows how to convert the BGL dictionary to the AppleDict format for Dictionary.app.

The key tool to convert formats is the Python package pyglossary on github. You may clone this project, or install from PyPI by $pip install pyglossary.

Moreover, we need additional tools to support AppleDict reading and writing:

  • Required Python libraries for AppleDict:

    • Reading from AppleDict Binary (.dictionary) sudo pip3 install lxml

    • Writing to AppleDict sudo pip3 install lxml beautifulsoup4 html5lib

  • Requirements for AppleDict on Mac OS X: if you want to convert glossaries into AppleDict format on Mac OS X, you also need:

    • GNU make as part of Command Line Tools for Xcode.

    • Dictionary Development Kit as part of Additional Tools for Xcode. Extract to /Applications/Utilities/Dictionary Development Kit (if using github clone), or ~/Developer/Extras/Dictionary Development Kit (if using pip installation).

Now it is ready to use pylossary to convert a BGL dictionary to AppleDict, e.g., "Merriam-Webster.Collegiate.Dictionary.BGL":

1
2
3
4
5
6
  pyglossary --write-format=AppleDict \
             Merriam-Webster.Collegiate.Dictionary.BGL Merriam-Webster

  cd Merriam-Webster/
  make
  make install

Images

Infomation

To display image meta information:
magick identify -verbose IN.png

Resize

Resize image with fixed ratios (the height and width may be exactly 64, which will be modified by the ratio):
convert dragon.gif -resize 64x64 resize_dragon.gif

Resize image and ignore ratios:
convert dragon.gif -resize 64x64\! exact_dragon.gif

Only shrink larger images:
convert dragon.gif -resize 64x64\> shrink_dragon.gif

Resize by percentages:
convert dragon.gif -resize 50% half_dragon.gif

Conversion

color

Convert color images into grayscale:
convert --colorspace gray IN.jpg OUT.jpg

SVG

DEPENDENCY: require the package librsvg; install by brew install librsvg

Convert img.svg to img.jpg with height set to 1500px:
rsvg-convert -h 1500 img.svg img.jpg

Convert img.svg to img.pdf (vector image):
rsvg-convert -f pdf -o img.pdf img.svg

JPEG/PNG

Convert img.png to img.jpg with density 300, quality 100 and trimmed:
convert -density 300 -trim -quality 100

webp

DEPENDENCY: require the package webp; install by brew install webp

Convert img.webp to img.jpg:
dwebp img.webp -o img.jpg

parallel conversion

Use 8-cores to convert images to pdf in parallel:

1
  parallel -j 8 convert {} -density 300 -quality 100 {.}.pdf ::: *.png

Combination

Concatenate all images vertically as a new one:
convert -append *.png out.png

Quality/Size reduction

Reduce file size of an image:
convert -resize 1000 -trim -quality 72 IN.jpg OUT.jpg

Videos

FFMPEG

Convert flv to mp4 without losing quality:

1
  ffmpeg -i INPUT.flv -c:v libx264 -crf 19 -strict experimental OUTPUT.mp4

Alternatively, the following only changes container without reencoding:

1
  ffmpeg -i INPUT.flv -codec copy OUTPUT.mp4

AVCONV

Combine video file mp4 with sound track m4a (useful when you download video and audio files separately):

1
  avconv -y -i in.mp4 -i in.m4a -c copy out.mp4