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