Image and Video Cheat Sheet

Mar 12, 2024 · 4 min read
Photo by Alp Ancel on Unsplash
Table of Contents

The command line might seem intimidating, but it offers hidden gems for everyday tasks. Today, we’ll explore its power for image and video conversions, giving you more control over your media files for future reference or projects.

Image Manipulation

Here are some examples of how to manipulate images with ImageMagick’s tools, as always there are tons of options available, so make sure to check the official documentation for more details.

TIP: parameter order matters a lot in ImageMagick

Resize Images

magick input.png -resize 100x200 output.jpg
# or
mogrify -resize 50% *.png

Remove background

ImageMagick Examples
To remove the black background from an image, use the following command:

 magick input.png -fuzz 5% -transparent black out.png

Extent canvas size with transparency

convert input.png -background none -gravity center -extent 512x512 output.png
# or
mogrify -background none -gravity center -extent 512x512 *.png

Looping GIFs: Ever wanted a GIF to play endlessly?

convert -delay 20 -loop 0 nonloopingImage.gif loopingImage.gif

This creates a looping version of nonloopingImage.gif with a delay of 20 milliseconds between frames. Adjust the delay for faster or slower playback. The -loop 0 sets unlimited loops.

Creating GIFs with Transparency: Want a GIF with clear backgrounds?

magick -delay 100 -loop 0 -dispose previous *.png hands.gif

This command combines all PNG files in the current directory (replace *.png with specific filenames if needed) into a GIF named hands.gif. The -delay 100 sets a delay of 100 milliseconds, -loop 0 enables infinite looping, and -dispose previous ensures transparency in the final GIF.

PNG optimization

optipng is a command-line tool that optimizes PNG images to reduce file size without losing quality. Here’s how to use it:

optipng -strip all image.png

SVG to PNG Conversion

Scalable Vector Graphics (SVG) are perfect for logos and icons, but sometimes you need a PNG image. Here’s how to convert SVG to PNG using rsvg-convert

rsvg-convert -h 32 icon.svg > icon-32.png

This command converts icon.svg to a PNG named icon-32.png with a height of 32 pixels. Replace 32 with your desired pixel height or width for a specific aspect ratio.

Audio Manipulation

Generate audio from console

Play a sine wave at 234Hz for 5 seconds using the play command:

play -n synth 5 sin 234 hz

Here’s a breakdown of the options used:

  • -n This option tells play to play the sound only once (for 5 seconds in this case).
  • synth This specifies that we want to generate a synthesized sound.
  • 5 This is the duration in seconds for which the sound will be played.
  • sin This keyword tells play to generate a sine wave.
  • 234 hz This defines the frequency of the sine wave as 234 Hertz.

Make sure you have the play command from SoX installed on your system for this to work.

Video Manipulation

Extract a video segment from a larger video file using ffmpeg

fmpeg -i Untitled.mp3 -t 00:00:09 -acodec copy output.mp3
  • -t 00:00:09: set output duration to 9 seconds. Here, 00 represents hours and minutes.
  • -acodec copy: copy the audio stream from the input file without re-encoding it. This is efficient if you don’t need to modify the audio quality.

Overall, this command extracts a 9-second segment from your Untitled.mp3 file and saves it as output.mp3 while preserving the original audio quality.

Downloading Videos

youtube-dl works on OS X, Linux and Windows, and can download videos in any format YouTube provides.

Video DownloadHelper is a Firefox/Chrome extension for downloading videos from many sites with minimum effort.

Playing a video stream from the command line with mpv

mpv --cache-on-disk stream_93b9d36741fe2f46.m3u

Check the official page for more info https://mpv.io/