This repository has no description
2.8 kB
100 lines
1{ pkgs, ... }:
2
3let
4 dl_subs = pkgs.writers.writePython3Bin "dl_subs" {
5 libraries = [ pkgs.python3Packages.subliminal ];
6 flakeIgnore = [ "E501" ];
7 } ''
8 import babelfish
9 import subliminal
10 from pathlib import Path
11 import sys
12 import os
13
14 video_path = Path(sys.argv[1])
15 video = subliminal.scan_video(video_path)
16 cache_path = Path(os.path.expanduser("~/.cache/subliminal")) / f"{video.title}.srt"
17 if not cache_path.exists():
18 cache_path.parent.mkdir(parents=True, exist_ok=True)
19 language = babelfish.Language("eng")
20 sub = subliminal.download_best_subtitles([video], {language})[video][0]
21 cache_path.write_bytes(sub.content)
22 print(cache_path)
23 '';
24
25 autosub = pkgs.writeText "autosub.lua" ''
26 utils = require 'mp.utils'
27
28 -- Log function: log to both terminal and mpv OSD (On-Screen Display)
29 function log(string, secs)
30 secs = secs or 2 -- secs defaults to 2 when the secs parameter is absent
31 mp.msg.warn(string) -- This logs to the terminal
32 mp.osd_message(string, secs) -- This logs to mpv screen
33 end
34
35 function download()
36 log('Searching subtitles ...', 10)
37 path = mp.get_property('path')
38 result = utils.subprocess({ args = {"${dl_subs}/bin/dl_subs", path} })
39 if result.error == nil then
40 filename = string.gsub(result.stdout, "\n", "")
41 log(filename)
42 mp.commandv('sub_add', filename)
43 log('Subtitles ready!')
44 else
45 log('Subtitles failed downloading')
46 end
47 end
48
49 mp.add_key_binding('S', "download_subs", download)
50 '';
51
52 mpvInput = pkgs.writeText "mpv.input" ''
53 : script-binding console/enable
54 x add audio-delay -0.050
55 X add audio-delay 0.050
56 '';
57
58 mpvConfig = pkgs.writeText "mpv.conf" ''
59 osd-font-size=20
60 '';
61
62 mpv = pkgs.symlinkJoin {
63 name = "mpv";
64 paths = [
65 (pkgs.writers.writeDashBin "mpv" ''
66 set -efu
67 Y_RES=1081
68 # we need to disable sponsorblock local database because of
69 # https://github.com/po5/mpv_sponsorblock/issues/31
70 exec ${
71 pkgs.mpv.override {
72 scripts = with pkgs.mpvScripts; [
73 sponsorblock
74 quality-menu
75 vr-reversal
76 visualizer
77 ];
78 }
79 }/bin/mpv \
80 --no-config \
81 --input-conf=${mpvInput} \
82 --include=${mpvConfig} \
83 --script=${autosub} \
84 --ytdl-format="best[height<$Y_RES]" \
85 --script-opts=ytdl_hook-ytdl_path=${pkgs.yt-dlp}/bin/yt-dlp \
86 --script-opts-append=sponsorblock-local_database=no \
87 --audio-channels=2 \
88 "$@"
89 '')
90 pkgs.mpv
91 ];
92 };
93
94in
95{
96 environment.systemPackages = [
97 mpv
98 dl_subs
99 ];
100}