f
This commit is contained in:
123
MPV/LUA/main.lua
123
MPV/LUA/main.lua
@@ -145,6 +145,81 @@ end
|
||||
|
||||
_lua_log('medeia lua loaded version=' .. tostring(MEDEIA_LUA_VERSION) .. ' script=' .. tostring(mp.get_script_name()))
|
||||
|
||||
-- Log to database (logs.db) for centralized error/message tracking
|
||||
-- This ensures all OSD messages and errors are persisted for debugging
|
||||
local function _log_to_db(level, message)
|
||||
message = tostring(message or ''):gsub('"', '\\"')
|
||||
level = tostring(level or 'INFO'):upper()
|
||||
|
||||
-- Find the repo root by looking for CLI.py upwards from script directory
|
||||
local repo_root = ''
|
||||
do
|
||||
local script_dir = mp.get_script_directory() or utils.getcwd() or ''
|
||||
if script_dir ~= '' then
|
||||
local function find_up(start_dir, relative_path, max_levels)
|
||||
local d = start_dir
|
||||
local levels = max_levels or 6
|
||||
for _ = 0, levels do
|
||||
if d and d ~= '' then
|
||||
local candidate = d .. '/' .. relative_path
|
||||
if utils.file_info(candidate) then
|
||||
return candidate
|
||||
end
|
||||
end
|
||||
local parent = d and d:match('(.*)[/\\]') or nil
|
||||
if not parent or parent == d or parent == '' then
|
||||
break
|
||||
end
|
||||
d = parent
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local cli = find_up(script_dir, 'CLI.py', 8)
|
||||
if cli and cli ~= '' then
|
||||
repo_root = cli:match('(.*)[/\\]') or ''
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if repo_root == '' then
|
||||
return -- Can't find repo root, skip logging to database
|
||||
end
|
||||
|
||||
-- Escape paths for Python subprocess
|
||||
repo_root = repo_root:gsub('\\', '/')
|
||||
|
||||
-- Use Python to write to the database since Lua can't easily access sqlite3
|
||||
-- We use a subprocess call with minimal Python to insert into logs.db
|
||||
local python = (opts and opts.python_path) and tostring(opts.python_path) or 'python'
|
||||
local db_path = repo_root .. '/logs.db'
|
||||
|
||||
local script = string.format(
|
||||
"import sqlite3,os;p='%s';c=sqlite3.connect(p) if os.path.exists(p) else None;c and (c.execute('INSERT INTO logs (level,module,message) VALUES (?,?,?)',('%s','mpv','%s')),c.commit(),c.close())",
|
||||
db_path:gsub('\\', '/'),
|
||||
level,
|
||||
message
|
||||
)
|
||||
|
||||
pcall(function()
|
||||
mp.command_native_async({ name = 'subprocess', args = { python, '-c', script }, cwd = nil }, function() end)
|
||||
end)
|
||||
end
|
||||
|
||||
-- Combined log: to file + database (for persistence and debugging)
|
||||
local function _log_all(level, text)
|
||||
if not text or text == '' then
|
||||
return
|
||||
end
|
||||
level = tostring(level or 'INFO'):upper()
|
||||
text = tostring(text)
|
||||
|
||||
-- Log to file
|
||||
_lua_log('[' .. level .. '] ' .. text)
|
||||
|
||||
-- Log to database (async, non-blocking)
|
||||
_log_to_db(level, text)
|
||||
end
|
||||
|
||||
local function ensure_uosc_loaded()
|
||||
if _uosc_loaded or _is_script_loaded('uosc') then
|
||||
_uosc_loaded = true
|
||||
@@ -2317,8 +2392,9 @@ function M.run_pipeline(pipeline_cmd, seeds, cb)
|
||||
end
|
||||
ensure_mpv_ipc_server()
|
||||
|
||||
-- Use longer timeout for .mpv -url commands since they may involve downloading
|
||||
local timeout_seconds = pipeline_cmd:match('%.mpv%s+%-url') and 120 or 30
|
||||
-- Use shorter timeout for .mpv -url commands since they just queue the URL (non-blocking)
|
||||
-- The actual URL resolution happens asynchronously in MPV itself
|
||||
local timeout_seconds = pipeline_cmd:match('%.mpv%s+%-url') and 10 or 30
|
||||
_run_pipeline_request_async(pipeline_cmd, seeds, timeout_seconds, function(resp, err)
|
||||
_lua_log('M.run_pipeline callback fired: resp=' .. tostring(resp) .. ', err=' .. tostring(err))
|
||||
if resp and resp.success then
|
||||
@@ -2776,6 +2852,7 @@ mp.register_script_message('medios-load-url-event', function(json)
|
||||
_lua_log('[LOAD-URL] Trimmed URL: "' .. url .. '"')
|
||||
if url == '' then
|
||||
_lua_log('[LOAD-URL] URL is empty')
|
||||
_log_all('ERROR', 'Load URL failed: URL is empty')
|
||||
mp.osd_message('URL is empty', 2)
|
||||
if ensure_uosc_loaded() then
|
||||
_lua_log('[LOAD-URL] Closing menu due to empty URL')
|
||||
@@ -2785,6 +2862,7 @@ mp.register_script_message('medios-load-url-event', function(json)
|
||||
end
|
||||
|
||||
mp.osd_message('Loading URL...', 1)
|
||||
_log_all('INFO', 'Load URL started: ' .. url)
|
||||
_lua_log('[LOAD-URL] Starting to load: ' .. url)
|
||||
|
||||
local function close_menu()
|
||||
@@ -2806,11 +2884,13 @@ mp.register_script_message('medios-load-url-event', function(json)
|
||||
local ok_load = pcall(mp.commandv, 'loadfile', url, 'replace')
|
||||
if ok_load then
|
||||
_lua_log('[LOAD-URL] Direct loadfile command sent successfully')
|
||||
_log_all('INFO', 'Load URL succeeded via direct load')
|
||||
mp.osd_message('URL loaded', 2)
|
||||
close_menu()
|
||||
return
|
||||
else
|
||||
_lua_log('[LOAD-URL] Direct loadfile command failed')
|
||||
_log_all('ERROR', 'Load URL failed: direct loadfile command failed')
|
||||
mp.osd_message('Load URL failed (direct)', 3)
|
||||
close_menu()
|
||||
return
|
||||
@@ -2824,24 +2904,57 @@ mp.register_script_message('medios-load-url-event', function(json)
|
||||
_lua_log('[LOAD-URL] Pipeline helper ready: ' .. tostring(helper_ready))
|
||||
|
||||
if not helper_ready then
|
||||
_lua_log('[LOAD-URL] Pipeline helper not available')
|
||||
mp.osd_message('Pipeline helper not running (try menu again)', 3)
|
||||
close_menu()
|
||||
_lua_log('[LOAD-URL] Pipeline helper not available, attempting to start...')
|
||||
_log_all('WARN', 'Pipeline helper not running, attempting auto-start')
|
||||
mp.osd_message('Starting pipeline helper...', 2)
|
||||
|
||||
-- Attempt to start the helper asynchronously
|
||||
attempt_start_pipeline_helper_async(function(success)
|
||||
if success then
|
||||
_lua_log('[LOAD-URL] Helper started successfully, retry Load URL from menu')
|
||||
_log_all('INFO', 'Pipeline helper started successfully')
|
||||
mp.osd_message('Helper started! Try Load URL again', 2)
|
||||
else
|
||||
_lua_log('[LOAD-URL] Failed to start helper')
|
||||
_log_all('ERROR', 'Failed to start pipeline helper')
|
||||
mp.osd_message('Could not start pipeline helper', 3)
|
||||
end
|
||||
close_menu()
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
-- Use pipeline to download/prepare the URL
|
||||
local pipeline_cmd = '.mpv -url ' .. quote_pipeline_arg(url) .. ' -play'
|
||||
_lua_log('[LOAD-URL] Sending to pipeline: ' .. pipeline_cmd)
|
||||
_lua_log('[LOAD-URL] Pipeline helper ready: ' .. tostring(_is_pipeline_helper_ready()))
|
||||
|
||||
-- Add a timeout message after a delay to give user feedback
|
||||
local timeout_timer = nil
|
||||
timeout_timer = mp.add_timeout(5, function()
|
||||
if timeout_timer then
|
||||
mp.osd_message('Still loading... (helper may be resolving URL)', 2)
|
||||
_log_all('WARN', 'Load URL still processing after 5 seconds')
|
||||
_lua_log('[LOAD-URL] Timeout message shown (helper still processing)')
|
||||
end
|
||||
end)
|
||||
|
||||
M.run_pipeline(pipeline_cmd, nil, function(resp, err)
|
||||
if timeout_timer then
|
||||
timeout_timer:kill()
|
||||
timeout_timer = nil
|
||||
end
|
||||
|
||||
_lua_log('[LOAD-URL] Pipeline callback received: resp=' .. tostring(resp) .. ', err=' .. tostring(err))
|
||||
if err then
|
||||
_lua_log('[LOAD-URL] Pipeline error: ' .. tostring(err))
|
||||
_log_all('ERROR', 'Load URL pipeline failed: ' .. tostring(err))
|
||||
mp.osd_message('Load URL failed: ' .. tostring(err), 3)
|
||||
close_menu()
|
||||
return
|
||||
end
|
||||
_lua_log('[LOAD-URL] URL loaded successfully')
|
||||
_log_all('INFO', 'Load URL succeeded')
|
||||
mp.osd_message('URL loaded', 2)
|
||||
close_menu()
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user