mpvnet-player / mpv.net

🎞 mpv.net is a media player for Windows with a modern GUI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Force subtitle location with alt+s

toprak opened this issue · comments

This template is meant for usage questions of mpv.net.

Some type of questions like questions about a problem require filling out an issue template:

Describe the problem
I only use one folder for my subtitles so I want it to open that specific folder every time I do alt+s, otherwise it's tiring to keep finding the subtitle folder.

To Reproduce
Default input.conf

In case of an audio or video problem, try to reproduce the problem using mpv instead of mpv.net.
If it can be reproduced with mpv, use the mpv issue tracker.

Steps to reproduce the behavior:
press alt+s

Expected behavior
I think we can specify a subtitle folder via input.conf or mpv.conf and access it with alt+s.

Screenshots
I do not think its needed since its already default config we talking about

Additional context
Add any other context about the problem here.

  1. mpv.net version mpv.net 7.1.0.0 (12/01/2024)
  2. Windows version 11
  3. GPU name RTX 3050
  4. Media info mkv files

If you're not against using PowerShell then you can use a script and have even more control over it:

-- opensubtitles.lua
local utils = require "mp.utils"

local subtitle_formats = "*.srt;*.ass;*.ssa;*.idx;*.sub;*.txt"

function open_subtitles(directory)
    local path = mp.get_property("path")
    local default_directory = path and utils.split_path(path) or nil

    directory = directory or default_directory

    if not directory then
        mp.msg.error("No directory provided")
        return
    end

    local open = utils.subprocess({playback_only=true, args={"powershell", "-NoProfile", "-Command", [[
        Add-Type -AssemblyName System.windows.forms;
        $f = [System.Windows.Forms.OpenFileDialog]::new();
        $f.InitialDirectory = "]]..directory..[[";
        $f.Filter = "Subtitles (" + "]]..subtitle_formats..[[" + ")|" + "]]..subtitle_formats..[[" + "|All files (*.*)|*.*";
        $f.Multiselect = $true;
        $f.ShowDialog() | Out-Null;
        $utf8 = New-Object System.Text.UTF8Encoding $false;
        [Console]::OutputEncoding = $utf8;
        $f.FileNames -join "`n"
    ]]}})
    local subtitle_files = open.stdout
    for subtitle_file in string.gmatch(subtitle_files, '([^\n]+)') do
        subtitle_file = subtitle_file:gsub("\r", "")
        if subtitle_file ~= "" then
            mp.commandv("sub_add", subtitle_file)
        end
    end
end

mp.add_key_binding(nil, "open", open_subtitles)

[edit] Added multiselect support

input.conf

a script-message-to opensubtitles open
b script-message-to opensubtitles open 'C:\test\test test\sub titles'

a opens directory of currently loaded file.
b opens custom path.

You could also probably define an invalid path (script-message-to opensubtitles open nil) and it should remember the last used directory, but i'm not certain how well that will work.

@Sneakpeakcss appreciate but it feels its slower than default alt+s

Well, that's unfortunate. The typical issue with any PowerShell solution, where it can either be quite fast depending on what you're trying to achieve, or take significantly longer for no apparent reason:

justpowershellthings

On my side it's either on par with script-message-to mpvnet load-sub or barely slower.

Though, i did try my luck with modifying LoadSubtitle:

void LoadSubtitle(IList<string> args)
{
using var dialog = new OpenFileDialog();
string path = Player.GetPropertyString("path");
if (File.Exists(path))
dialog.InitialDirectory = Path.GetDirectoryName(path);
dialog.Multiselect = true;
if (dialog.ShowDialog() == DialogResult.OK)
foreach (string filename in dialog.FileNames)
Player.CommandV("sub-add", filename);
}

diff --git a/src/MpvNet.Windows/GuiCommand.cs b/src/MpvNet.Windows/GuiCommand.cs
index afd7070..dfa10b7 100644
--- a/src/MpvNet.Windows/GuiCommand.cs
+++ b/src/MpvNet.Windows/GuiCommand.cs
@@ -80,9 +80,11 @@ void ShowDialog(Type winType)
     void LoadSubtitle(IList<string> args)
     {
         using var dialog = new OpenFileDialog();
-        string path = Player.GetPropertyString("path");
+        string path = args.Count == 1 ? args[0] : Player.GetPropertyString("path");
 
-        if (File.Exists(path))
+        if (Directory.Exists(path))
+            dialog.InitialDirectory = path;
+        else if (File.Exists(path))
             dialog.InitialDirectory = Path.GetDirectoryName(path);
 
         dialog.Multiselect = true;
a script-message-to mpvnet load-sub
b script-message-to mpvnet load-sub 'C:\test\test test\sub titles'

And it seems to work identically as the above lua script, so you might either try building mpv.net with this, or wait for an official implementation when stax76 comes back from development break.

@Sneakpeakcss Thank you so much for helps unfortunately i have no idea how to build it also i cant build it rn sadly. I tried another powershell script it works but whats really weird is your script is not appearing on my end. This one appears but a bit laggy but its k.

---Launch a dialog for opening files or URLs (PowerShell)
---@author ObserverOfTime
---@license 0BSD

local utils = require 'mp.utils'

local MULTIMEDIA = table.concat({
    '*.aac',
    '*.avi',
    '*.flac',
    '*.flv',
    '*.m3u',
    '*.m3u8',
    '*.m4v',
    '*.mkv',
    '*.mov',
    '*.mp3',
    '*.mp4',
    '*.mpeg',
    '*.mpg',
    '*.oga',
    '*.ogg',
    '*.ogv',
    '*.opus',
    '*.wav',
    '*.webm',
    '*.wmv',
}, ';')

local SUBTITLES = table.concat({
    '*.ass',
    '*.srt',
    '*.ssa',
    '*.sub',
    '*.txt',
}, ';')

---@class PSOpts
---@field title string
---@field text string
---@field args string[]
---@field template? string

---@param opts PSOpts
---@return fun()
local function PowerShell(opts)
    return function()
        local template = opts.template or [[& {
            Add-Type -AssemblyName System.Windows.Forms;
            [Windows.Forms.Application]::EnableVisualStyles();
            $dialog = New-Object Windows.Forms.OpenFileDialog;
            $dialog.Filter = %q;
            $dialog.Title = %q;

            # Set the Initial Directory
            $InitialDir = "C:\\Users\\test\\test"
            $dialog.InitialDirectory = [System.IO.Path]::GetFullPath($InitialDir)

            $dialog.Multiselect = $true;
            $dialog.ShowHelp = $true;
            $dialog.ShowDialog() > $null;
            Write-Output $dialog.FileNames;
            $dialog.Dispose();
        }]]
        
        local ontop = mp.get_property_native('ontop')
        mp.set_property_native('ontop', false)
        local powershell = utils.subprocess {
            args = {
                'powershell', '-NoProfile', '-Command',
                template:format(opts.text, opts.title)
            }, cancellable = false
        }
        mp.set_property_native('ontop', ontop)
        if powershell.status ~= 0 then return end
        for file in powershell.stdout:gmatch('[^\r\n]+') do
            mp.commandv(opts.args[1], file, opts.args[2])
        end
    end
end

mp.add_key_binding('Ctrl+f', 'open-files', PowerShell {
    title = 'Select Files',
    text = 'Multimedia Files|'..MULTIMEDIA,
    args = {'loadfile', 'append-play'},
})
mp.add_key_binding('Ctrl+F', 'open-url', PowerShell {
    title = 'Open URL',
    text = 'Enter the URL to open:',
    args = {'loadfile', 'replace'},
    template = [[& {
        Add-Type -AssemblyName Microsoft.VisualBasic;
        $url = [Microsoft.VisualBasic.Interaction]::InputBox(%q, %q);
        Write-Output $url;
    }]],
})
mp.add_key_binding('Alt+f', 'open-subs', PowerShell {
    title = 'Select Subs',
    text = 'Subtitle Files|'..SUBTITLES,
    args = {'sub-add', 'select'},
})

but whats really weird is your script is not appearing on my end

That's really weird. I assume it doesn't show any errors in console? The only thing that comes to mind would be wrong command:

a script-message-to opensubtitles open

opensubtitles corresponds to the scripts name, so maybe you didn't save it as opensubtitles.lua? The other thing would be that my script only opens the dialog if there's a file currently playing, where mpv.net (and your script) allows it to open when player is idle.
There's also a chance that the button you've chosen is already used somewhere else in input.conf.

You could try using the command straight from console script-message-to opensubtitles open and see if that does anything, it should at least show an error message when no file is playing.

@Sneakpeakcss yeah, ur script does not let user execute it on idle, but works like a charm even though its a bit slow its still usable i will use it till new build come thank you so much.