You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Reaper内置Lua如何正确读取含重音、变音符号的UTF8文件名与路径

解决方案

根因说明

你现有代码失效的核心原因是依赖io.popen调用系统shell命令(Windows下的dir)获取文件列表,不同系统的shell默认输出编码和Reaper内置Lua环境使用的UTF-8编码不兼容,带变音、重音符号的字符会出现编码乱码,导致识别失效。同时现有代码仅适配Windows系统,无法在Mac、Linux上运行。

最优实现方案

Reaper内置了原生的文件遍历API,完全不需要调用系统shell,从根源上避开编码兼容问题,且天然跨三大操作系统,不需要安装任何第三方扩展。
优化后的完整代码如下:

----------------------------------------------
-- SCAN RENDERED AUDIO
----------------------------------------------
function scandir(directory, format)
    local i = 0
    local t = ""
    local LF = "\n"
    -- 支持的音频格式列表
    local support_exts = {
        [".wav"] = true,
        [".mp3"] = true,
        [".flac"] = true,
        [".mov"] = true,
        [".ogg"] = true,
        [".mp4"] = true
    }
    -- 统一路径末尾的分隔符,避免拼接出错
    if directory:sub(-1) ~= "/" and directory:sub(-1) ~= "\\" then
        directory = directory .. package.config:sub(1,1)
    end
    -- 调用Reaper原生API遍历文件,索引从0开始
    local index = 0
    while true do
        local filename = reaper.EnumerateFiles(directory, index)
        if not filename then break end -- 遍历结束退出循环
        -- 匹配后缀名
        local extension = filename:match("^.+(%..+)$")
        if extension and support_exts[extension:lower()] then -- 后缀不区分大小写
            local uriFormat = filename:gsub(" ", "%%20")
            if format == 1 then
                t = t..'<tr class="Rendered"><td>'..directory..'</td><td>'..filename..'</td><td><audio controls src="'..directory..'/'..uriFormat..'"/></td></tr>'
            elseif format == 2 then
                t = t..directory..','..filename..','..directory..filename..LF
            end
            i = i + 1
        end
        index = index + 1
    end
    return t
end

方案优势

  • 完全基于Reaper内置API实现,不需要依赖任何第三方库,符合你的环境限制
  • 原生支持UTF-8编码的路径和文件名,不管是重音、变音符号还是其他非ASCII字符都可以正常识别读取
  • 天然兼容Windows、Mac、Linux三大操作系统,不需要额外做系统适配
  • 额外增加了后缀名大小写兼容逻辑,比如.WAV、.Mp3这类格式也可以正常识别

内容的提问来源于stack exchange,提问作者Tormy Van Cool

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 01:36:07