Skip to main content

Music and video at the Linux terminal

Your terminal doesn't have to just be for text. Enjoy video and audio at the command line as you work with these simple tools.
Image
Cassette tape

As a system administrator, you probably spend a lot of time at your terminal. We all have a tool that we begrudge having to leave the command line to use, whether it's for a web browser or a desktop GUI application.

If you poke around GitHub for long enough, you can find a command line utility to replace the graphical front end of just about every service out there, at least those with an accessible API. Some of these tools work better than others, of course, but a lot of them are worth poking around to see if they work for you.

Today we're going to look at three tools for enjoying sound and video at your Linux terminal: youtube-dl, mplayer, and cava. I originally profiled these tools as a part of my 24 Days of Linux Toys series on Opensource.com; a user there suggested that you might choose mpv as a suitable mplayer replacement, but I'll leave that up to the reader to explore and decide.

Listen to the radio

MPlayer is a versatile multimedia player that will support just about any media format you throw at it. If MPlayer is not already installed, you can probably find it packaged for your distribution. On Fedora, I found it in RPM Fusion (be aware that this is not an "official" repository for Fedora, so exercise caution as you would with any unfamiliar binary source):

$ sudo dnf install mplayer

MPlayer has a slew of command-line options to set depending on your situation. I wanted to listen to the local college radio station here in Raleigh (88.1 WKNC; they're pretty good!), and so after grabbing the streaming URL from their website, all that took to get my radio up and running, no GUI or web player needed, was:

$ mplayer -nocache -afm ffmpeg http://wknc.sma.ncsu.edu:8000/wknchd1.mp3

MPlayer is open source under the GPLv3, and you can find out more about the project and download source code from the project's website.

Visualize your music

If you want to do more than just listen, and get a visual representation of what you're listening to, try cava. Short for "console-based audio visualizer for ALSA" (although it supports more than just ALSA now), cava is a nice MIT-licensed terminal audio visualization tool that's fun to watch. You can install cava much in the same way, through:

$ sudo dnf install mplayer
Cava

Watch a video

Finally, let's double down on our first tool, MPlayer, and add in one more, youtube-dl.

As its name would imply, youtube-dl is a command-line utility for downloading YouTube videos, but it can capture video from a number of other sites as well, and it's a really quite full-featured application with thorough documentation to make video acquisition easy. A note: please don't use youtube-dl in any context that would violate the copyright laws in your jurisdiction.

youtube-dl is licensed under a public domain dedication known as the Unlicense that's similar to Creative Common's CC0. There are some interesting legal opinions out there about where public domain dedication fits into the open source landscape, but it's generally considered compatible with existing open source licenses even by organizations who don't recommend its use.

In its simplest form, we're going to use youtube-dl to grab a video for playback in our terminal. First, install it using a method appropriate for your distribution. For me, in Fedora, it was packaged in my repositories, so installation was as simple as:

$ sudo dnf install youtube-dl

Then, let's grab a video. YouTube allows you to search by license, so today, we're going to take a look at a fireplace video from Gemmy's Videos available under a Creative Commons attribution license. For YouTube videos, you can download with the file ID alone, like this, and we'll specify an output file name as well. I intentionally picked a short video, since long videos can get quite large!

$ youtube-dl pec8P5K4s8c -o fireplace.mp4

You may need to install libcaca for your system if it's not already installed. If you just use MPlayer to launch the video from the command line as-is (mplayer fireplace.mp4), it will play, but in a window all of its own; not exactly what we were going for.

First, I set my libcaca settings to force it to use ncurses as the display driver, keeping the output in my terminal, with:

$ export CACA_DRIVER=ncurses

Then, I zoomed way out in my terminal (the more "pixels", the better), and played the file with the following (forcing the use of libcaca and silencing text output from MPlayer):

$ mplayer -really-quiet -vo caca fireplace.mp4

And, there you go!

Fireplace

 

Author’s photo

Jason Baker

Red Hat Certified Engineer. Linux desktop enthusiast. Map/geospatial nerd. Raspberry Pi tinkerer. Data analysis and visualization geek. Occasional Pythonista. More about me

Free Event: Red Hat Summit 2021 Virtual Experience

Join Red Hat Summit Virtual Experience for live demos, keynotes, and technical
sessions from experts around the globe—happening April 27–28 and June 15–16.

OUR BEST CONTENT, DELIVERED TO YOUR INBOX