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.
1. Why Learn Multimedia in the Command Line?
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.
2. Playing Audio and Video
Using mpv
mpv is a versatile media player that supports a wide range of audio and video formats.
- Install
mpv:
$ sudo apt install mpv # Debian/Ubuntu
$ sudo dnf install mpv # Red Hat/Fedora - Play a Media File:
$ mpv file.mp4 - Play in the Background:
$ mpv --no-video file.mp3
Using mplayer
mplayer is another popular command-line media player.
- Install
mplayer:
$ sudo apt install mplayer # Debian/Ubuntu
$ sudo dnf install mplayer # Red Hat/Fedora - Play a Media File:
$ mplayer file.mp4
3. Editing and Converting Multimedia
Using ffmpeg
ffmpeg is a powerful tool for editing, converting, and processing multimedia files.
- Install
ffmpeg:
$ sudo apt install ffmpeg # Debian/Ubuntu
$ sudo dnf install ffmpeg # Red Hat/Fedora - Convert a Video File:
$ ffmpeg -i input.mp4 output.avi - Extract Audio from a Video:
$ ffmpeg -i input.mp4 -q:a 0 -map a output.mp3 - Resize a Video:
$ ffmpeg -i input.mp4 -vf scale=640:360 output.mp4
Using imagemagick
imagemagick is a suite of tools for editing and converting images.
- Install
imagemagick:
$ sudo apt install imagemagick # Debian/Ubuntu
$ sudo dnf install imagemagick # Red Hat/Fedora - Resize an Image:
$ convert input.jpg -resize 50% output.jpg - Convert Image Formats:
$ convert input.png output.jpg
4. Working with Images
Viewing Images
Use feh to view images from the command line.
- Install
feh:
$ sudo apt install feh # Debian/Ubuntu
$ sudo dnf install feh # Red Hat/Fedora - View an Image:
$ feh image.jpg
Creating 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.
5. Practical Examples
Batch Convert Images
Convert all .png files in a directory to .jpg:
$ for file in *.png; do convert "$file" "${file%.png}.jpg"; done
Extract 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"; done
Create 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.mp4
6. Practice Time!
Let’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.