提交demo1
This commit is contained in:
parent
771b5f5b86
commit
04a38a7da4
49
demo1.lua
Normal file
49
demo1.lua
Normal file
@ -0,0 +1,49 @@
|
||||
-- 定义 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()
|
Loading…
x
Reference in New Issue
Block a user