Building Frames
Go with lavfi
Generating a GIF in two steps – generating the palette and then applying it – is not elegant. Besides, it leaves behind a PNG file that is useless for anything else.
Thanks to the lavfi
virtual device, you can do everything in one go (Listing 2), which has the advantage of making you look like a veritable FFmpeg wizard, without generating any PNG cruft to boot.
Listing 2
One-Step GIF Generation
At this stage, it should be clear what is going on, but for the sake of clarity:
- The
clip_gray.mp4
file is the grayscale and scaled-down video you want to convert. - The
lavfi
filter options in quotation marks generate the palette image and pipe it through[out]
to the … - …
-filter_complex
chain of filters.
Apart from using the palette you created with the lavfi
virtual device, you reduce the frame rate to 10 frames per second and push the resulting frames out to clip_gray.gif
.
The resulting GIF image is smooth, acceptably lightweight, and created in seconds.
Conclusion
Not surprisingly, FFmpeg is the back end for so many video editing applications: The number of things you can do with it is astounding. Although it is true that some of the command lines you have to generate can be eye-wateringly complex, it is often worth working directly with FFmpeg, because it gives you that extra control on the final result. Apart from lavfi, you can learn more about FFmpeg devices by taking a look at the "More Devices" box.
More Devices
Apart from lavfi
, a few other FFmpeg devices are worth investigating, especially if you want a raw, but very reliable way of grabbing streams from your camera or desktop:
(1) video4linux2
, or v4l2
[4] for short (you can use either), captures streams from your webcam. To work out what devices you have on which to capture streams, run ls /dev/video*
. My laptop has, apart from its built-in webcam, an external webcam with its own microphone, so I see:
> ls /dev/video* /dev/video0 /dev/video1
If you run ffplay -f v4l2 -i /dev/video1
, a window pops up showing the feed from the external webcam.
(2) On a related note, alsa
[5] and pulse
[6] capture audio. To know which device to capture audio from, if you want to use alsa
, enter arecord -l
. Again, on my laptop, apart from the built-in microphone, I have the microphone attached to the external webcam and a microphone input on an external USB sound card, so I see:
> arecord -l **** List of CAPTURE Hardware Devices **** card 0: PCH [HDA Intel PCH], device 0: ALC3241 Analog [ALC3241 Analog] Subdevices: 1/1 Subdevice #0: subdevice #0 card 1: Device [USB Sound Device], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0 card 2: H2300 [HP Webcam HD 2300], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0
If I want to record from the microphone on the webcam, I can see it is card 2 (HP Webcam HD 2300) connected to device 0. This means ffmpeg -f alsa -i hw:2,0 voice.mp3
records to a file called voice.mp3
.
With pulse
, things are a bit different. First, you need to find out the name of your devices:
> pactl list sources Source #2 State: SUSPENDED Name: alsa_input.usb-Hewlett_Packard_HP_Webcam_HD_2300-02.analog-stereo Description: HP Webcam HD 2300 Analog Stereo Driver: module-alsa-card.c Sample Specification: s16le 2ch 44100Hz Channel Map: front-left,front-right Owner Module: 7 Mute: no Volume: front-left: 48497 / 74% / -7.85 dB, front-right: 48497 / 74% / -7.85 dB balance 0.00 Base Volume: 41350 / 63% / -12.00 dB Monitor of Sink: n/a Latency: 0 usec, configured 0 usec Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY[...]
Once you have located the device from which you want to record, you can then use its name in FFmpeg:
ffmpeg -f pulse -i alsa_input.usb-Hewlett_Packard_HP_Webcam_HD_2300-02.analog-stereo voice.mp3
In this case, it captures from an external webcam microphone and records to voice.mp3
.
(3) x11grab
[7] is interesting if you want to make screencasts. You can grab the whole screen, just a rectangle, or a rectangle centered around the cursor. For example, the line
ffmpeg -f x11grab -follow_mouse centered -framerate 25 -video_size 640x480 -i :0.0 screncast.mp4
captures a 640x480
region centered around the mouse and records it to screencast.mp4
.
You can mix and match all the above devices and apply all sorts of effects to the streams they produce. Altogether, they provide a very complete and complex screencasting solution.
Infos
- FFmpeg colors: https://ffmpeg.org/ffmpeg-all.html#Color
- "Tutorials – FFmpeg" by Paul Brown, Linux Magazine, issue 206, January 2018, pg. 92, http://www.linux-magazine.com/Issues/2018/206/Tutorials-FFmpeg
- Big Buck Bunny: https://peach.blender.org/
- video4linux2 device: https://www.ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2
- alsa device: https://www.ffmpeg.org/ffmpeg-devices.html#alsa
- pulse device: https://www.ffmpeg.org/ffmpeg-devices.html#pulse
- x11grab device: https://www.ffmpeg.org/ffmpeg-devices.html#x11grab
« Previous 1 2
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.