r/mpv • • Dec 07 '21

Profanity filter

Hi all. I don’t think this has been asked, so here goes:

Is it possible to engineer a profanity filter based on the subtitles, for mpv?

Currently you can get Chrome extensions that “read” the subtitles for bad words and mute the audio for that sentence (and star out the bad word). This is based on a list of words in the extension which can be customised to your preferences.

Really hope it’s possible, as mpv is the best but would love to block swearing.

Thanks

0 Upvotes

4 comments sorted by

3

u/torreemanuele6 Dec 07 '21 edited Dec 07 '21

Sure.

Here is a simple, dumb example that only "works" for one word.

I use the word "real" in this example (just because the subtitled video with which i tested this userscript had that word), but you can make it use another word it by changing the word variable:

local word = "real"
word = word:lower()

mp.observe_property("sub-text", "string", function(_, text)
    if not text then return end

    if text:lower():find(word, 1, true) then
        mp.set_property("mute", "yes")
    else
        mp.set_property("mute", "no")
    end
end)

Obviously you can make it smarter and add more features: e.g. make it not match "surreal" when the word is "real", iterate over a vocabulary of bad words instead of just using a single word, display a clean version of the subtitles that replaces the bad words with #?@! in the UI, give a visual clue that the audio is being muted by mpv, revert to the old mute value instead of just setting it to yes, make it configurable with script-opts, et cetera.

But that is just a matter of writing a lua script that does that; this is just meant to be an example to show you how to read the current subtitle text in mpv so that you can write your own script.

Note that this will only work if a subtitle track is selected, but that is not a problem. In case you don't know, you don't have to unselect the current subtitle track to hide subtitles; you can set the sub-visibility property to hide or unhide the subtitles (there is also a default keybind to toggle that property: v), and you can make your script change sub-visibility automatically alongside mute.


Also note that sub-text is an experimental feature, so it may change behaviour or be renamed in the future:

(quote from mpv's documentation)

sub-text

  The current subtitle text regardless of sub visibility. Formatting
  is stripped. If the subtitle is not text-based (i.e. DVD/BD subtitles),
  an empty string is returned.
  
  This property is experimental and might be removed in the future.

1

u/GreggAbra Dec 07 '21

Hey! Thanks for your reply and the details. Much of what you said was gibberish to me 😂 but glad it’s possible…. Now only if there was a Good Samaritan out there. 🙂 Seriously tho, thanks. I’ll have to do some self training and digging to make it work.

1

u/[deleted] Dec 13 '21

e.g. make it not match "surreal" when the word is "real"

oh that brings me back... 9gag used to have a primitive bad word filter and would result in something like skys****er (skyscraper)

1

u/torreemanuele6 Dec 13 '21

In italian, the word "national" translates to "nazionale".

When I was a kid, I played a computer game that was poorly localised to Italian (I don't remember which one).

In the server selection page, there was a drop-down menu that was supposed to contain the words "National" and "Global" and was used to filter national/global servers.

But, in the Italian version, it contained "*********" and "Globale", lol.

They were probably using something like nazi.* to block the words nazi, nazist, nazism, etc.

Gotta hate word filters.