Articles

Anki mpv config

A small mpv input config file that parses subtitle text and forwards it to Anki using AnkiConnect.

A few years ago I wanted to be able to extract the current subtitle text of whatever I was watching with mpv, as reading subtitle files and manually extracting parts would be quite time consuming and cumbersome, especially in cases where I didn't have the subtitle files locally but they were instead streamed over to mpv (like when watching youtube videos).

Luckily mpv has just the command for this: sub-text, which fetches the current subtitle text. Now all we need to do is to create a bind that will pass the subtitle text to our clipboard. Let's bind Ctrl+c to that.

Ctrl+c run "/bin/bash" "-c" "echo \"${sub-text}\" | tr '\n' ' ' | xclip -selection clipboard"; show-text "Text kopiert"

Adding this line to our input.conf works great. We pipe the subtitle text to tr to remove newline characters, then pipe the output to our clipboard and finally inform the user that the text was copied. This could make creating anki cards a lot easier... but why stop there?

Instead of just passing the text to our clipboard, why don't we automatically open Anki and pass the subtitle text to a new card. The easiest way to do this is by using AnkiConnect, which will run a server on localhost:8765 to which we will post our request. Let's use Ctrl+n to do that.

Ctrl+n run "/bin/sh" "-c" "curl localhost:8765 -X POST -d '{\"action\": \"guiAddCards\", \"version\": 6, \"params\": {\"note\": {\"deckName\": \"Deutsch\", \"modelName\": \"Deutsch\", \"fields\": {\"Vorderseite\": \"\", \"Satz1\": \"'\"$(echo \"${sub-text}\" | tr '\n' ' ')\"'\"}}}}'"; show-text "Text weitergeleitet"

We use curl to post our request, it will call guiAddCards which opens the "New Card" Anki window, and we will then specify which deck and card type to use, after "deckName" and "modelName" respectively.

In my case the deckName is "Deutsch" and modelName, (which is just the card type you can see at the top of the New Card window next to the deck name) is also called "Deutsch". If you didn't create your own card type the default one is probably called "Basic"

Lastly we will pass the actual subtitle text to, in my case, the "Satz1" field. If you are using the "Basic" card type, you will use "Front" or "Back" here, depending on the card you want to create.

And there you have it, a relatively simple script that makes it really easy to add new Anki cards. After banging my head against a wall for a day trying to figure out which characters needed to be escaped to send a valid request, I'd say it was worth it.

Date written: 02. Aug 2025

07. Aug 2025 - Last page update.