This repository has no description
3.4 kB
106 lines
1{ pkgs, lib, ... }:
2
3let
4 dl_subs = pkgs.writers.writeDashBin "dl_subs" ''
5 set -efu
6 filename=$1
7 cd "$(dirname "$filename")"
8 filename=$(basename "$filename")
9 ${pkgs.subdl}/bin/subdl --output='/tmp/{m}.{M}.sub' "$filename" 1>&2
10 echo "/tmp/$filename.sub"
11 '';
12
13 autosub = pkgs.writeText "autosub.lua" ''
14 -- Requires Subliminal version 1.0 or newer
15 -- Make sure to specify your system's Subliminal location below:
16 local utils = require 'mp.utils'
17
18 -- Log function: log to both terminal and mpv OSD (On-Screen Display)
19 function log(string, secs)
20 secs = secs or 2 -- secs defaults to 2 when the secs parameter is absent
21 mp.msg.warn(string) -- This logs to the terminal
22 mp.osd_message(string, secs) -- This logs to mpv screen
23 end
24
25 function download()
26 log('Searching subtitles ...', 10)
27 path = mp.get_property('path')
28 result = utils.subprocess({ args = {"${dl_subs}/bin/dl_subs", path} })
29 if result.error == nil then
30 filename = string.gsub(result.stdout, "\n", "")
31 log(filename)
32 mp.commandv('sub_add', filename)
33 log('Subtitles ready!')
34 else
35 log('Subtitles failed downloading')
36 end
37 end
38
39 -- Control function: only download if necessary
40 function control_download()
41 duration = tonumber(mp.get_property('duration'))
42 if duration < 900 then
43 mp.msg.warn('Video is less than 15 minutes\n', '=> NOT downloading any subtitles')
44 return
45 end
46 -- There does not seem to be any documentation for the 'sub' property,
47 -- but it works on both internally encoded as well as external subtitle files!
48 -- -> sub = '1' when subtitles are present
49 -- -> sub = 'no' when subtitles are not present
50 -- -> sub = 'auto' when called before the 'file-loaded' event is triggered
51 sub = mp.get_property('sub')
52 if sub == '1' then
53 mp.msg.warn('Sub track is already present\n', '=> NOT downloading other subtitles')
54 return
55 end
56 mp.msg.warn('No sub track was detected\n', '=> Proceeding to download subtitles:')
57 download()
58 end
59
60 mp.add_key_binding('S', "download_subs", download)
61 '';
62
63 mpvInput = pkgs.writeText "mpv.input" ''
64 : script-binding console/enable
65 x add audio-delay -0.050
66 X add audio-delay 0.050
67 '';
68
69 mpvConfig = pkgs.writeText "mpv.conf" ''
70 osd-font-size=20
71 '';
72
73 mpv = pkgs.symlinkJoin {
74 name = "mpv";
75 paths = [
76 (pkgs.writeDashBin "mpv" ''
77 set -efu
78 Y_RES=1081
79 # we need to disable sponsorblock local database because of
80 # https://github.com/po5/mpv_sponsorblock/issues/31
81 exec ${pkgs.mpv.override {
82 scripts = with pkgs.mpvScripts; [
83 sponsorblock
84 quality-menu
85 ];
86 }}/bin/mpv \
87 --no-config \
88 --input-conf=${mpvInput} \
89 --include=${mpvConfig} \
90 --script=${autosub} \
91 --ytdl-format="best[height<$Y_RES]" \
92 --script-opts=ytdl_hook-ytdl_path=${pkgs.yt-dlp}/bin/yt-dlp \
93 --script-opts-append=sponsorblock-local_database=no \
94 --audio-channels=2 \
95 "$@"
96 '')
97 pkgs.mpv
98 ];
99 };
100
101in {
102 environment.systemPackages = [
103 mpv
104 dl_subs
105 ];
106}