Skip to content

CheatSheet for Linux

Images

Convert a set of image with ImageMagick

for i in *.png ; do convert $i ${i%.*}.jpg ; done
for i in *.png ; do convert -resize 1080 $i ${i%.*}.jpg ; done

Resize an image for the web

convert input.jpg -quality 75 output.jpg

Creating a gif out of a set of pictures

convert [-resize 120] -delay 20 -loop 0 [images].jpg [movie].gif

Check the disk usage in an interactive way

ncdu

Screenshot

scrot –delay 2 docs/images/week04/assignment_USB_OK.png
scrot -s docs/images/week04/assignment_USB_OK.png

Slow down a gif

identify -verbose your.gif | grep Delay
Give : Delay: 5x100 –> change the number before the “x”
convert -delay 10x100 your.gif your_slow.gif

Video

Compressing video with good quality

ffmpeg -i input.mp4 -c:v libx264 -crf 24 -preset slow output.mp4

Cropping a video (from mobile phone for example)

ffmpeg -i input.mp4 -filter:v “crop=w:h:x:y” output.mp4

Rotating a video

ffmpeg -i input.mp4 -vf “transpose=1” output.mp4 // 90º Clockwise ffmpeg -i input.mp4 -vf “transpose=2” output.mp4 // 180º Clockwise ffmpeg -i input.mp4 -vf “transpose=3” output.mp4 // etc…

Adding caption to a video

ffmpeg -y -i PA_Pouring.mp4 -filter_complex \ “drawtext=fontsize=120:fontcolor=black:x=100:y=200:text=Removing the last bubbles:enable=’between(t,9,13)’, \ drawtext=fontsize=120:fontcolor=black:x=100:y=200:text=Blowing the mold:enable=’between(t,24,28)’, \ drawtext=fontsize=120:fontcolor=black:x=100:y=200:text=Pouring … slowly:enable=’between(t,30,52)’” \ PA_Pouring_comments.mp4

Adding caption and image to a video (and encode it)

ffmpeg -y -i VID-20210414-WA0004.mp4 -i redarrow.png -i greenarrow.png -i bluearrow.png \ -filter_complex “drawtext=fontsize=35:fontcolor=black:x=300:y=430:text=Sending the signal:enable=’between(t,0,3)’,\ drawtext=fontsize=35:fontcolor=black:x=300:y=430:text=Led confirms reception:enable=’between(t,4,8)’,\ drawtext=fontsize=35:fontcolor=black:x=280:y=430:text=Signal seen on Oscilloscope:enable=’between(t,9,11)’,\ overlay=W-w-120:H-h-160:enable=’between(t,0,3)’,\ overlay=W-w-150:H-h-150:enable=’between(t,4,8)’,\ overlay=W-w-480:H-h-40:enable=’between(t,9,11)’” -c:v libx264 -crf 32 PA_Sensing_IR.mp4

Cut/Trim video with encoding :

-ss 00:03 -to 00:08 -> from 3 to 8 s

-ss 00:03 -t 5 -> 5s starting from the 3rd second

Fast way

ffmpeg -ss 00:00:03 -i inputVideo.mp4 -to 00:00:08 -c:v copy -c:a copy trim_ipseek_copy.mp4

Clean way

ffmpeg -i inputVideo.mp4 -ss 00:03 -to 00:08 -c:v libx264 -crf 30 trim_opseek_encode.mp4

Remove part of a video + re-encoding :

ffmpeg -y -i VID_20210419_112844.mp4 -filter_complex \ “[0:v] trim=start=00:end=35,setpts=N/FRAME_RATE/TB [v0]; \ [0:a] atrim=start=00:end=35,asetpts=N/SR/TB [a0]; \ [0:v] trim=start=56:end=101,setpts=N/FRAME_RATE/TB [v1]; \ [0:a] atrim=start=56:end=101,asetpts=N/SR/TB [a1]; \ [v0][a0][v1][a1] concat=n=2:v=1:a=1 [v] [a]” \ -map “[v]” -map “[a]” -c:v libx264 -crf 36 PA_Casting_pouring_TRIM.mp4

Miscellaneous

Find usb devices

ls /dev/* | grep usb lsusb

Looking for a string in a lot of code file

grep -rnw ‘my path’ -e ‘mystring’

To get the pixel position in a video using pyton and vlc : This script

The python code

Adapt your path on line 11 of the code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    # importing vlc module
    import vlc

    # importing time module
    import time

    # creating vlc media player object
    media_player = vlc.MediaPlayer()

    # media resource locator
    mrl = "/home/pi/Downloads/Week10/PA_Sensing_IR.mp4"

    # setting mrl to the media player
    media_player.set_mrl(mrl)


    # start playing video
    media_player.play()

    # wait so the video can be played for 5 seconds
    # irrespective for length of video
    time.sleep(5)

    print("Cursor Co-ordinates : ")

    while(1):
        # getting cursor co-ordinates
        value = media_player.video_get_cursor()
        # printing cursor co-ordinates
        print(value)
        time.sleep(0.2)

Last update: June 29, 2021