diff --git a/devices/nos/default.nix b/devices/nos/default.nix index 57d39b5f..74f25094 100644 --- a/devices/nos/default.nix +++ b/devices/nos/default.nix @@ -14,6 +14,7 @@ in { ./modules/qbittorrent ./modules/snapraid.nix ./modules/subtitles/sub-clean.nix + ./modules/subtitles/subsync ]; vars = { diff --git a/devices/nos/modules/subtitles/subsync/default.nix b/devices/nos/modules/subtitles/subsync/default.nix new file mode 100644 index 00000000..cc37527b --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/default.nix @@ -0,0 +1,45 @@ +{ + config, + pkgs, + ... +}: let + inherit (config.vars) mainUser; + + sphinxbase = pkgs.callPackage ./sphinxbase.nix {}; + pocketsphinx = pkgs.callPackage ./pocketsphinx.nix {inherit sphinxbase;}; + subsync = pkgs.callPackage ./subsync.nix {inherit sphinxbase pocketsphinx;}; +in { + systemd = { + services.subsync-job = { + serviceConfig = { + Type = "oneshot"; + User = mainUser; + Group = config.users.users.${mainUser}.group; + }; + + path = with pkgs; [ + findutils + subsync + (writeShellApplication { + name = "sync-sub"; + runtimeInputs = [subsync]; + text = '' + # TODO: sync on a specific file + # $1 = file path + ''; + }) + ]; + + script = '' + find /data/anime -name '*.srt' -exec sync-sub "{}" \; + find /data/movies -name '*.srt' -exec sync-sub "{}" \; + find /data/tv -name '*.srt' -exec sync-sub "{}" \; + ''; + }; + timers.subsync-job = { + wantedBy = ["timers.target"]; + partOf = ["subsync-job.service"]; + timerConfig.OnCalendar = ["0:00:00"]; + }; + }; +} diff --git a/devices/nos/modules/subtitles/subsync/patches/cmd_ln.patch b/devices/nos/modules/subtitles/subsync/patches/cmd_ln.patch new file mode 100644 index 00000000..28011ba9 --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/patches/cmd_ln.patch @@ -0,0 +1,65 @@ +diff --git a/gizmo/media/speechrec.h b/gizmo/media/speechrec.h +index de7a932..076c8fe 100644 +--- a/gizmo/media/speechrec.h ++++ b/gizmo/media/speechrec.h +@@ -5,6 +5,7 @@ + #include "text/words.h" + #include + #include ++#include + + + class SpeechRecognition : public AVOutput + +diff --git a/gizmo/media/speechrec.cpp b/gizmo/media/speechrec.cpp +index 4fe0bf7..fd041b9 100644 +--- a/gizmo/media/speechrec.cpp ++++ b/gizmo/media/speechrec.cpp +@@ -3,6 +3,7 @@ + #include "general/exception.h" + #include + #include ++#include + + using namespace std; + +@@ -30,28 +31,13 @@ SpeechRecognition::~SpeechRecognition() + + void SpeechRecognition::setParam(const string &key, const string &val) + { +- arg_t const *args = ps_args(); ++ ps_arg_t const *args = ps_args(); + + for (size_t i = 0; args[i].name != NULL; i++) + { + if (key == args[i].name) + { +- int type = args[i].type; +- if (type & ARG_INTEGER) +- cmd_ln_set_int_r(m_config, key.c_str(), atol(val.c_str())); +- else if (type & ARG_FLOATING) +- cmd_ln_set_float_r(m_config, key.c_str(), atof(val.c_str())); +- else if (type & ARG_STRING) +- cmd_ln_set_str_r(m_config, key.c_str(), val.c_str()); +- else if (type & ARG_BOOLEAN) +- cmd_ln_set_boolean_r(m_config, key.c_str(), +- !(val.empty() || val == "0")); +- else +- throw EXCEPTION("invalid parameter type") +- .module("SpeechRecognition", "setParameter") +- .add("parameter", key) +- .add("value", val) +- .add("type", type); ++ cmd_ln_set_str_extra_r(m_config, key.c_str(), val.c_str()); + + return; + } +@@ -89,7 +75,7 @@ void SpeechRecognition::start(const AVStream *stream) + throw EXCEPTION("can't init Sphinx engine") + .module("SpeechRecognition", "ps_init"); + +- int32_t frate = cmd_ln_int32_r(m_config, "-frate"); ++ int32_t frate = ((cmd_ln_access_r(m_config, "-frate"))->val).fl; + m_framePeriod = 1.0 / (double)frate; + + if (frate == 0) diff --git a/devices/nos/modules/subtitles/subsync/patches/cstdint.patch b/devices/nos/modules/subtitles/subsync/patches/cstdint.patch new file mode 100644 index 00000000..adb6a56e --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/patches/cstdint.patch @@ -0,0 +1,25 @@ +diff --git a/gizmo/text/utf8.h b/gizmo/text/utf8.h +index 7c3bcdb..99f1a92 100644 +--- a/gizmo/text/utf8.h ++++ b/gizmo/text/utf8.h +@@ -2,6 +2,7 @@ + #define __UTF8_H__ + + #include ++#include + + + class Utf8 + +diff --git a/gizmo/text/ssa.h b/gizmo/text/ssa.h +index 66d9918..170e265 100644 +--- a/gizmo/text/ssa.h ++++ b/gizmo/text/ssa.h +@@ -5,6 +5,7 @@ + #include + #include + #include ++#include + + + class SSAParser diff --git a/devices/nos/modules/subtitles/subsync/pocketsphinx.nix b/devices/nos/modules/subtitles/subsync/pocketsphinx.nix new file mode 100644 index 00000000..cdc6a256 --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/pocketsphinx.nix @@ -0,0 +1,26 @@ +{ + cmake, + fetchFromGitHub, + pkg-config, + sphinxbase, + stdenv, + ... +}: +stdenv.mkDerivation rec { + name = "pocketsphinx"; + version = "unstable"; + + src = fetchFromGitHub { + owner = "cmusphinx"; + repo = "pocketsphinx"; + rev = "7be89aae3e76568e02e4f3d41cf1adcb7654430c"; + hash = "sha256-imrwUIpORpfInitVjU11SKPPpjvObKyfI8IB4f41hfY="; + }; + + buildInputs = [pkg-config]; + nativeBuildInputs = [cmake sphinxbase]; + + postFixup = '' + cp -ar ${src}/src/util $out/include + ''; +} diff --git a/devices/nos/modules/subtitles/subsync/sphinxbase.nix b/devices/nos/modules/subtitles/subsync/sphinxbase.nix new file mode 100644 index 00000000..299cb496 --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/sphinxbase.nix @@ -0,0 +1,28 @@ +{ + autoreconfHook, + bison, + fetchFromGitHub, + pkg-config, + python3, + stdenv, + swig2, + ... +}: +stdenv.mkDerivation { + name = "sphinxbase"; + version = "unstable"; + + buildInputs = [bison pkg-config python3 swig2]; + nativeBuildInputs = [autoreconfHook]; + + autoreconfPhase = '' + ./autogen.sh + ''; + + src = fetchFromGitHub { + owner = "cmusphinx"; + repo = "sphinxbase"; + rev = "617e53691889336a482631380f75b453445d0dae"; + hash = "sha256-w/Huz4+crTzdiSyQVAx0h3lhtTTrtPyKp3xpQD5EG9g="; + }; +} diff --git a/devices/nos/modules/subtitles/subsync/subsync.nix b/devices/nos/modules/subtitles/subsync/subsync.nix new file mode 100644 index 00000000..43b173b4 --- /dev/null +++ b/devices/nos/modules/subtitles/subsync/subsync.nix @@ -0,0 +1,66 @@ +{ + fetchFromGitHub, + ffmpeg, + pkg-config, + pocketsphinx, + python3Packages, + sphinxbase, + ... +}: let + inherit (builtins) concatStringsSep; +in +python3Packages.buildPythonPackage { + pname = "subsync"; + version = "unstable"; + + src = fetchFromGitHub { + owner = "sc0ty"; + repo = "subsync"; + rev = "8e0cf71960b9a5418acb60a1910cf3295d67e6bf"; + hash = "sha256-jUur1U1yNShQx70/mj36+sGoVk8+E5hQUV/G79q2A2k="; + }; + + buildInputs = [ + ffmpeg + pkg-config + pocketsphinx + sphinxbase + ]; + + nativeBuildInputs = with python3Packages; [ + pip + setuptools + wheel + ]; + + propagatedBuildInputs = with python3Packages; [ + certifi + cryptography + pybind11 + pycryptodome + pysubs2 + pyyaml + requests + utils + ]; + + patches = [ + ./patches/cmd_ln.patch + ./patches/cstdint.patch + ]; + + # The tests are for the GUI + doCheck = false; + + # 'pip install .' takes care of building the package + buildPhase = ""; + + installPhase = '' + python -m pip install . ${concatStringsSep " " [ + "--no-index" + "--no-warn-script-location" + "--prefix=\"$out\"" + "--no-cache" + ]} + ''; +}