2025-04-13 12:31:06 +08:00

49 lines
1.3 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 定义 API 地址
local API_URL = "http://ffmpeg.liulikeji.cn/api/ffmpeg"
-- 内置 MP3 URL可以替换成你的目标音频
local INPUT_URL = "http://example.com/audio.mp3"
-- 发送 POST 请求到 FFmpeg API
local function convertAndPlay()
-- 构造请求数据(使用 DFPWM 转换参数)
local requestData = {
input_url = INPUT_URL,
args = { "-vn", "-ar", "48000", "-ac", "1" },
output_format = "dfpwm"
}
-- 发送 HTTP POST 请求
local response = http.post(
API_URL,
textutils.serializeJSON(requestData),
{ ["Content-Type"] = "application/json" }
)
-- 检查请求是否成功
if not response then
printError("HTTP 请求失败,请检查网络或 API 地址")
return
end
-- 解析 JSON 响应
local responseData = textutils.unserializeJSON(response.readAll())
response.close()
-- 检查 API 返回状态
if responseData.status ~= "success" then
printError("音频转换失败: " .. (responseData.error or "未知错误"))
return
end
-- 获取下载 URL
local downloadUrl = responseData.download_url
print("转换成功!下载 URL: " .. downloadUrl)
-- 使用 speaker 播放音频
print("正在播放...")
shell.execute("speaker play " .. downloadUrl)
end
-- 调用函数
convertAndPlay()