View difference between Paste ID: QDx07uGC and rjmbpxFH
SHOW: | | - or go back to the newest paste.
1
local mp    = require 'mp'
2
local msg   = require 'mp.msg'
3
local utils = require 'mp.utils'
4
5
-- IPC socket path per OS
6
local ipc_socket_path
7
if package.config:sub(1,1) == "\\" then
8
    ipc_socket_path = "\\\\.\\pipe\\mpvsocket"
9
else
10
    ipc_socket_path = "/tmp/mpvsocket"
11
end
12
13
-- Escape JSON special chars
14
local function escape_json_str(str)
15
    if not str then return "" end
16
    return (str:gsub("\\", "\\\\")
17
               :gsub("\"", "\\\""))
18
end
19
20
-- Get full absolute path for the current file
21
local function get_full_path()
22
    local path = mp.get_property("path") or ""
23
    if path == "" then return "" end
24
    return path
25
end
26
27
-- Try open IPC pipe in write mode to check if main instance is running
28
local function try_connect_pipe(path)
29
    local f = io.open(path, "w")
30
    if f then f:close() return true end
31
    return false
32
end
33
34-
-- Send loadfile command with 'replace' to main instance
34+
-- Send loadfile command with 'replace' to main instance or use 'append' to ADD to PLAYLIST instead
35
local function send_file_to_main(path, filepath)
36
    local escaped_path = escape_json_str(filepath or "")
37
    local json = string.format('{"command": ["loadfile", "%s", "replace"]}', escaped_path)
38
39
    local f = io.open(path, "w")
40
    if not f then
41
        msg.error("Could not connect to IPC pipe: " .. path)
42
        return false
43
    end
44
    f:write(json .. "\n")
45
    f:close()
46
    msg.info("Sent file to main MPV: " .. filepath)
47
    return true
48
end
49
50
-- Create IPC server pipe for main instance
51
local function create_ipc_server(path)
52
    mp.set_property("input-ipc-server", path)
53
    msg.info("Created IPC server pipe: " .. path)
54
end
55
56
-- Determine instance mode on script load
57
local is_main_instance = false
58
if try_connect_pipe(ipc_socket_path) then
59
    is_main_instance = false
60
    msg.info("Detected existing MPV instance. This is a secondary instance.")
61
else
62
    create_ipc_server(ipc_socket_path)
63
    is_main_instance = true
64
    msg.info("No MPV instance detected. This is the main instance.")
65
end
66
67
-- Handle file start events
68
mp.register_event("start-file", function()
69
    local filepath = get_full_path()
70
71
    if filepath == "" then
72
        msg.warn("No valid file path detected. Continuing standby.")
73
        return
74
    end
75
76
    msg.info("Playing file: " .. filepath)
77
78
    if is_main_instance then
79
        msg.info("Main instance: playing file normally.")
80
        -- normal playback behavior
81
    else
82
        msg.info("Secondary instance: sending file and quitting.")
83
        if send_file_to_main(ipc_socket_path, filepath) then
84
            -- Delay quit to ensure IPC pipe flushes properly
85
            mp.add_timeout(0.1, function() mp.commandv("quit") end)
86
        else
87
            msg.error("Failed to send file; continuing as standalone.")
88
            create_ipc_server(ipc_socket_path)
89
            is_main_instance = true
90
        end
91
    end
92
end)