This repository has no description
3.9 kB
110 lines
1{ self, ... }:
2{
3 perSystem =
4 { pkgs, system, ... }:
5 {
6 packages.mpv =
7 let
8 mpvInput = pkgs.writeText "mpv.input" ''
9 : script-binding console/enable
10 x add audio-delay -0.050
11 X add audio-delay 0.050
12 F script-binding quality_menu/video_formats_toggle
13 Alt+f script-binding quality_menu/audio_formats_toggle
14 '';
15
16 mpvConfig = pkgs.writeText "mpv.conf" ''
17 osd-font-size=20
18 '';
19
20 # Lazy-loaded autosub script
21 autosub = pkgs.writeText "autosub.lua" ''
22 utils = require 'mp.utils'
23
24 -- Log function: log to both terminal and mpv OSD (On-Screen Display)
25 function log(string, secs)
26 secs = secs or 2 -- secs defaults to 2 when the secs parameter is absent
27 mp.msg.warn(string) -- This logs to the terminal
28 mp.osd_message(string, secs) -- This logs to mpv screen
29 end
30
31 function download()
32 log('Searching subtitles ...', 10)
33 path = mp.get_property('path')
34
35 -- Build dl_subs lazily when needed
36 log('Building subtitle downloader...', 5)
37 result = utils.subprocess({ args = {"nix", "build", "--no-link", "--print-out-paths", "${self}#mpv-dl-subs"} })
38 if result.error ~= nil then
39 log('Failed to build subtitle downloader')
40 return
41 end
42
43 dl_subs_path = string.gsub(result.stdout, "\n", "") .. "/bin/dl_subs"
44
45 -- Download subtitles
46 result = utils.subprocess({ args = {dl_subs_path, path} })
47 if result.error == nil then
48 filename = string.gsub(result.stdout, "\n", "")
49 log(filename)
50 mp.commandv('sub_add', filename)
51 log('Subtitles ready!')
52 else
53 log('Subtitles failed downloading')
54 end
55 end
56
57 mp.add_key_binding('S', "download_subs", download)
58 '';
59
60 mpvPackage = pkgs.mpv.override {
61 scripts = with pkgs.mpvScripts; [
62 sponsorblock
63 quality-menu
64 vr-reversal
65 visualizer
66 ];
67 };
68
69 in
70 self.libWithPkgs.${system}.makeWrapper mpvPackage {
71 runtimeInputs = [ pkgs.yt-dlp ];
72 flagSeparator = "=";
73 flags = {
74 "--no-config" = { };
75 "--input-conf" = "${mpvInput}";
76 "--include" = "${mpvConfig}";
77 "--script" = "${autosub}";
78 "--ytdl-format" = "best[height<1081]";
79 "--script-opts" = "ytdl_hook-ytdl_path=${pkgs.yt-dlp}/bin/yt-dlp";
80 "--script-opts-append" = "sponsorblock-local_database=no";
81 "--audio-channels" = "2";
82 };
83 };
84
85 # Separate package for subtitle downloader
86 packages.mpv-dl-subs =
87 pkgs.writers.writePython3Bin "dl_subs"
88 {
89 libraries = [ pkgs.python3Packages.subliminal ];
90 flakeIgnore = [ "E501" ];
91 }
92 ''
93 import babelfish
94 import subliminal
95 from pathlib import Path
96 import sys
97 import os
98
99 video_path = Path(sys.argv[1])
100 video = subliminal.scan_video(video_path)
101 cache_path = Path(os.path.expanduser("~/.cache/subliminal")) / f"{video.title}.srt"
102 if not cache_path.exists():
103 cache_path.parent.mkdir(parents=True, exist_ok=True)
104 language = babelfish.Language("eng")
105 sub = subliminal.download_best_subtitles([video], {language})[video][0]
106 cache_path.write_bytes(sub.content)
107 print(cache_path)
108 '';
109 };
110}