UP | HOME
Sachin Patil

Sachin Patil

Free Software Developer | GNU Emacs Hacker

[Notes] Handy commands (Command that works!)
Published on Oct 09, 2016 by Sachin.

List of commands that I find useful.

(Last updated on Dec 09, 2022)

PDF tricks1

  • Reduce PDF size

    gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -sOutputFile=new_file.pdf original_file.pdf
    # Also try using different 'dPDFSETTINGS' settings explained in http://milan.kupcevic.net/ghostscript-ps-pdf/, see what works for you.
    
  • Unlock PDF file

    gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -SPDFPassword=THE_PASSWORD -sOutputFile=unencrypted.pdf -c .setpdfwrite -f encrypted.pdf
    
  • Extract pages from PDF file

     1: # Extract pages 1 to 3 from a file.
     2: gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=1 -dLastPage=3 -sOutputFile=out_file_page_1_to_3.pdf input_file.pdf
     3: 
     4: # Or extract pages from 3 to 7
     5: gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sPageList=3-7 -sOutputFile=out_file.pdf in_file.pdf
     6: 
     7: # Extract page 1,2,3,4, & 6
     8: gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sPageList=1-4,6 -sOutputFile=out_file.pdf in_file.pdf
     9: 
    10: # Extract page 1,4,5,6 & 9
    11: gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sPageList=1,4,5,6,9 -sOutputFile=out_file.pdf in_file.pdf
    
  • Merge PDF files

    gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=out_file.pdf in_file_1.pdf in_file_2.pdf
    

Media file tricks

  • Rotate video

    1: # Using mplayer(watch rotated video)
    2: mplayer -vf-add rotate=1 sample.mp4
    3: 
    4: # Using ffmpeg
    5: ffmpeg -i infile.mp4 -strict -2 -vf "transpose=1" outfile.mp4
    
  • Convert 3GP to MP4

    ffmpeg -i VID_0050.3gp -strict -2 -q:a 0 -ab 64k -ar 44100 VID_0050.mp4
    
  • FLV to MP4

    ffmpeg -i input.flv -qscale 1 -ar 22050 output.mp4
    
  • MKV to MP4

    1: ffmpeg -i input.mkv -c:v mpeg4 -c:a libmp3lame -b:a 128K output.mp4
    2: # or you can keep the video stream
    3: ffmpeg -i input.mkv -c:v copy -c:a aac -b:a 128K output.mp4
    4: 
    5: # Convert initial 10 seconds of the video for testing
    6: ffmpeg -t 10 -i input.mkv -c:v copy -c:a aac -b:a 128K output.mp4
    
  • PS4 compatible format 2

    1: # Ex1
    2: ffmpeg -i input.mkv -c:v libx264 -c:a libmp3lame -preset slow -profile:v high -level 4.2 -b:a 320K output.mkv
    3: # Ex2
    4: ffmpeg -i input.mkv -c:v libx264 -vf format=yuv420p -c:a libmp3lame -preset slow -profile:v high -level 4.2 -b:a 320K output.mkv
    5: # Ex3
    6: ffmpeg -i input.mkv -c:v libx264 -vf format=yuv420p -c:a libmp3lame -preset slow -profile:v high -level 4.2 -crf 21 -b:a 320K output.mkv
    7: # Ex4
    8: ffmpeg -i input.mkv -c:v libx264 -vf format=yuv420p -c:a aac -preset slow -profile:v high -level 4.2 -crf 21 -b:a 128K output.mkv
    

    Refer this post to compile ffmpeg with lame & libx264 support.

  • Extract audio(MP3) from MP4 video

    1: ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3
    2: 
    3: # or specify codec to use `libmp3lame` in this case.
    4: ffmpeg -i video.mp4 -f mp3 -codec:a libmp3lame -ab 320000 -vn music.mp3
    

    where:

    -i: input file

    -f mp3: file format should be MP3

    -ab 192000: audio should be encoded at 192Kbps. 320000 for 320Kbps

    -vn: don’t want video

  • Merge Audio:

    1: # Assuming that the video does not contain any audio stream.
    2: ffmpeg -i in_video.mp4 -i in_audio.mp3 -c:v copy -c:a mp3 output.mp4
    

    where:

    -c:v copy: Copy the audio stream. Similar to -vcodec copy

    -c:a: Set MP3 code

  • Merge/concat video files

    1: $ cat videos.txt
    2: file 'video_1.mp4'
    3: file 'video_2.mp4'
    4: file 'video_3.mp4'
    5: 
    6: ffmpeg f concat -i videos.txt -c copy output.mp4
    

    where:

    -c copy: Copy both video & audio codecs.

  • Cut video starting from 19 min 49 seconds up to 04 mins 18 seconds.

    ffmpeg -y -i Video.mp4 -qscale 1 -ss 00:19:49.0 -t 00:04:18.0 -acodec copy -vcodec copy output.mp4
    

    where:

    -ss: Start time.

    -t: Duration. You can also specify the end time using -to.

    Note: -sameq was removed in the recent versions of ffmpeg

  • Use mplayer to extract audio(MP3)

    mplayer -dumpaudio movie.flv -dumpfile movie_audio_track.mp3
    
  • Reduce resolution

    ffmpeg -i Birdman.mp4 -strict -2  -s 720x480 birdman.mp4
    
  • Record a screen cast using recordmydesktop

    recordmydesktop -x 100 -y 100 --width 1280 --height 720 --freq 48000 --fps 30 -o output.ogv
    
  • Record a screen cast using ffmpeg (Please disable Wayland):

    ffmpeg -video_size 1280x720 -show_region 1 -framerate 30 -f x11grab -i :1+30,30 output.mp4
    

    where:

    -video_size 1280x720 Width and height of 1280x720

    -show_region 1 Highlight a region to be recorded

    -framerate 30 30 FPS

    -i :1+30,30 On DISPLAY :1 starting with the upper-left corner at x=30, y=30

  • Include subtitles(Use subtitleeditor to create subtitle)

    ffmpeg -i video.avi -vf subtitles=subtitle.srt out.avi
    
  • Adjust aspect ratio

    ffmpeg -i input.mp4 -aspect 1280:720 -c copy output.mp4
    ffmpeg -i input.mp4 -vf scale=1280:720 -c:v h264 output.mp4 # Using h264 video codec
    
  • Text overlay (Use --enable-libfreetype while compiling Mplayer)

    ffmpeg -i input.mp4 \
           -vf "[in]drawtext=fontfile=/usr/share/fonts/gnu-free/FreeSans.ttf:text='This text will appear from 1 to 4 seconds':fontcolor=white:fontsize=50:box=1:boxcolor=black@0.5:boxborderw=40:x=(w-text_w)/2:y=(h-text_h)/2:enable='between(t,1,4)'" \
           -codec:a copy output.mp4
    

Image manipulation

  • Capture screenshot:

    import -window root screenshot.png
    
  • Re-size image 50% of its original size:

    convert dragon.gif -resize 50% half_dragon.gif
    
  • Limit pixel count to 30000:

    convert input.png -resize 3000@ output.png
    
  • Limit maximum image size of 265x64(hxw):

    convert input.png -resize '265x64' output.png
    
  • Limit image size of 265x64(hxw):

    convert input.png -resize '265x64!' output.png
    
  • Control compression level of an image:

    convert input.png -quality 75 output.jpg
    
  • Convert JPG(or any image) to PDF:

    convert input.jpg -page a4 output.pdf
    
  • Combine images

    convert image_1.jpg image_2.jpg -append out.jpg
    # Note: '-' will combine images vertically whereas '+' will combine images horizontally
    
  • Crop image

    crop.svg

    Figure 1: Crop

    convert -crop 2560x1440+10+10 in.png out.png
    

Git3

  • Find commits by author between the time-line

    1: # Syntax
    2: git log --since "START" --until "END" --author=AUTHOR --pretty=oneline --diff-filter=A
    3: 
    4: # Example
    5: git log --since "Oct 1 2018" --until "DEC 28 2018" --author=psachin@redhat.com --pretty=oneline --diff-filter=A
    6: 
    7: # If you also want to see the file names
    8: git log --since "Oct 1 2018" --until "DEC 28 2018" --author=psachin@redhat.com --name-status --pretty=oneline --diff-filter=A
    
  • Clone specific branch from remote

    1: git clone -b <REMOTE_BRANCH> <REMOTE_REPO_URL>
    2: # Example
    3: git clone -b ics https://github.com/androportal/abt.git
    
  • Checkout specific branch

    1: ## New
    2: git fetch <REMOTE>
    3: get branch --all  # show all the branches
    4: git checkout <BRANCH>
    5: 
    6: ## Old
    7: git checkout -b <REMOTE_BRANCH> remotes/<REMOTE>/<BRANCH>
    8: # Example
    9: git checkout -b holo_theme remotes/origin/holo_theme
    
  • Squash

    1: # Squash last five commits interactively
    2: git rebase -i HEAD~5  # and follow the instructions
    

Misc

  • Clean up ~/.ccache/ directory

    1: # View statistics using
    2: ccache -s
    3: 
    4: # Clear cache using
    5: ccache -C
    
  • wget a website

    wget -rkp -l5 -np -nH --cut-dirs=1 https://example.com
    

    where:

    -rkp: recursive, make link suitable for local viewing, download all files needed to properly view the page.

    -l5: recursively download 5 links away form the original page.

    -n: retrieve files below parent directory.

    -H: Span across hosts when doing recursive retrieving.

  • Find the File system/partition of current directory

    df -h .   # Notice the DOT
    
  • List commands/applications using IPv4/IPv6

     1: lsof -i 4
     2: # and
     3: lsof -i 6
     4: 
     5: # Sample output of `lsof -i 6`
     6: COMMAND     PID    USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
     7: emacs      3131 psachin   21u  IPv6 4519926      0t0  TCP nubia:55524->rajaniemi.freenode.net:ircu-3 (ESTABLISHED)
     8: emacs      3131 psachin   22u  IPv6 4524689      0t0  TCP nubia:41416->irc0.acc.umu.se:ircu-3 (ESTABLISHED)
     9: emacs      3131 psachin   23u  IPv6 4548798      0t0  TCP nubia:33452->strange.oftc.net:ircu-3 (ESTABLISHED)
    10: firefox    3656 psachin   51u  IPv6 5606852      0t0  TCP nubia:54302->sc-in-xbd.1e100.net:https (ESTABLISHED)
    11: chrome     3878 psachin  128u  IPv6 4518904      0t0  UDP *:mdns
    12: chrome     3878 psachin  148u  IPv6 4518906      0t0  UDP *:mdns
    13: thunderbi 13475 psachin   40u  IPv6 5552262      0t0  TCP nubia:35084->sc-in-x6d.1e100.net:imaps (ESTABLISHED)
    14: thunderbi 13475 psachin   61u  IPv6 4526804      0t0  TCP nubia:59268->sb-in-x6c.1e100.net:imaps (ESTABLISHED)
    15: thunderbi 13475 psachin   62u  IPv6 5592285      0t0  TCP nubia:60922->sa-in-x6d.1e100.net:imaps (ESTABLISHED)
    16: thunderbi 13475 psachin   85u  IPv6 5408348      0t0  TCP nubia:48110->sb-in-x6d.1e100.net:imaps (ESTABLISHED)
    17: thunderbi 13475 psachin  134u  IPv6 5561644      0t0  TCP nubia:44914->sb-in-x6c.1e100.net:imaps (ESTABLISHED)
    

Footnotes:

1

ghostscript has a very good usage documentation at https://www.ghostscript.com/doc/current/Use.htm

2

Special thanks to the guys at #ffmpeg@freenode for helping me converting to the PS4 compatible format.

3

https://lwn.net/Articles/811068/ (not related to the notes mentioned in this post)