forked from
lass.pds.lassul.us/superconfig
This repository has no description
3.8 kB
104 lines
1{ self, ... }:
2{
3 perSystem =
4 { pkgs, system, ... }:
5 {
6 packages.mpv =
7 let
8 yt-dlp-master = self.packages.${system}.yt-dlp-master;
9 in
10 let
11 # Lazy-loaded autosub script
12 autosub = pkgs.writeText "autosub.lua" ''
13 utils = require 'mp.utils'
14
15 -- Log function: log to both terminal and mpv OSD (On-Screen Display)
16 function log(string, secs)
17 secs = secs or 2 -- secs defaults to 2 when the secs parameter is absent
18 mp.msg.warn(string) -- This logs to the terminal
19 mp.osd_message(string, secs) -- This logs to mpv screen
20 end
21
22 function download()
23 log('Searching subtitles ...', 10)
24 path = mp.get_property('path')
25
26 -- Build dl_subs lazily when needed
27 log('Building subtitle downloader...', 5)
28 result = utils.subprocess({ args = {"nix", "build", "--no-link", "--print-out-paths", "${self}#mpv-dl-subs"} })
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 self.wrapperModules.mpv.apply {
53 pkgs = pkgs;
54 scripts = with pkgs.mpvScripts; [
55 sponsorblock
56 quality-menu
57 vr-reversal
58 visualizer
59 ];
60 extraFlags = {
61 "--ytdl-format" = "bestvideo[height<=1080]+bestaudio/best";
62 "--script-opts" = "ytdl_hook-ytdl_path=${yt-dlp-master}/bin/yt-dlp";
63 "--script-opts-append" = "sponsorblock-local_database=no";
64 "--audio-channels" = "2";
65 "--script" = autosub;
66 };
67 "mpv.input".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 };
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}