How To Extract Images from a Video with avconv on Linux
Suppose you’ve got an awesome video that nobody is watching and you want to create animated GIFs from excerpts of it so that people are forced to see your genius in action. But how do you extract images from a video? You’re in luck! Using the super-powerful avconv on Linux, you can quickly and easily break a video down frame by frame. If you follow our tutorial How To Create Animated GIFs from a PNG Sequence with ImageMagick and the Command Line, you can quickly and easily convert your PNG image sequence to an animated GIF.
I’m going to use this video as our example. It’s episode 4, “ls or List Directory Contents”, of our series, Superusers: The Legendary GNU/Linux Show. At 2:03 there’s a close-up of a jittery gremlin and some acorns that I think will make a great GIF.
If you don’t already have it, install avconv:
sudo apt-getinstall libav-tools
From the command line, navigate to the directory containing your video. The run the following command:
Let’s break this down, no pun intended.
First, we pass avconv our input file, ‘my_awesome_video.mp4’. Then, using the -r, or rate, switch, we specify that we want to extract 30 frames per second. Why? Because the standard frame-rate for video is 29.97 frames per second. But 30 is close enough. The -f option specifies that we want our output to be an image. And lastly: %04d.png applies a numerical sequence to the frames using four digits starting with 0001. Thus, our output will be a sequence of images titled ‘0002.png’, ‘0003.png’, ‘0004.png’, and so on.
If you’re working with a very long video and you only want to extract a short clip, use the seek option, -ss, and the duration option, -t, to specify the start point and the length of the clip, using this format: hh:mm:ss, where hh is hours, mm is minutes, and ss is seconds.
For example, to extract the 4 seconds I want to convert to a GIF I run the following command:
This returns a much more manageable image sequence and takes much less time to complete. Lastly, if you want to scale your image sequence up or down, you can use the -s option and pass it the WxH parameters. I’ll let you figure that one out. If you need help, Read The Fantastic Manual!
We want to take you on a journey through the other side of the creative process: making a living. Explore the necessary evils of business as we develop a creative media studio using free and open source software. Sign up for our New Moon newsletter delivered on, you guessed it, every New Moon.
Master the commands and structure of the fastest-growing computer operating system, Linux. Join super friends Adelie the penguin and Aramis the gnu on surreal adventures in the file tree. Learn the essential commands you need to take greater control of your computer in Superusers: The Legendary GNU/Linux Show.
How To Extract Images from a Video with avconv on Linux was posted by Jared on . Jared is one half of the creative force behind Dototot. In addition to writing scripts and tutorials, he draws and animates both the digital and the analog.