Cheat Sheets

Jul 21 2022 · LastMod: Mar 17 2023

PDF Editing

Extracting and rotating pages

Extract pages:

1qpdf [INPUT] --pages . [start]-[end] -- [OUTPUT]

run with the option --replace-input and without [OUTPUT] to substitute the [INPUT] file.

Rotate pages:

1qpdf [INPUT] [OUTPUT] --rotate=[+|-][ANGLE]:[PAGE_RANGE]

Compression + Monochromize

  1. Split pages with qpdf --split-pages in.pdf out.pdf
  2. Compress each page with convert -monochrome -compress lzw -density 300 [INPUT] [OUTPUT] (use parallel, with seq -w [1 801])
  3. Merge outpus with qpdf --empty --pages *.pdf -- out.pdf

A better alternative to 2. convert -density 300 out-017.pdf -threshold 90% -type bilevel -compress fax try.pdf


1ocrmypdf -l [LANG] --optimize [1,2,3] [INPUT] [OUTPUT]

Run tesseract --list-langs to see all available languages

To get rid of additional blank spaces,

1echo preserve_interword_spaces 1 > config
2ocrmypdf --tesseract-config config [...]

Removing text

1gs -o no-more-texts.pdf -sDEVICE=pdfwrite -dFILTERTEXT   Original.pdf


Querying Available Codecs and Formats

1ffmpeg -codecs
2ffmpeg -formats

Extracting Streams

1ffmpeg -i [INPUT] [-map 0:1 -map 0:2] -c:a [AUDIO CODEC] -c:v [VIDEO CODEC] 01_1.mkv


1ffmpeg -ss [TIME_START] -i [FILE] -ss [+TIME_START] -t [TIME_DURATION] -c copy [OUTPUT]


1ffmpeg -i [FILE] -ss [TIME_START] -to [TIME_END] -c copy [OUTPUT]

Adding Subtitles

Add Subtitle with

1ffmpeg -i [VIDEO_FILE] -vf subtitles=[.ASS_FILE] -c:a copy [OUTPUT]

.ass format example:

 1[Script Info]
 2Title: [TITLE]
 3Original Script: RoRo
 4Script Updated By: version 2.8.01
 5ScriptType: v4.00+
 6Collisions: Normal
 7PlayResY: 600
 8PlayDepth: 0
 9Timer: 100,0000
10Video Aspect Ratio: 0
11Video Zoom: 6
12Video Position: 0
14[V4+ Styles]
15Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
16Style: DefaultVCD, Arial,28,&H00B4FCFC,&H00B4FCFC,&H00000008,&H80000008,-1,0,0,0,100,100,0.00,0.00,1,1.00,2.00,2,30,30,30,0
19Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
20Dialogue: 0,0:00:01.18,0:00:06.85,DefaultVCD, NTP,0000,0000,0000,,{\pos(400,570)}The first line\NThe second line in subtitle

Downsampling FLAC

For instance,

1ffmpeg -i input.flac -af "aresample=44100:resampler=soxr:precision=33:osf=s16:dither_method=shibata" -acodec flac output.m4a

Changing Audio Quality for mp3

To 320k

1ffmpeg -i input -c:a libmp3lame -b:a 320k -o output.mp3

Stripping metadata

1ffmpeg -i in.mkv -map_metadata -1 -c:v copy -c:a copy out.mkv
2ffmpeg -i tagged.mp3 -write_xing 0 -id3v2_version 0 untagged.mp3

or use

1id3v2 -D input.mp3

Generating visualizations

1ffmpeg -i input.flac -filter_complex \
2"[0:a]avectorscope=s=640x518,pad=1280:720[vs]; \
3 [0:a]showspectrum=mode=separate:color=intensity:scale=cbrt:s=640x518[ss]; \
4 [0:a]showwaves=s=1280x202:mode=line[sw]; \
5 [vs][ss]overlay=w[bg]; \
6 [bg][sw]overlay=0:H-h,drawtext=fontfile=/usr/share/fonts/TTF/Vera.ttf:fontcolor=white:x=10:y=10:text='\"Song Title\" by Artist'[out]" \
7-map "[out]" -map 0:a -c:v libx264 -preset fast -crf 18 -c:a copy output.mkv

without text

1ffmpeg -i input.flac -filter_complex \
2"[0:a]avectorscope=s=640x518,pad=1280:720[vs]; \
3 [0:a]showspectrum=mode=separate:color=intensity:scale=cbrt:s=640x518[ss]; \
4 [0:a]showwaves=s=1280x202:mode=line[sw]; \
5 [vs][ss]overlay=w[bg]; \
6 [bg][sw]overlay=0:H-h" \
7 -map "[out]" -map 0:a -c:v libx264 -preset fast -crf 18 -c:a copy output.mkv