VLC 媒体播放器 LUA 扩展无法检测 Windows 中媒体文件是否实际存在

问题描述 投票:0回答:2

正如我在 Q-title 中所述,我正在尝试利用以 LUA 编程语言开发的现有 VLC 扩展。

Lua 扩展可以从这里引用,当我正确地将这个扩展放在

%ProgramFiles%\VideoLAN\VLC\lua\extensions
路径中,然后打开任何视频/音频文件并运行它,然后当我选择View > Remove current file from playlist and disk选项,它会关闭当前播放的媒体文件并抛出此错误:
lua info: [vlc-delete] error: File does not exist
.

不确定,但我怀疑这是由于当 FileName 和/或 FilePath 中包含空格时 Windows 引号问题。而且从错误来看,似乎

io.popen("if exist " .. file .. " (echo 1)") : read "*l" == "1"
对于正确检测文件是否实际存在是不可靠的。

我是 Lua 编程的新手,所以任何人都可以协助检查文件是否存在的更好方法,这些方法适用于最新的 VLC 版本,如 3.x+(因为我在 Windows 10/11 64 位中使用 VLC 3.0.17.4 64 位), 或者只是帮助解决这个提到的问题?

请注意,当脚本调用

fileExists
方法时,它会正确处理引号:
if not fileExists("\"" .. file .. "\"") then return nil, "File does not exist" end

windows file-io lua vlc delete-file
2个回答
0
投票

经过几个小时的故障排除,我能够解决这个问题,这是由于引用(包含空格)的文件路径传递给另一个函数的方式以及

os.remove
的 Lua 库方法无法处理传递的参数。

所以我有一个想法,我主要只使用 Windows 操作系统,那么为什么不将脚本转换为仅依赖 Windows 操作系统核心(批处理/cmd)功能,并安全检查文件是否存在,然后用这些核心删除Windows 功能也一样。

所以我就这样做了,让插件实际检查文件是否存在并删除文件,如果存在则删除它。这是工作代码:

-- Copy this file to %ProgramFiles%\VideoLAN\VLC\lua\extensions\ and restart VLC Media player.
function descriptor()
    return {
        title = "VLC Delete Media File(Windows only)";
        version = "1.0";
        author = "Vicky Dev";
        shortdesc = "&Remove current file from playlist and disk";
        description = [[
<h1>VLC Delete Media File(Windows only)</h1>"
When you're playing a file, use this to easily
delete the current file from your <b>playlist</b> and <b>disk</b> with one click.<br>
Disclaimer: The author is not responsible for damage caused by this extension.
        ]];
    }
end

function sleep(seconds)
    local t0 = os.clock()
    local tOriginal = t0
    while os.clock() - t0 <= seconds and os.clock() >= tOriginal do end
end

function removeItem()
    local id = vlc.playlist.current()
    vlc.playlist.delete(id)
    vlc.playlist.gotoitem(id + 1)
    vlc.deactivate()
end

function activate()
    local item = vlc.input.item()
    local uri = item:uri()
    uri = string.gsub(uri, "^file:///", "")
    uri = vlc.strings.decode_uri(uri)
    path = string.gsub(uri, "/", "\\")
    vlc.msg.info("[VLC Delete Media File(Windows only)] removing: "..uri.." : "..path)
    removeItem()
    retval, err = os.execute("if exist ".."\""..path.."\"".." @(call )")
    if (type(retval) == 'number' and retval == 0) then
        os.execute("del /f /a /q ".."\""..path.."\"")
    end
end

function click_ok()
    d:delete()
    vlc.deactivate()
end

function deactivate()
    vlc.deactivate()
end

function close()
    deactivate()
end

function meta_changed()
end

希望这可以帮助任何想要从播放器本身删除媒体文件的酷功能的人,而不是去该位置并执行此操作。


0
投票

我在 2 天前将 vlc 从 3.0.12 更新到 3.0.18,突然间脚本不再工作了。它会将其从播放列表中删除,但不会从驱动器中删除。然后我遇到了更新版本,但我仍然有同样的问题,我不知道为什么。

有人知道我做错了什么(除了更新 xD 之外)?在那个时间范围内,我没有改变任何其他东西,从工作到不工作。

关于永不更改正在运行的系统的另一个教训。

© www.soinside.com 2019 - 2024. All rights reserved.