Terminal Media tools........................
Nick Peers discovers how the Terminal can do more than simply let you play back your collection of video, music and image files…
Nick Peers explains how you can manhandle all your media from the endless, soul-consuming blackness of the terminal.
Multimedia plays a key role in computing, and you don’t need to be logged into the desktop to be able to enjoy your collection of music and video files, and photogrphs. The command line also provides access to a host of tools that enable you to rip, encode, convert and tweak your media files.
Let’s start by making sure Ubuntu has full multimedia support. Even if you ticked the box to install third-party software during the install process you’re still missing several key elements. First, let’s install all of those “restricted” extras, not just the few added at the install stage. Open the Terminal and type the following: $ sudo apt-get install ubuntu-restricted-extras
This will install several additional packages, while the libavcodec-ffmpeg56 package will be replaced by libavcodec-ffmpeg-extra56, which comprises a wider selection of GStreamer plugins (see https://gstreamer. freedesktop.org) for media streaming, playback, editing and processing. You’ll be prompted to install Microsoft TrueType fonts – press Tab followed by Enter, then select Yes with the cursor keys before hitting Enter again.
This command adds support for playing encrypted DVDs: $ sudo apt-get install libdvd-pkg
When installed, type: $ sudo dpkg-reconfigure libdvd-pkg
This completes the process. If you’re looking to play Blu-rays on your PC, check out the box ( seetop-right).
There are plenty of choices when it comes to playing media on your PC, and VLC is the obvious choice for, particularly if you want to play commercial discs. Install it with sudo apt-get install vlc , then you have a choice of running it from the desktop or directly from the command line, which will open a window on the desktop with your media playing. You can control VLC completely from the command line – type vlc --help for a list of commands, but for basic playback purposes it distils down to typing vlc followed by the source, which can be to a media file ( file:///path/file ), DVD ( dvd:// [device] ) or Blu-ray ( bluray://[device] ) – for example: $ vlc dvd:///dev/sr0
You can also stream media over the internet ( http:// host[:port]/file and substitute http with ftp or MMS if necessary) and via a UDP streaming server: $ vlc udp://[[<source address>]@[<bind address>][:<bind port>]]
Play from the command line
If you want a music player that comes with a command-line interface (perfect for remote access or using outside of the desktop), then there are several choices. Let’s start with a tool
for playing a wide range of music formats (FLAC, OGG, MP3 and so on) direct from the command line called SoX: $ sudo apt install sox libsox-fmt-all The libsox-fmt-all library includes support for proprietary formats such as MP3. Once installed, browse to the folder containing your music and invoke it using the play command: $ play *.mp3
Use Ctrl+C to jump to the next track, or press it twice to stop playback. Type play --help to see a full list of available options, including a wide range of effects and filters, such as: $ play *.flac bass +2 gain +1 reverb If you’d like a more interactive player, then try cmus ( sudo apt install cmus ). Once launched, after a short pause you’ll be shown a two-pane window. The app is controlled in a similar way to the Vi text editor – for example, to load your Music folder into the program, type: :add ~/Music You can then browse for tracks and playlists - type man cmus for a detailed user guide. You can also plug in extra features via extension scripts – see https://github.com/ cmus/cmus/wiki for more details.
The power of FFmpeg
Things naturally get more complicated when you look to convert audio and video, whether that’s switching between formats (say .avi to .mp4), recording a live stream or something more complex, such as embedding subtitles into a file. One of the best-known tools for the job is FFmpeg ( sudo apt install ffmpeg ). It recognises a wide range of formats and comes with tools ( ffserver, ffplay and ffprobe) for transcoding, streaming and playing media, too.
We’re going to scratch its very large surface by looking at some basic real-world uses. First up – to convert a file from MKV to MP4 format: $ ffmpeg -i inputfile.mkv -vcodec copy -acodec copy outputfile.mp4
You can take this further and quickly convert (or remux as it’s known) a folder full of files: $ for i in *mkv; do ffmpeg -i “$i” -vcodec copy -acodec copy “$i.mp4”; done
This basic example changes the file type (which is also known as the container), but doesn’t change its codecs (the audio and video content). It’s almost instantaneous because there’s no decoding, converting and then re-encoding of the video or audio streams: they simply switch from one codec to another.
If you want to convert a video’s codec as well as its container (type ffmpeg -codecs for a full list of supported codecs), you’d assume the following syntax would work: $ ffmpeg -i inputfile.wmv -vcodec h264 -acodec aac outputfile.mkv
In fact, you’re likely to get a string of errors: “too many channels” indicates there are multiple video and/or audio streams, but FFmpeg will at least try to choose the correct ones (a process called stream mapping). However, the next error will be the killer – because you’ll need to specify certain parameters for the encoder you picked under -vcodec .
These parameters can be determined by typing the following command: $ ffmpeg -h encoder=h264
This produces a long, complex list – for a guide to encoding in common formats such as H.264, VP8/9 and AAC, read through the Encoding section at https://trac. ffmpeg.org/wiki.
Let’s finish by looking at some niche tools. First up is ffprobe, which is installed alongside FFmpeg. It’s a handy way to view the metadata attached to a specific media file: $ ffprobe file.mp3
Next, there’s normalize, a tool that basically ensures all your music tracks have a standard volume level, perfect for playlists where you don’t want to be constantly yanking the volume control up and down: $ sudo apt install normalize-audio
Now browse to a folder of files and type the following: $ normalize-audio -bv *.mp3
This scans all the files in the directory to compute a standard deviation between the tracks (extreme deviations are ignored), then it applies an adjustment to all the tracks – including those with extreme deviations – to bring them all into line with each other. If you don’t need the verbose output, use -b instead of -bv .
Finally, exiftool ( sudo apt install libimage-exiftool-perl ) displays metadata for a range of media formats, but it’s also capable of writing metadata to many popular image formats too, including JPG, TIF, GIF and PNG. Go to https://linux.die. net/man/1/exiftool for a detailed guide to using it.
You’ll have most success playing older Blu-ray movies through VLC – launch it from the command line.
The CMus music player works in a similar way to the Vi text editor. Remember to preface commands with the colon (:) symbol.