1. Overview

Most users prefer web browsers or dedicated applications to stream videos. However, command-line tools are also available for Linux users who prefer the terminal environment.

In this tutorial, we’ll explore some command-line tools that allow us to stream and watch remote videos directly in Linux. We’ll discuss the most popular media players that support streaming remote videos and youtube-dl to extract direct URLs for streaming.

2. Streaming Remote Videos Using the CLI

Streaming remote videos via the command line is possible with various media players on Linux. In this section, we’ll look at different media players that have this ability.

2.1. mpv

mpv is a powerful media player that supports a plethora of video and audio formats. Essentially, mpv is a fork of the MPlayer project. It aims to be highly customizable so that it can be easily integrated with various software applications.

By default, it might not be installed on most Linux distributions. It is, however, available on most official repositories. We can install it using a package manager like apt using its canonical name mpv:

$ sudo apt install mpv -y

Once installed, let’s verify it:

$ mpv --version
mpv 0.35.1 Copyright © 2000-2023 mpv/MPlayer/mplayer2 projects

Here’s the general syntax for streaming a video using mpv:

$ mpv [OPTIONS] <URL>

There are different options available for video, audio, controls, network, and so on. For instance, we can use –cache=2048 to set a cache size of 2 GB for smoother playback and –fs to enable fullscreen mode:

$ mpv --cache=2048 --fs https://www.youtube.com/watch?v=5ocq6_3-nEw

Once we run the command, mpv presents a simple window with basic controls for watching the video. In contrast, if we stream an audio source, it would play right in the terminal without a graphical window.

Moreover, we can customize the interface to the core by using the mpv documentation as a reference.

 2.2. VLC

VLC is a popular cross-platform GUI media player that offers advanced playback features and the ability to stream media over networks.

Some distributions come with VLC pre-installed. However, we can install it from the official package repositories using its canonical name vlc:

$ sudo apt install vlc -y

Once installed, let’s verify it:

$ vlc --version
VLC media player 3.0.18 Vetinari

The syntax for playing a stream through vlc is similar to mpv:

$ cvlc [OPTIONS] <URL>

In the command, we used cvlc, which is an alias for vlc-ldummy, which represents a minimal graphical interface suitable for command-line usage.

Unlike mpv, VLC cannot directly play videos by providing a URL. The URL needs to be a direct link to a video stream in order to play it. We’ll discuss extracting direct streaming URLs from pages in the upcoming sections.

Moreover, we can provide different options to the command to render the video.

2.3. ffplay

ffplay is a simple and versatile media player based on the FFmpeg multimedia framework. It’s designed to play various audio and video formats and offers a minimalistic user interface.

It comes with the ffmpeg package, which we can install using a package manager:

$ sudo apt install ffmpeg -y

Once installed, we can verify it:

$ ffplay --version
ffplay version 5.1.2-3ubuntu1 Copyright (c) 2003-2022 the FFmpeg developers

ffplay has a straightforward syntax to play a video file. We simply type in ffplay followed by the URL:

$ ffplay <URL>

We can do a lot of magic with ffplay because it uses FFmpeg as its underlying technology. For instance, we select a specific video codec and mono channel for audio:

$ ffplay -ac 1 -vcodec vp9 <URL>

ffplay downloads the stream, applies the media settings, and plays it in real-time. Although it can transcode the video in real-time, we should note that it’s a resource-intensive task.

2.4. mplayer

mplayer is a video player that can play videos from various sources, including local files, DVDs, and streaming protocols. It’s been around for a lot of time, but recently, its development has slowed down.

We can install mplayer from our official distribution package repository:

$ sudo apt install mplayer -y

Once installed, verify it:

$ mplayer --version
MPlayer UNKNOWN-12 (C) 2000-2023 MPlayer Team

Similar to the other media players, it follows the same syntax:

$ mplayer <URL>

Like VLC and ffplay, the URL needs to be a link to a direct video stream.

3. youtube-dl and yt-dlp

youtube-dl is a powerful command-line tool that allows users to download videos and audio. Despite “youtube” in its name, it also supports hundreds of other video and audio platforms.

youtube-dl offers extensive customization options, such as selecting the desired video quality, extracting audio, downloading subtitles, and more. It’s widely used and appreciated by users who want to save online videos for offline viewing or archiving purposes.

On the other hand, yt-dlp is a fork of youtube-dl, which aims to provide additional features, bug fixes, and ongoing development beyond what’s available in youtube-dl. Therefore, we’ll use yt-dlp for our purpose in this section.

3.1. Installation

youtube-dl and yt-dlp have active communities that constantly contribute to the projects. Due to their reputation, both tools have paved their way to most distros’ official repositories. So, we can install it straight from there:

$ sudo apt install yt-dlp -y

Alternatively, we can also use pip:

$ pip install yt-dlp

Once installed, we can verify it:

$ yt-dlp --version
2023.05.04

3.2. Extracting Direct Stream URLs

Behind the scenes, mpv uses youtube-dl or yt-dlp to extract the direct stream URL from a webpage. For instance, let’s stream a video from YouTube:

$ mpv https://www.youtube.com/watch?v=5ocq6_3-nEw

It works because mpv first fetches the webpage and extracts the required URLs through youtube-dl. Additionally, we can also manually fetch the direct stream URLs by using the -g option to yt-dlp to fetch the default URLs:

$ yt-dlp -g -f 18 "https://www.youtube.com/watch?v=5ocq6_3-nEw"
https://rr1---sn-2uja-h4bl.googlevideo.com/videoplayback?expire=1686362400&ei=wISDZPn8CZmU1gaVrobQDQ&ip=39.33.65.33&id=o-AMkGIYZkuBXjBr-pu_LeVx4Qe-NBwSjv7FMxb11UOxn1&itag=18&source=youtube&requiressl=yes&mh=1l&mm=31%2C29&mn=sn-2uja-h4bl%2Csn-4wg7zne7&ms=au%2Crdu&mv=m&mvi=1&pl=17&initcwndbps=176250&spc=qEK7BzfSiRtoA-NxGlhkZV11AsWRZwQ&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=218.940&lmt=1666532561427872&mt=1686340426&fvip=4&fexp=24007246%2C24362687%2C51000011%2C51000024&beids=24350018&c=ANDROID&txp=1438434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAOpuEO3tcCuewi00dk4Y6Q9m1OZEgKkc56jMgOlux4SwAiEA0xzfpz53Tz2wVuEQvHZPhRCqTC0x7_iEyMvYrY-8MH8%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAKY_zH6HAnsi_K63GL49EcpwCPtpLp7Wyne489umB4kLAiBVNACXb8BIw2JqEC-0GM3Pt4Cgn81CqaTV4ayPFMIVwQ%3D%3D

Let’s break it down:

  • -g specifies that we only want to extract the URLs
  • -f specifies the type of media
  • 18 is the identifier for a dual-channel 360p video

3.3. Using yt-dlp With mpv

We can also stream. Moreover, we can view the full list of available formats for a video using the -F flag:

$ yt-dlp -F https://www.youtube.com/watch?v=5ocq6_3-nEw
ID  EXT   RESOLUTION FPS CH │   FILESIZE   TBR PROTO │ VCODEC        VBR ACODEC      ABR ASR MORE INFO
──────────────────────────────────────────────────────────────────────────────────────────────────────────────
...
160 mp4   256x144     30    │  955.39KiB   36k dash  │ avc1.4d400c   36k video only          144p, mp4_dash
278 webm  256x144     30    │    1.38MiB   53k dash  │ vp9           53k video only          144p, webm_dash
133 mp4   426x240     30    │    1.60MiB   61k dash  │ avc1.4d4015   61k video only          240p, mp4_dash
242 webm  426x240     30    │    2.47MiB   95k dash  │ vp9           95k video only          240p, webm_dash
134 mp4   640x360     30    │    3.46MiB  133k dash  │ avc1.4d401e  133k video only          360p, mp4_dash
18  mp4   640x360     30  2 │ ~  7.00MiB  262k https │ avc1.42001E  262k mp4a.40.2    0k 44k 360p
...

Notably, it contains a lot of formats that we can choose based on our requirements. We can feed these formats to mpv using the –ytdl-format option:

$ mpv --ytdl-format=18 <URL>

Similarly, we can also specify formats using a more memorable approach:

$ mpv --ytdl-format=bestvideo[height<=360]+bestaudio <URL>

In the –ytdl-format option, we specify that our video quality should be lower than or equal to 360p. For the audio, we choose the best available format.

4. Conclusion

In this article, we learned how we can stream videos from a remote resource in Linux. For that purpose, we explored different media players that we can use from the command-line.

Finally, we discussed the youtube-dl and yt-dlp utilities to help us extract and stream videos from sources that don’t offer direct stream URLs.

Comments are closed on this article!