Skip to content

Instantly share code, notes, and snippets.

@rueycheng
Last active February 12, 2023 14:45
Show Gist options
  • Save rueycheng/7d58552bea227efc02bffb25abb528d5 to your computer and use it in GitHub Desktop.
Save rueycheng/7d58552bea227efc02bffb25abb528d5 to your computer and use it in GitHub Desktop.
Commonly used Unix recipes

Create a blank-page PDF file

https://unix.stackexchange.com/questions/277892/how-do-i-create-a-blank-pdf-from-the-command-line

convert xc:none -page a4 blankpage.pdf

Convert PDF to greyscale

https://superuser.com/questions/104656/convert-a-pdf-to-greyscale-on-the-command-line-in-floss

gs -sOutputFile=output.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH input.pdf

Reduce scanned PDF file size

The imagemagick approach is based on lossy compression and works the best with scans. Otherwise ghostscript can downsize the PDF quite effectively without sacrificing much of the quality.

https://askubuntu.com/questions/113544/how-can-i-reduce-the-file-size-of-a-scanned-pdf-file

convert -density 100x100 -quality 60 -compress jpeg input.pdf output.pdf

https://gist.github.com/firstdoit/6390547

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Make low-resolution scans

This might just work when needing to submit ID documents to someone to see

convert -density 120 input.pdf output.pdf

Convert m4a file to mp3

https://gist.github.com/protrolium/e0dbd4bb0f1a396fcb55

ffmpeg -i input.m4a -f mp3 -ab 192k -vn output.mp3

Convert mp3 file bitrate

#!/bin/bash

if [ $# -lt 2 ]; then
    echo "usage: $(basename $0) <src> <dest>" >&2
    exit
fi

src=${1%%/}
dest=${2%%/}

find "$src" -name '*.mp3' -print0 | 
    while IFS= read -r -d '' input_file; do 
        name=${input_file##"$src"/}
        output_file="$dest/$name"
        output_dir="$(dirname "$output_file")"

        mkdir -p "$output_dir"
        ffmpeg -i "$input_file" -f mp3 -ab 192k -vn "$output_file" </dev/null
    done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment