fxmarty / rikai-mpv

A port of Rikaichamp Japanese dictionary and parser into mpv video player
MIT License
33 stars 3 forks source link

About rikai-mpv

rikai-mpv is a port of rikaichamp Japanese dictionary and parser into mpv video player.

As for the graphical part, it takes inspiration from interSubs script but has been largely rewritten for the Python side.

https://user-images.githubusercontent.com/9808326/115159800-2d914b00-a095-11eb-81cc-18a63eff74e7.mp4

Pieces of code are also borrowed from normal-jp and hikibiki-data.

Dependencies

By default, rikai-mpv uses Noto Serif CJK JP Light font: https://www.google.com/get/noto/#serif-jpan . If you wish to use an other one, make sure to change it in rikai_config.py.

Dependencies are:

rikai-mpv may work with different versions, but has not been tested. A port into Windows should be doable, although we rely on Unix sockets for lua <--> python <--> node.js communication.

Install

Currently, the script supports Linux only. It has not been tested for Wayland-based display and has been designed for X11. You can install as follow:

Clone in your mpv/scripts/ folder (the install will break elsewhere):

cd ~/.config/mpv/scripts/
git clone https://github.com/fxmarty/rikai-mpv.git

Add a symlink at the top of the scripts directory for mpv to detect it:

ln -s rikai-mpv/front.lua .

Then, go to rikai-mpv/rikai-backend/ and run:

yarn install
tsc

This is to install necessary dependencies, and compile typescript source into javascript.

If necessary, modify rikai_config.py to fit your needs. The available options are limited right now and should be expended. In particular, if you use several screens, set n_screen as a number corresponding to the screen you want the subtitles to be displayed on.

On KDE (kwin), go to Compositor settings and uncheck "Allow applications to block compositing". This is necessary to allow transparency. Screenshot.

Key bindings

On top of these bindings, mpv's built-in hotkeys for going to next/previous subtitle may prove useful:

# to add in ~/.config/mpv/input.conf
LEFT        no-osd sub-seek -1  # previous subtitle
RIGHT       no-osd sub-seek +1  # next subtitle

Important notes

Build

If you wish to do modifications to the TypeScript side, make sure to have installed typescript and yarn system-wide and do as follows in rikaichamp-backend:

yarn install

Modifications will be applied to rikai-mpv after compiling TypeScript into JavaScript with tsc, as rikai-mpv uses plain JavaScript with node.js.

Known issues

Outlining text in Qt is tricky. The current solution may well break repainting of the subtitles, and a better solution is welcome if found. If repainting of subtitles is broken (e.g. only some part is showing, or it is not showing at all even though the semi-transparent background is showing), the culprit is most likely here: https://github.com/fxmarty/rikai-mpv/blob/main/subtitles_popup_graphics.py#L452

Under the hood

The port of rikaichamp rikaichamp-backend is more or less v0.3.5. Everything related to the browser has been removed so that the parser is node-friendly. Note however that the popup HTML is still built with rikaichamp (using jsdom), and that it is still a web rendering that happens under the hood in Qt with a QWebEngineView instance for the popup.

Compared to interSubs, the subtitle rendering uses QTextEdit instead of QLabel, which offers much more flexibility. Notably, interSubs was not fit for Japanese parsing, as it relies on spaces between words to query a dictionary. This implementation should be more flexible if it were to be merged into interSubs, as it allows parsing as many characters as needed and not just separated by blanks, e.g. interSubs could not parse "living room" as a single word, and could also not parse languages such as Chinese or Thai.

A great thanks to birtles for helping me to get through rikaichamp's code!

License

The code borrowed from rikaichamp and modified is published under GNU v3 license. The rest of the code base is published under the MIT license.