Image and Video Cheat Sheet

Mastering Image and Video Conversions with the Command Line

Photo by Alp Ancel on Unsplash

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

Animated GIFs are a fun way to add life to your content. Here’s how to manipulate them with ImageMagick’s convert command:

Looping GIFs: Ever wanted a GIF to play endlessly?

Use this command

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?

Here’s the command

convert -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.

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

Let’s break down what the command above does:

  • -i: This option specifies the input file. In your case, it’s Untitled.mp3.
  • -t 00:00:09: This option sets the output duration to 9 seconds. Here, 00 represents hours and minutes (which are zero in this case).
  • -acodec copy: This option tells ffmpeg to 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.
  • output.mp3: This specifies the name of the output file, which will be a 9-second clip of your original MP3.

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/

George Litos
George Litos
Senior Software Engineer