l mpv autosub: switch to subdl

This commit is contained in:
lassulus 2023-07-02 19:43:23 +02:00
parent e9ed4ea7ed
commit 0660f1de49

View File

@ -1,28 +1,10 @@
{ pkgs, lib, ... }: { pkgs, lib, ... }:
let let
dl_subs = pkgs.writers.writeDashBin "dl_subs" ''
download_subs = pkgs.writers.writePython3 "download_sub" { filename=$1
libraries = [ pkgs.python3Packages.subliminal ]; ${pkgs.subdl}/bin/subdl --output='/tmp/{m}.{M}.sub' "$filename" 1>&2
} '' echo "/tmp/$(basename "$filename").sub"
from subliminal import download_best_subtitles, scan_video
from babelfish import Language
import sys
video_filename = sys.argv[1]
vid = scan_video(video_filename)
try:
sub = download_best_subtitles([vid], {Language('eng')})[vid][0]
filename = '/tmp/' + vid.title + '.srt'
with open(filename, 'wb+') as file:
file.write(sub.content)
print(filename)
except: # noqa
print("/dev/null")
''; '';
autosub = pkgs.writeText "autosub.lua" '' autosub = pkgs.writeText "autosub.lua" ''
@ -39,10 +21,9 @@ let
function download() function download()
log('Searching subtitles ...', 10) log('Searching subtitles ...', 10)
table = { args = {"${download_subs}", mp.get_property('path')} } path = mp.get_property('path')
result = utils.subprocess(table) result = utils.subprocess({ args = {"${dl_subs}/bin/dl_subs", path} })
if result.error == nil then if result.error == nil then
-- remove trailing newline from subtitle filename
filename = string.gsub(result.stdout, "\n", "") filename = string.gsub(result.stdout, "\n", "")
log(filename) log(filename)
mp.commandv('sub_add', filename) mp.commandv('sub_add', filename)
@ -122,5 +103,6 @@ let
in { in {
environment.systemPackages = [ environment.systemPackages = [
mpv mpv
dl_subs
]; ];
} }