Fixing mpv not playing my webcam on NixOS
[note: I am not an advanced NixOS user. If I’m making a mistake, let me know! tom@tomontheinternet.com]
I use this command line to open my webcam in mpv on Linux:
# note: $DEVICE is the webcam location. ex: /dev/video4
mpv --demuxer-lavf-format=video4linux2 --demuxer-lavf-o-set=input_format=mjpeg av://v4l2:"$DEVICE" --profile=low-latency --untimed --vf=hflip --no-keepaspect-window
This command line shows my webcam with very little delay.
Problem
On NixOS, this did not work. It had been working on Arch. I wasn’t sure what was different. I would see errors like unknown lavf format video4linux2
or unknown lavf format v4l2
. This was frustrating.
Explanation
The issue was that the version of ffmpeg used by mpv on NixOS did not support v4l2. When the binary was compiled, this codec (I think?) was not included. On NixOS, there are two different ffmpeg packages: ffmpeg_5
and ffmpeg_5-full
. The ffmpeg_5-full
package is the one we want. It supports v4l2.
But, installing ffmpeg_5-full
isn’t going to do the job. We need to install mpv so that it is built with ffmpeg_5-full
.
Solution
I needed to create a nix overlay for the mpv-unwrapped
package.
Here is the overlay:
(self: super: {
mpv-unwrapped =
super.mpv-unwrapped.override { ffmpeg_5 = super.ffmpeg_5-full; };
})
This overlay is taking the mpv-unwrapped
package, and changing the ffmpeg
package it is built with.
You can look here for how this is built: https://github.com/NixOS/nixpkgs/blob/nixos-22.11/pkgs/applications/video/mpv/default.nix#L223. Frankly, I still find these hard to read.
Now, you need to make sure you are installing the mpv-unwrapped
package, and not the mpv
package. Don’t worry, the executable is called mpv
either way.
I figured this out by stumbling upon this:
https://github.com/NixOS/nixpkgs/issues/218859#issuecomment-1465283667
Closing
If there is a better way, or if this has helped you, please let me know. I’m on Twitter, or you can email me. tom@tomontheinternet.com