Examples of using mpv with youtube

If you don’t like to watch youtube videos in a browser this article probably what you need. MPV is a good choice for such a thing. But it need youtube-dl to be installed. I already have an article about how to install youtube-dl on Linux and Windows.

So let’s get started

The simplest example, without using any parameters is to copy the link to YouTube video. Try to use this link format.

mpv "https://www.youtube.com/watch?v=id"

Other link formats may not work properly.

The second example shows how to seek the video. It is in Hours:Minutes:Seconds format.

mpv --start=32:00 'link to youtube video'

In this case, the video will start from the 32 minute. This can be useful when you are watching a long video.

You will not believe, but MPV allows you to take screenshots of videos from YouTube. You don’t need anything but mpv+youtube-dl for this!

 mpv --no-audio --start=00:15:40 --frames=1 'link to youtube video' --o="/directory/path/screenshot.png"

This will save the screenshot in PNG format in the specified directory, do not forget to change it!

You can play Youtube videos in any available format using this command

mpv --ytdl-format <video format code>+<audio format code> 'link to youtube video'

You can list all available formats by using

youtube-dl --list-formats 'link to video'

OR

youtube-dl -F 'link to video'
...
[info] Available formats for <video id>:
format code  extension  resolution note
249          webm       audio only DASH audio   72k , opus @ 50k, 20.45MiB
250          webm       audio only DASH audio   94k , opus @ 70k, 27.24MiB
140          m4a        audio only DASH audio  130k , m4a_dash container, [email protected], 48.11MiB
171          webm       audio only DASH audio  150k , [email protected], 43.53MiB
251          webm       audio only DASH audio  175k , opus @160k, 52.35MiB
394          mp4        256x144    144p   85k , av01.0.05M.08, 24fps, video only, 29.25MiB
160          mp4        256x144    144p   92k , avc1.4d400c, 24fps, video only, 29.68MiB
278          webm       256x144    144p  113k , webm container, vp9, 24fps, video only, 35.33MiB
395          mp4        426x240    240p  141k , av01.0.05M.08, 24fps, video only, 45.97MiB
242          webm       426x240    240p  146k , vp9, 24fps, video only, 43.93MiB
133          mp4        426x240    240p  149k , avc1.4d4015, 24fps, video only, 46.07MiB
396          mp4        640x360    360p  226k , av01.0.05M.08, 24fps, video only, 72.43MiB
243          webm       640x360    360p  257k , vp9, 24fps, video only, 67.77MiB
...

So, for example

mpv --ytdl-format 243+251 'link to video'

In that case mpv will be play the video in 640×360 (VP9, 24fps) + [email protected] options.

The next example, by using this command format

mpv --ytdl-format="format string" 'link to video'

You can control automatic stream selection of mpv. By default, mpv does it itself

This command plays YouTube in best video and audio quality.

mpv --ytdl-format="bestvideo+bestaudio/best" 'link to video'

Or in this case it can even be simplified

mpv --ytdl-format bestvideo+bestaudio 'link to video'

YouTube video will play in maximum video and audio quality.

You can pick the video resolution by adding [height<=?value] parameter after “bestvideo”. height<= means less or exactly

mpv --ytdl-format="bestvideo[height<=?480]+bestaudio/best" 'link to video'

The value is the height of the video. Frequent values 360, 480, 720, 1080. But you can also use [width<=?value] parameter.

Or choose a specific file extension if your hardware doesn’t support other.

For example, we can choose mp4 extension, in which YouTube uses h264 video codec. For a change, there are many similar options

mpv --ytdl-format="bestvideo[ext=mp4][width<=640][height<=360]+bestaudio[ext=m4a]" 'link to video'

Or in another way. We can limit the load on the computer by reducing the fps value and exclude the use of the VP9 codec

mpv --ytdl-format="bestvideo[height<=?1080][fps<=?30][vcodec!=?vp9]+bestaudio/best" 'link to video'

This command allows you to select a video track that no more than 30 fps, no more than 1080 in height and also not use the VP9 codec.