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