zenwarr / mpv-config

Collection of my personalized mpv scripts and configuration files
20 stars 3 forks source link

[sub-search][Feature Request] Support for YouTube videos #4

Closed christoph-heinrich closed 1 year ago

christoph-heinrich commented 1 year ago

I would like to have support for searching in subtitles while playing YouTube videos.

I've looked into it a bit and already made a proof of concept. The subtitle track already has the link to it in the external-filename, but it is an EDL. Once the url is parsed out from there, the subtitle can be downloaded and converted using ffmpeg.

The 'download and convert' step would solve #3 as well I think.

Proof of concept ``` diff diff --git a/scripts/subtitle-search.lua b/scripts/subtitle-search.lua index 4a1e5ae..a5934cb 100644 --- a/scripts/subtitle-search.lua +++ b/scripts/subtitle-search.lua @@ -91,6 +91,10 @@ function get_sub_filename_async(track_name, on_done) local is_external = active_track.external local external_filename = active_track["external-filename"] + if external_filename:sub(1, 6) == "edl://" then + external_filename = external_filename:match("https://.*") + end + if is_external and external_filename then on_done(external_filename) return @@ -203,7 +207,13 @@ function load_sub(path, prefix) return cached end - local f = open_file(path) + local res = mp.command_native({ + name = "subprocess", + capture_stdout = true, + args = { "ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-i", path, "-vn", "-an", "-c:s", "srt", '/tmp/test.srt' } + }) + + local f = open_file('/tmp/test.srt') if not f then return nil end ```

P.S. It is generally recommended to not set hard coded keybindings in scripts and instead use nil as the key in the script and then bind a key with e.g. Ctrl+f script-binding subtitle_search/search-toggle in input.conf

zenwarr commented 1 year ago

Thank you! As for keybindings, I should review the way scripts in this repo define their bindings and unify them because it is complete mess currently.

christoph-heinrich commented 1 year ago

I've tried to use it on this video but unfortunately current master doesn't work. I always get "External subtitles not found". I've also tried it on a second video, but it also didn't work there.

When trying it with my provided proof of concept on top of 2a85d058403e67053eb3a70d1159718d884ec7a1 it does work.

I haven't dug into the code to find where things fail, but maybe I'll find some time in the coming days.