In this chapter, we’ll explore how to handle multimedia files (audio, video, and images) directly from the command line. You’ll learn how to play, edit, and convert multimedia files using powerful tools like ffmpeg, mpv, and imagemagick. By the end of this chapter, you’ll be able to manage multimedia files efficiently without relying on graphical applications.
Command-line multimedia tools are:
- Lightweight: They consume fewer resources compared to graphical applications.
- Powerful: They offer advanced features for editing, converting, and processing multimedia files.
- Automation-Friendly: They can be scripted for batch processing and automation.
- Remote Work: They are ideal for managing multimedia files on remote servers or headless systems.
Using mpv
mpv is a versatile media player that supports a wide range of audio and video formats.
mpv: $ sudo apt install mpv # Debian/Ubuntu $ sudo dnf install mpv # Red Hat/Fedora$ mpv file.mp4$ mpv --no-video file.mp3Using mplayer
mplayer is another popular command-line media player.
mplayer: $ sudo apt install mplayer # Debian/Ubuntu $ sudo dnf install mplayer # Red Hat/Fedora$ mplayer file.mp4Using ffmpeg
ffmpeg is a powerful tool for editing, converting, and processing multimedia files.
ffmpeg: $ sudo apt install ffmpeg # Debian/Ubuntu $ sudo dnf install ffmpeg # Red Hat/Fedora$ ffmpeg -i input.mp4 output.avi$ ffmpeg -i input.mp4 -q:a 0 -map a output.mp3$ ffmpeg -i input.mp4 -vf scale=640:360 output.mp4Using imagemagick
imagemagick is a suite of tools for editing and converting images.
imagemagick: $ sudo apt install imagemagick # Debian/Ubuntu $ sudo dnf install imagemagick # Red Hat/Fedora$ convert input.jpg -resize 50% output.jpg$ convert input.png output.jpgViewing Images
Use feh to view images from the command line.
feh: $ sudo apt install feh # Debian/Ubuntu $ sudo dnf install feh # Red Hat/Fedora$ feh image.jpgCreating Image Slideshows
Use feh to create a slideshow of images:
$ feh -D 5 -F -Z /path/to/images/-D 5: Delay of 5 seconds between images.-F: Fullscreen mode.-Z: Automatically zoom images to fit the screen.Batch Convert Images
Convert all .png files in a directory to .jpg:
$ for file in *.png; do convert "$file" "${file%.png}.jpg"; doneExtract Audio from Multiple Videos
Extract audio from all .mp4 files in a directory:
$ for file in *.mp4; do ffmpeg -i "$file" -q:a 0 -map a "${file%.mp4}.mp3"; doneCreate a Video Slideshow
Combine images into a video slideshow using ffmpeg:
$ ffmpeg -framerate 1 -i image%d.jpg -c:v libx264 -r 30 -pix_fmt yuv420p slideshow.mp4Let’s put your new skills to the test:
1. Use mpv to play a video file and adjust playback speed.
2. Convert a .mp4 video to .avi using ffmpeg.
3. Resize an image to 50% of its original size using imagemagick.
4. Create a slideshow of images using feh.
Command-line multimedia tools like ffmpeg, mpv, and imagemagick are powerful and versatile, allowing you to handle audio, video, and images efficiently. By mastering these tools, you can streamline your multimedia workflows, automate repetitive tasks, and manage files effectively without relying on graphical applications.