Advertisement
alaestor

mpv.nix

Dec 17th, 2024 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.69 KB | Source Code | 0 0
  1. /* NOTE: relevant definitions from "utility"
  2.  
  3. # Create attrset {name=$name, value=$value}
  4. nameValuePair = name: value: { inherit name value; };
  5.  
  6. # Create attrset of many keys with the same value
  7. repeatValueForKeys = value: listOfKeys: builtins.listToAttrs (builtins.map (key: nameValuePair key value) listOfKeys);
  8. */
  9.  
  10. # TODO: modularize?
  11. # TODO: firejail?
  12.  
  13. {home-manager, pkgs, fetchFromGitHub, utility, ...}:
  14. let
  15.  
  16. # got the structure and idea to use profiles from https://github.com/arvigeus/nixos-config/blob/d653e29a76537eaeb8edd857d36a4d2bf0f00693/apps/mpv.nix
  17.  
  18. # mpv scripts that aren't in nixpkgs
  19. customScripts = {
  20.  
  21. cycleCommands = ( # https://github.com/CogentRedTester/mpv-scripts
  22. pkgs.stdenvNoCC.mkDerivation rec {
  23. pname = "mpv-cycleCommands";
  24. version = "2024-01-18_05aee33";
  25. src = pkgs.fetchFromGitHub {
  26. owner = "CogentRedTester";
  27. repo = "mpv-scripts";
  28. rev = "05aee333a232e858dc3e54f973edfe392cefe13b";
  29. hash = "sha256-JaPl7XcdsIJ3Y0iWxbCVxZz9yEMaFjJ8qMSbQ3Z/tr4=";
  30. } + "/cycle-commands.lua";
  31. dontBuild = true;
  32. dontUnpack = true;
  33. installPhase = "install -Dm644 ${src} $out/share/mpv/scripts/cycle-commands.lua";
  34. passthru.scriptName = "cycle-commands.lua";
  35. }
  36. );
  37.  
  38. clipboard = ( # https://github.com/CogentRedTester/mpv-clipboard
  39. pkgs.stdenvNoCC.mkDerivation rec {
  40. pname = "mpv-clipboard";
  41. version = "2024-01-18_05aee33";
  42. src = pkgs.fetchFromGitHub {
  43. owner = "CogentRedTester";
  44. repo = "mpv-clipboard";
  45. rev = "d5cb11094da23a2153370964b65dbbc44c3058df";
  46. hash = "sha256-426WEPd/hfvngAIzNuQOLhcXgpP5ekX7EmtlFCbHhAM=";
  47. } + "/clipboard.lua";
  48. dontBuild = true;
  49. dontUnpack = true;
  50. installPhase = "install -Dm644 ${src} $out/share/mpv/scripts/clipboard.lua";
  51. passthru.scriptName = "clipboard.lua";
  52. }
  53. );
  54.  
  55. copyPasteTime = ( # https://github.com/alaestor/mpv-copyPasteTime
  56. pkgs.stdenvNoCC.mkDerivation rec {
  57. pname = "mpv-copyPasteTime";
  58. version = "0.1.0";
  59. src = pkgs.fetchFromGitHub {
  60. owner = "alaestor";
  61. repo = "mpv-copyPasteTime";
  62. rev = "v${version}"; # commit or tag
  63. hash = "sha256-1hEj20z0/NVP3tSd4bBGCmC3O2eVIhUJWGJ5+lphx08=";
  64. } + "/copyPasteTime.lua";
  65. dontBuild = true;
  66. dontUnpack = true;
  67. installPhase = "install -Dm644 ${src} $out/share/mpv/scripts/copyPasteTime.lua";
  68. passthru.scriptName = "copyPasteTime.lua";
  69. }
  70. );
  71.  
  72. };
  73.  
  74. # mpv shaders that aren't in nixpkgs
  75. customShaders = {
  76.  
  77. SSSR = ( # https://gist.github.com/igv/2364ffa6e81540f29cb7ab4c9bc05b6b
  78. pkgs.stdenvNoCC.mkDerivation rec {
  79. name = "SSimSuperRes";
  80. src = pkgs.fetchurl {
  81. url = "https://gist.githubusercontent.com/igv/2364ffa6e81540f29cb7ab4c9bc05b6b/raw/15d93440d0a24fc4b8770070be6a9fa2af6f200b/SSimSuperRes.glsl";
  82. sha256 = "sha256-qLJxFYQMYARSUEEbN14BiAACFyWK13butRckyXgVRg8=";
  83. };
  84. dontBuild = true;
  85. dontUnpack = true;
  86. installPhase = "install -Dm644 ${src} $out/share/shaders/SSimSuperRes.glsl";
  87. }
  88. );
  89.  
  90. ArtCNN = ( # https://github.com/Artoriuz/ArtCNN
  91. pkgs.stdenvNoCC.mkDerivation rec {
  92. pname = "ArtCNN";
  93. version = "1.1.0";
  94. src = pkgs.fetchFromGitHub {
  95. owner = "Artoriuz";
  96. repo = "ArtCNN";
  97. rev = "v${version}";
  98. hash = "sha256-7im2NK1Jr2+Cc0lGyGjtk4YClMm5ZK6DfTZyr1jUYLI=";
  99. } + "/GLSL";
  100. dontBuild = true;
  101. dontUnpack = true;
  102. installPhase = ''
  103. mkdir -p $out/share/${pname}
  104. cp -r ${src} $out/share/${pname}
  105. '';
  106. }
  107. );
  108.  
  109. };
  110.  
  111. # paths to shaders from packages
  112. shaders = {
  113. defaultPack = "${pkgs.mpv-shim-default-shaders}/share/mpv-shim-default-shaders/shaders";
  114. artCNN = "${customShaders.ArtCNN}/share/ArtCNN/GLSL";
  115. sssrFile = "${customShaders.SSSR}/share/shaders/SSimSuperRes.glsl";
  116. };
  117.  
  118. # only including the ones I care about... I'll probably change and tune these in the future.
  119. # got the structure and idea to use profiles from https://github.com/arvigeus/nixos-config/blob/d653e29a76537eaeb8edd857d36a4d2bf0f00693/apps/mpv.nix
  120. scalers = {
  121.  
  122. ## MPV native upscaling (default)
  123. generic = {
  124. name = "hanning (default)";
  125. settings = {
  126. scale = "ewa_hanning";
  127. cscale = "ewa_lanczos";
  128. dscale = "mitchell";
  129. };
  130. };
  131.  
  132. ## SSimSuperRes
  133. super = {
  134. name = "superRes";
  135. settings = {
  136. scale = "ewa_hanning";
  137. cscale = "ewa_lanczos";
  138. dscale = "mitchell";
  139. glsl-shaders = [ "${shaders.sssrFile}" ];
  140. };
  141.  
  142. };
  143.  
  144. ## FSRCNNX
  145. fsrcnnx = {
  146. name = "general purpose [FSRCNNX16/SSimDown/KrigBilat]";
  147. settings = {
  148. cscale = "mitchell";
  149. dscale = "mitchell";
  150. glsl-shaders = [ "${shaders.defaultPack}/FSRCNNX_x2_16-0-4-1.glsl" ];
  151. glsl-shaders-append = [
  152. "${shaders.defaultPack}/SSimDownscaler.glsl"
  153. "${shaders.defaultPack}/KrigBilateral.glsl"
  154. ];
  155. };
  156. };
  157.  
  158. ## ArtCNN - NOTE: switch to non-compute variants for compatibility (omit "/Compute" and "_CMP")
  159. art = {
  160. name = "anime [ArtCNN_C4F32_CMP]";
  161. settings = {
  162. cscale = "mitchell";
  163. dscale = "mitchell";
  164. glsl-shaders = [ "${shaders.artCNN}/Compute/ArtCNN_C4F32_CMP.glsl" ];
  165. };
  166. };
  167. artde = {
  168. name = "anime denoising [ArtCNN_C4F32_DS_CMP]";
  169. settings = {
  170. cscale = "mitchell";
  171. dscale = "mitchell";
  172. glsl-shaders = [ "${shaders.artCNN}/Compute/ArtCNN_C4F32_DS_CMP.glsl" ];
  173. };
  174. };
  175.  
  176. ## Anime4k
  177. anime4k_a = {
  178. name = "Anime4K A (HQ) - For Very Blurry/Compressed";
  179. settings = {
  180. glsl-shaders = [ "${shaders.defaultPack}/Anime4K_Clamp_Highlights.glsl" ];
  181. glsl-shaders-append = [
  182. "${shaders.defaultPack}/Anime4K_Restore_CNN_VL.glsl"
  183. "${shaders.defaultPack}/Anime4K_Upscale_CNN_x2_VL.glsl"
  184. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x2.glsl"
  185. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x4.glsl"
  186. "${shaders.defaultPack}/Anime4K_Upscale_CNN_x2_M.glsl"
  187. ];
  188. };
  189. };
  190. anime4k_b = {
  191. name = "Anime4K B (HQ) - For Blurry/Ringing";
  192. settings = {
  193. glsl-shaders = [ "${shaders.defaultPack}/Anime4K_Clamp_Highlights.glsl" ];
  194. glsl-shaders-append = [
  195. "${shaders.defaultPack}/CAS-scaled.glsl"
  196. "${shaders.defaultPack}/Anime4K_Restore_CNN_Soft_VL.glsl"
  197. "${shaders.defaultPack}/Anime4K_Upscale_CNN_x2_VL.glsl"
  198. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x2.glsl"
  199. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x4.glsl"
  200. "${shaders.defaultPack}/Anime4K_Upscale_CNN_x2_M.glsl"
  201. ];
  202. };
  203. };
  204. anime4k_c = {
  205. name = "Anime4K C (HQ) - For Crisp/Sharp";
  206. settings = {
  207. glsl-shaders = [ "${shaders.defaultPack}/Anime4K_Clamp_Highlights.glsl" ];
  208. glsl-shaders-append = [
  209. "${shaders.defaultPack}/Anime4K_Upscale_Denoise_CNN_x2_VL.glsl"
  210. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x2.glsl"
  211. "${shaders.defaultPack}/Anime4K_AutoDownscalePre_x4.glsl"
  212. "${shaders.defaultPack}/Anime4K_Upscale_CNN_x2_M.glsl"
  213. ];
  214. };
  215. };
  216.  
  217. };
  218.  
  219. in
  220. {
  221. # pull in shaders from nixpkgs and customShaders
  222. home.packages = [
  223. pkgs.mpv-shim-default-shaders
  224. customShaders.SSSR
  225. customShaders.ArtCNN
  226. ];
  227.  
  228. # enable and configure mpv
  229. programs.mpv = {
  230. enable = true;
  231. package = pkgs.mpv;
  232.  
  233. # pull in scripts from nixpkgs and customScripts
  234. scripts = [
  235. pkgs.mpvScripts.seekTo
  236. pkgs.mpvScripts.thumbfast
  237. customScripts.cycleCommands
  238. customScripts.clipboard
  239. customScripts.copyPasteTime
  240. ];
  241.  
  242. # note: to convert from nix to raw conf files:
  243. # remove `= ""`, replace true/false -> yes/no, remove outer `''` and `"` quotes (not inner eg cycle-commands)
  244. # reference: https://github.com/mpv-player/mpv/blob/master/DOCS/man/options.rst
  245. config = {
  246. ### Player, Console, OSD/OSC, UI
  247. title = "\${filename} - mpv";
  248. keep-open = true; # don't close when finished
  249. border = true;
  250. fullscreen = false; # start windowed
  251. msg-color = true;
  252. msg-module = true;
  253. reset-on-next-file = "audio-delay,mute,pause,speed,sub-delay,video-aspect-override,video-pan-x,video-pan-y,video-rotate,video-zoom";
  254. no-resume-playback = "";
  255. no-input-default-bindings = ""; # disable keybinds; explicitly declared via `bindings` (input.conf)
  256. ### Screenshots
  257. screenshot-dir = "~/Pictures/mpv";
  258. screenshot-template = ''"T%wM%wS-%#01n-%F [%P]"'';
  259. screenshot-format = "jpg"; # jpg,png - png for reference/comparisons, jpg for quick sharing
  260. screenshot-jpeg-quality = 90;
  261. screenshot-png-compression = 0; # 0 to 10. 0 being no compression.
  262. screenshot-tag-colorspace = true;
  263. screenshot-high-bit-depth = true; # rame output bitdepth as the video
  264. ### Subtitles
  265. sid = "auto"; # default sub stream
  266. sub-auto = "fuzzy"; # external subs don't have to match the file name exactly to autoload
  267. sub-ass-use-video-data = "all"; # Backward compatibility for vsfilter fansubs
  268. sub-ass-scale-with-window = false; # May have undesired effects with signs being misplaced.
  269. sub-file-paths-append = ["ass" "srt" "sub" "subs" "subtitles" "en" "eng" "english"];
  270. demuxer-mkv-subtitle-preroll = ""; # try to correctly show embedded subs when seeking
  271. embeddedfonts = true; # use embedded fonts for SSA/ASS subs
  272. sub-fix-timing = false; # do not try to fix gaps (which might make it worse in some cases). Enable if there are scenebleeds.
  273. blend-subtitles = true; # yes=bake into frame before filtering, no=overlay after frame filtering
  274. ### Demuxer and cache
  275. cache = true;
  276. cache-on-disk = false; # false = cache in RAM rather than a temporary file
  277. demuxer-max-bytes = "1GiB"; # forward default is 400MiB
  278. demuxer-max-back-bytes = "1GiB";
  279. ### Renderer
  280. vo = "gpu-next";
  281. hwdec = "auto-safe"; # enable best HW decoder; `false` for software decoding
  282. # NOTE switch to opengl for compatibility
  283. gpu-api = "vulkan"; # `auto`, `vulkan`, `opengl`, or `d3d11` (typically for nvidia)
  284. vulkan-async-compute = true;
  285. vulkan-async-transfer = true;
  286. vd-lavc-dr = true; # decode directly to GPU video memory (or staging buffers)
  287. ### Audio
  288. volume = 50;
  289. volume-max = 150; # >100 is amplification
  290. audio-stream-silence = false; # "enabling this option is strongly discouraged" but can fix audio popping on random seek
  291. audio-channels = "stereo"; # downmix for headphones
  292. audio-file-auto = "fuzzy"; # external audio doesn't has to match the file name exactly to autoload
  293. audio-pitch-correction = true; # automatically insert scaletempo when playing with higher speed
  294. af = "lavfi=[loudnorm=I=-16:TP=-3:LRA=4]"; # loudness normalization; solves quiet dialogue problem in movies
  295. ## Video & Filtering
  296. dither = "error-diffusion";
  297. dither-depth = "auto";
  298. temporal-dither = true;
  299. deband = false; # could lose detail; toggle manually using hotkey (b for banding)
  300. deband-iterations = 3;
  301. deband-threshold = 20;
  302. deband-range = 16;
  303. deband-grain = 5; # dynamic grain: set to "0" if using a static grain shader
  304. scale = "ewa_hanning";
  305. scale-radius = 3.2383154841662362; # magic number from somewhere?
  306. cscale = "ewa_lanczos";
  307. dscale = "mitchell";
  308. scale-antiring = 0.6; # luma upscale deringing
  309. dscale-antiring = 0.6; # luma downscale deringing
  310. cscale-antiring = 0; # chroma upscale deringing
  311. #tscale = "box"; # frame interp... nty.
  312. #tscale-window = "quadric";
  313. #tscale-radius = 1.1;
  314. #tscale-clamp = 0.0;
  315. #tscale = "oversample";
  316. video-sync = "display-resample"; # resample audio to video, rather than video to audio. Try to adjust audio speed to compensate for drift
  317. interpolation = false; # reduce stuttering caused by mismatches
  318. correct-downscaling = true;
  319. linear-downscaling = false;
  320. sigmoid-upscaling = true;
  321. tone-mapping = "mobius"; # bt.2390
  322. target-colorspace-hint = true;
  323. target-prim = "auto"; # bt.709
  324. target-trc = "auto";
  325. target-peak = "auto"; # 960
  326. hdr-peak-percentile = 99.995; # from MPV's high-quality builtin profile
  327. hdr-contrast-recovery = 0.30; # from MPV's high-quality builtin profile
  328. hdr-compute-peak = true;
  329. #vf = ''format="colorlevels=full:colormatrix=auto"'';
  330. #video-output-levels = "full"; # "use your graphics driver's color range option instead, if available"
  331. # Default shaders
  332. glsl-shaders-clr = "";
  333. };
  334.  
  335. # see https://github.com/mpv-player/mpv/blob/master/etc/input.conf
  336. # and https://mpv.io/manual/master/#key-names
  337. bindings = {
  338. ### Navigation
  339. "CTRL+c" = "script-message-to copyPasteTime copyTime";
  340. "SHIFT+c" = "script-message-to copyPasteTime copyFrame";
  341. "CTRL+v" = "script-message-to copyPasteTime pasteTime";
  342. "t" = "script-message-to seek_to toggle-seeker";
  343. "." = "add chapter 1";
  344. "," = "add chapter -1";
  345. "RIGHT" = "seek 5"; # vanilla
  346. "LEFT" = "seek -5"; # vanilla
  347. "WHEEL_LEFT" = "seek -10"; # vanilla
  348. "WHEEL_RIGHT" = "seek 10"; # vanilla
  349. "SHIFT+RIGHT" = "frame-step";
  350. "SHIFT+LEFT" = "frame-back-step";
  351. "CTRL+RIGHT" = "seek +0.001 keyframes";
  352. "CTRL+LEFT" = "seek -0.01 keyframes";
  353. ### Playback
  354. "MBTN_LEFT" = "ignore"; # vanilla
  355. "MBTN_RIGHT" = "cycle pause"; # vanilla
  356. "SPACE" = "cycle pause"; # vanilla
  357. "{" = "add speed -0.25";
  358. "}" = "add speed 0.25";
  359. "[" = "add sub-delay -0.1";
  360. "]" = "add sub-delay +0.1";
  361. "-" = "add audio-delay -0.1";
  362. "=" = "add audio-delay +0.1";
  363. ### Track switching
  364. "a" = "cycle audio";
  365. "v" = "cycle video";
  366. "s" = "cycle sub";
  367. ### Screenshot
  368. "CTRL+s" = "screenshot";
  369. "SHIFT+s" = "screenshot video";
  370. ### Volume
  371. "UP" = "add volume 1";
  372. "DOWN" = "add volume -1";
  373. "SHIFT+UP" = "add volume 10";
  374. "SHIFT+DOWN" = "add volume -10";
  375. "WHEEL_UP" = "add volume 2"; # vanilla
  376. "WHEEL_DOWN" = "add volume -2"; # vanilla
  377. "m" = "cycle mute"; # vanilla
  378. ### Filtering & Adjustments
  379. "l" = ''script-message cycle-commands "set af \"lavfi=[loudnorm=I=-16:TP=-3:LRA=4]\";show-text \"Normalized Loudness\"" "set af \"\";show-text \"Unnormalized Loudness\""'';
  380. "d" = "cycle deinterlace";
  381. "b" = "cycle deband";
  382. "1" = "add contrast -1"; # vanilla
  383. "2" = "add contrast 1"; # vanilla
  384. "3" = "add brightness -1"; # vanilla
  385. "4" = "add brightness 1"; # vanilla
  386. "5" = "add gamma -1"; # vanilla
  387. "6" = "add gamma 1"; # vanilla
  388. "7" = "add saturation -1"; # vanilla
  389. "8" = "add saturation 1"; # vanilla
  390. ### Window
  391. "`" = "script-binding console/enable"; # vanilla
  392. "SHIFT+t" = "cycle ontop";
  393. "f" = "cycle fullscreen"; # vanilla
  394. "ESC" = "set fullscreen no";
  395. "MBTN_LEFT_DBL" = "cycle fullscreen";
  396. "CTRL+WHEEL_UP" = "add video-zoom 0.1"; # vanilla
  397. "CTRL+WHEEL_DOWN" = "add video-zoom -0.1"; # vanilla
  398. "p" = "show-progress";
  399. "e" = "cycle edition";
  400. "CLOSE_WIN" = "quit"; # vanilla
  401. "F3" = "script-binding stats/display-stats-toggle";
  402. ### Scaler profiles
  403. "CTRL+0" = ''show-text "Profile: ${scalers.generic.name}"; no-osd change-list glsl-shaders clr all; apply-profile generic'';
  404. "CTRL+1" = ''show-text "Profile: ${scalers.super.name}"; no-osd apply-profile super'';
  405. "CTRL+2" = ''show-text "Profile: ${scalers.fsrcnnx.name}"; no-osd apply-profile fsrcnnx'';
  406. "CTRL+3" = ''show-text "Profile: ${scalers.art.name}"; no-osd apply-profile art'';
  407. "CTRL+4" = ''show-text "Profile: ${scalers.artde.name}"; no-osd apply-profile artde'';
  408. "CTRL+5" = ''show-text "Profile: ${scalers.anime4k_a.name}"; no-osd apply-profile anime4k_a'';
  409. "CTRL+6" = ''show-text "Profile: ${scalers.anime4k_b.name}"; no-osd apply-profile anime4k_b'';
  410. "CTRL+7" = ''show-text "Profile: ${scalers.anime4k_c.name}"; no-osd apply-profile anime4k_c'';
  411. };
  412.  
  413. # using profiles for scalers
  414. profiles = {
  415. generic = scalers.generic.settings;
  416. super = scalers.super.settings;
  417. fsrcnnx = scalers.fsrcnnx.settings;
  418. art = scalers.art.settings;
  419. artde = scalers.artde.settings;
  420. anime4k_a = scalers.anime4k_a.settings;
  421. anime4k_b = scalers.anime4k_b.settings;
  422. anime4k_c = scalers.anime4k_c.settings;
  423. };
  424.  
  425. };
  426. xdg.mimeApps = {
  427. enable = true;
  428. #associations.added = { "inode/directory" = [ "mpv.desktop" ]; };
  429. defaultApplications = utility.repeatValueForKeys "mpv.desktop" [
  430. "application/mxf"
  431. "application/sdp"
  432. "application/smil"
  433. "application/streamingmedia"
  434. "application/vnd.apple.mpegurl"
  435. "application/vnd.ms-asf"
  436. "application/vnd.rn-realmedia"
  437. "application/vnd.rn-realmedia-vbr"
  438. "application/x-cue"
  439. "application/x-extension-m4a"
  440. "application/x-extension-mp4"
  441. "application/x-matroska"
  442. "application/x-mpegurl"
  443. "application/x-ogm"
  444. "application/x-ogm-video"
  445. "application/x-shorten"
  446. "application/x-smil"
  447. "application/x-streamingmedia"
  448. "video/3gp"
  449. "video/3gpp"
  450. "video/3gpp2"
  451. "video/avi"
  452. "video/divx"
  453. "video/dv"
  454. "video/fli"
  455. "video/flv"
  456. "video/mkv"
  457. "video/mp2t"
  458. "video/mp4"
  459. "video/mp4v-es"
  460. "video/mpeg"
  461. "video/msvideo"
  462. "video/ogg"
  463. "video/quicktime"
  464. "video/vnd.divx"
  465. "video/vnd.mpegurl"
  466. "video/vnd.rn-realvideo"
  467. "video/webm"
  468. "video/x-avi"
  469. "video/x-flc"
  470. "video/x-flic"
  471. "video/x-flv"
  472. "video/x-m4v"
  473. "video/x-matroska"
  474. "video/x-mpeg2"
  475. "video/x-mpeg3"
  476. "video/x-ms-afs"
  477. "video/x-ms-asf"
  478. "video/x-ms-wmv"
  479. "video/x-ms-wmx"
  480. "video/x-ms-wvxvideo"
  481. "video/x-msvideo"
  482. "video/x-ogm"
  483. "video/x-ogm+ogg"
  484. "video/x-theora"
  485. "video/x-theora+ogg"
  486. ];
  487. };
  488.  
  489. # ???
  490. # firejail = {
  491. # enable = true;
  492. # wrappedBinaries.mpv = {
  493. # executable = "${pkgs.mpv}/bin/mpv";
  494. # profile = "${pkgs.firejail}/etc/firejail/mpv.profile";
  495. # };
  496. # };
  497.  
  498. }
  499.  
Tags: MPV nix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement