-- API 地址 / API Endpoint
local API_URL = "http://newgmapi.liulikeji.cn/api/ffmpeg"

-- 输入音频URL (可替换) / Input audio URL (replaceable)
local INPUT_URL = "https://git.liulikeji.cn/xingluo/CCTweaked-Demo/raw/branch/main/Demo/FFmpegApi-Demo/demo.mp3"


local function Get_dfpwm_url(INPUT_URL,API_URL)
    -- ===== 1. 发送HTTP请求 / Send HTTP request =====
    local requestData = {
        input_url = INPUT_URL,
        args = { "-vn", "-ar", "48000", "-ac", "1" }, -- DFPWM转换参数 / DFPWM conversion args
        output_format = "dfpwm"
    }

    local response, err = http.post(
        API_URL,
        textutils.serializeJSON(requestData),
        { ["Content-Type"] = "application/json" }
    )

    -- ===== 2. 读取数据 / Send HTTP request =====
    if not response then error("HTTP Request Failure: "..(err or "Unknown error")) 
    else
        -- 读取响应 / Read response
        local responseData = textutils.unserializeJSON(response.readAll())
        response.close()

        --返回下载链接 / Return download URL
        if responseData.status ~= "success" then error("Conversion failed:"..(responseData.error or "Unknown error")) 
        else
            return responseData.download_url
        end
    end
end


local download_url = Get_dfpwm_url(INPUT_URL,API_URL)
-- ===== 播放音频 / Play audio =====
print("\nConversion successful! Download URL:")
print(download_url)
print("\nPlaying...")
shell.run("speaker play "..download_url)
