Efficient audio-only mpv playback
This last year I’ve become gladly impressed with two command-line pieces of software for linux: youtube-dl
and mpv
.
youtube-dl
lets you download videos from youtube as well as from other video websites. And it does all the niceties, such as getting through playlists, parsing HTML to look for embedded videos in random webpages, and something that I appreciate a lot: it can merge several video and audio encodings (and subtitles) into the same media container. Magic.
mpv
is the latest incarnation of good old mplayer
(actually mplayer2
). In order to play media from the console, mpv
is the tool of choice.
I came across the Skammens Diskotek song a couple of weeks ago. And then I used youtube-dl
to get a local copy. And then I run mpv --ao null
to play just the audio, (setting the video output to the null
driver). And then I noticed something strange. 100% CPU usage on my main machine (a 7th-gen quad-core Core i7).
Turns out that --vo=null
disables the video output, but not the video decoding. And when youtube-dl
has downloaded the full 4HD 3840x2160 video stream, just decoding the video and sending it to /dev/null
takes a non-trivial amount of CPU power.
$ time mpv -vo null Sam\ \&\ Sky\ -\ Skammens\ Diskotek.webm
real 3m8,193s
user 2m30,247s
sys 0m0,946s
Wow.
The solution? Do as good nerds do, use ffmpeg
to split the audio track, and then feed it to mpv
via a pipe.
$ time ffmpeg -i Sam\ \&\ Sky\ -\ Skammens\ Diskotek.webm -vn -acodec copy -f opus - | mpv -
real 3m8,207s
user 0m8,134s
sys 0m1,002s
Oh yeah, that looks much better.
However, the ffmpeg
+mpv
approach has one disadvantage: the keyboard input is fed to ffmpeg
, not to mpv
. That means that I cannot, for example, seek the stream.
I thought about using FIFOs, this way I could get stdin
into mpv
while keeping a pipe from ffmpeg
. But then there’s the problem of creating and destroying the FIFO.
And then, I noticed something in the mpv
manual:
Usually, it’s better to disable video with
--no-video
instead.
Damn. mpv
had an option to skip the decoding of the video stream, and I missed it altogether. Let’s see:
$ time mpv --no-video Sam\ \&\ Sky\ -\ Skammens\ Diskotek.webm
real 3m8,117s
user 0m6,964s
sys 0m1,047s
Yup. And even a slightly better time than before.
ProTip™: always check the manual.