optimize matching algorithm

This commit is contained in:
千住柱間 2024-07-22 17:19:52 -04:00
parent 2969a014ef
commit 6040539bb0
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -31,8 +31,8 @@ diff -ruN mpvacious/main.lua mpvacious-patched/main.lua
require('subs2srs')
diff -ruN mpvacious/utils/force-japanese.lua mpvacious-patched/utils/force-japanese.lua
--- mpvacious/utils/force-japanese.lua 1969-12-31 20:00:00.000000000 -0400
+++ mpvacious-patched/utils/force-japanese.lua 2024-07-22 15:31:41.919310736 -0400
@@ -0,0 +1,64 @@
+++ mpvacious-patched/utils/force-japanese.lua 2024-07-22 17:19:11.258733910 -0400
@@ -0,0 +1,75 @@
+local assdraw = require 'mp.assdraw'
+
+local preferred_alangs = {"jpn", "ja", "japanese"}
@ -46,30 +46,41 @@ diff -ruN mpvacious/utils/force-japanese.lua mpvacious-patched/utils/force-japan
+ "今、完全に日本語です!英語はなし!"
+}
+
+local function find_japanese_audio(tracks)
+ local regex = ".*[Jj][Pp].*"
+ for _, track in ipairs(tracks) do
+ if track.type == "audio" and string.match(track.title or "", regex) then
+ return track.id
+ end
+ end
+ return nil
+end
+
+local function find_japanese_sub(tracks)
+ local regex = ".*[Jj][Pp].*"
+ for _, track in ipairs(tracks) do
+ if track.type == "sub" and string.match(track.title or "", regex) then
+ return track.id
+ end
+ end
+ return nil
+end
+
+local function find_best_match(tracks, preferred_langs, track_type)
+ for _, lang in ipairs(preferred_langs) do
+ for _, track in ipairs(tracks) do
+ if track.type == track_type and track.lang == lang then
+ return track.id
+ end
+ end
+ end
+ return nil
+end
+
+function select_tracks()
+ local tracks = mp.get_property_native("track-list")
+ local selected_aid = nil
+ local selected_sid = nil
+
+ for _, lang in ipairs(preferred_alangs) do
+ for _, track in ipairs(tracks) do
+ if track.type == "audio" and track.lang == lang then
+ selected_aid = track.id
+ break
+ end
+ end
+ if selected_aid then break end
+ end
+
+ for _, lang in ipairs(preferred_slangs) do
+ for _, track in ipairs(tracks) do
+ if track.type == "sub" and track.lang == lang then
+ selected_sid = track.id
+ break
+ end
+ end
+ if selected_sid then break end
+ end
+ local selected_aid = find_japanese_audio(tracks) or find_best_match(tracks, preferred_alangs, "audio")
+ local selected_sid = find_japanese_sub(tracks) or find_best_match(tracks, preferred_slangs, "sub")
+
+ if selected_aid then
+ mp.set_property("aid", selected_aid)