Jump to content

MPV: Difference between revisions

From NixOS Wiki
imported>Samuelgrf
Edit examples to use overlays, since packageOverrides are deprecated.
added information about umpv
(16 intermediate revisions by 9 users not shown)
Line 1: Line 1:
There are 2 packages that provide an {{ic|mpv}} executable:
[https://mpv.io/ MPV] is an open-source command line media player.


* <code>mpv</code>
== Installation ==
* <code>mpv-with-scripts</code>


{{ic|mpv-with-scripts}} is a wrapper for the {{ic|mpv}} binary which adds {{ic|--script<nowiki>=</nowiki>}} arguments according to the scripts the wrapper was built with.
==== With nix-shell ====
<syntaxhighlight lang="bash" start="3">
nix-shell -p mpv
</syntaxhighlight>


If you'd like to add scripts to your {{ic|mpv}} wrapper, you'll need to create an overlay in {{ic|~/.config/nixpkgs/config.nix}} or {{ic|/etc/nixos/configuration.nix}} (if you are using NixOS):
==== With NixOS ====
<syntaxhighlight lang="text">
environment.systemPackages = [
  pkgs.mpv
];
</syntaxhighlight>
==== With Home Manager ====
<syntaxhighlight lang="text">
home.packages = [
  pkgs.mpv
];
</syntaxhighlight>
== Configuration ==


=== With Home Manager ===
==== Basic ====
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
nixpkgs.overlays = [
programs.mpv.enable = true;
   (self: super: {
</syntaxhighlight>
     mpv = super.mpv-with-scripts.override {
 
       scripts = [ self.mpvScripts.<your choice> ];
==== Advanced ====
     };
<syntaxhighlight lang="nix">
   })
programs.mpv = {
];
  enable = true;
 
   package = (
     pkgs.mpv-unwrapped.wrapper {
       scripts = with pkgs.mpvScripts; [
        uosc
        sponsorblock
      ];
 
      mpv = pkgs.mpv-unwrapped.override {
        waylandSupport = true;
      };
     }
  );
 
  config = {
    profile = "high-quality";
    ytdl-format = "bestvideo+bestaudio";
    cache-default = 4000000;
   };
};
</syntaxhighlight>
</syntaxhighlight>


All of the scripts for MPV can be found under the {{ic|mpvScripts}} attribute. You can search for them by running {{ic|nix search mpvScripts}}. The scripts are defined in the following Nixpkgs directory:
== Tips and Tricks ==
 
==== Where to get scripts ====
To find more scripts run this in a terminal: <syntaxhighlight lang="bash">
nix search nixpkgs mpvScripts
</syntaxhighlight>The scripts are also defined in the following [https://github.com/NixOS/nixpkgs/tree/master/pkgs/applications/video/mpv/scripts Nixpkgs directory].
 
==== Where to find override options and using umpv ====
The package override options are defined in the following [https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/video/mpv/default.nix Nixpkgs directory]. This includes a commonly used first party script called [https://github.com/mpv-player/mpv/blob/master/TOOLS/umpv umpv] which allows additional files to be appended to the playlist of an open instance. In the nix derivation of mpv, the umpv script is bundled into the program and can be ran from the command line with `umpv foo.ogg`. Note that umpv can only be ran with the name of the file being opened and cannot be ran with additional arguments or flags.
 
Also note that commands cannot be passed to mpv using socat when mpv is ran using the umpv python wrapper. For example, if you try to pause umpv with <code>echo '{"command": ["cycle", "pause"]}' | socat - /tmp/mpvsocket</code>, it will result in an error similar to the following:
<code>2025/05/07 22:49:15 socat[115919] E GOPEN: /tmp/mpvsocket: Connection refused</code>
 
== Troubleshooting ==
 
==== Error, unknown format ====
If you get the following sort of error, note that MPV currently uses the small ffmpeg version (ffmpeg_5) instead of the full version (ffmpeg_5-full).
 
<syntaxhighlight lang="shell">
$ mpv --log-file=foo.log av://v4l2:/dev/video5
[lavf] Unknown lavf format v4l2
Failed to recognize file format.


[https://github.com/NixOS/nixpkgs/tree/master/pkgs/applications/video/mpv/scripts {{ic|pkgs/applications/video/mpv/scripts}}]
Exiting... (Errors when loading file)
</syntaxhighlight>


So for example, for adding the MPRIS MPV script, use the following in your {{ic|~/.config/nixpkgs/config.nix}}:
To address this problem, you can use the following package configuration for ffmpeg.


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
programs.mpv = {
   nixpkgs.overlays = [
   enable = true;
     (self: super: {
 
       mpv = super.mpv-with-scripts.override {
  package = (
         scripts = [ self.mpvScripts.mpris ];
     pkgs.mpv-unwrapped.wrapper {
       mpv = pkgs.mpv-unwrapped.override {
         ffmpeg = pkgs.ffmpeg-full;
       };
       };
     })
     }
   ];
   );
}
};
</syntaxhighlight>
</syntaxhighlight>
== References ==
# https://github.com/mpv-player/mpv/wiki
# https://en.wikipedia.org/wiki/Mpv_(media_player)
# https://search.nixos.org/packages?query=mpv
# https://home-manager-options.extranix.com/?query=mpv
[[Category:Applications]]
[[Category:CLI Applications]]
[[Category:Media Player]]

Revision as of 01:43, 10 May 2025

MPV is an open-source command line media player.

Installation

With nix-shell

nix-shell -p mpv

With NixOS

environment.systemPackages = [
  pkgs.mpv
];

With Home Manager

home.packages = [ 
  pkgs.mpv 
];

Configuration

With Home Manager

Basic

programs.mpv.enable = true;

Advanced

programs.mpv = {
  enable = true;

  package = (
    pkgs.mpv-unwrapped.wrapper {
      scripts = with pkgs.mpvScripts; [
        uosc
        sponsorblock
      ];

      mpv = pkgs.mpv-unwrapped.override {
        waylandSupport = true;
      };
    }
  );

  config = {
    profile = "high-quality";
    ytdl-format = "bestvideo+bestaudio";
    cache-default = 4000000;
  };
};

Tips and Tricks

Where to get scripts

To find more scripts run this in a terminal:

nix search nixpkgs mpvScripts

The scripts are also defined in the following Nixpkgs directory.

Where to find override options and using umpv

The package override options are defined in the following Nixpkgs directory. This includes a commonly used first party script called umpv which allows additional files to be appended to the playlist of an open instance. In the nix derivation of mpv, the umpv script is bundled into the program and can be ran from the command line with `umpv foo.ogg`. Note that umpv can only be ran with the name of the file being opened and cannot be ran with additional arguments or flags.

Also note that commands cannot be passed to mpv using socat when mpv is ran using the umpv python wrapper. For example, if you try to pause umpv with echo '{"command": ["cycle", "pause"]}' | socat - /tmp/mpvsocket, it will result in an error similar to the following:

2025/05/07 22:49:15 socat[115919] E GOPEN: /tmp/mpvsocket: Connection refused

Troubleshooting

Error, unknown format

If you get the following sort of error, note that MPV currently uses the small ffmpeg version (ffmpeg_5) instead of the full version (ffmpeg_5-full).

$ mpv --log-file=foo.log av://v4l2:/dev/video5
[lavf] Unknown lavf format v4l2
Failed to recognize file format.

Exiting... (Errors when loading file)

To address this problem, you can use the following package configuration for ffmpeg.

programs.mpv = {
  enable = true;

  package = (
    pkgs.mpv-unwrapped.wrapper {
      mpv = pkgs.mpv-unwrapped.override {
        ffmpeg = pkgs.ffmpeg-full;
      };
    }
  );
};

References

  1. https://github.com/mpv-player/mpv/wiki
  2. https://en.wikipedia.org/wiki/Mpv_(media_player)
  3. https://search.nixos.org/packages?query=mpv
  4. https://home-manager-options.extranix.com/?query=mpv