Files
resty_functions/scripts/convert_audio.lua
T

38 lines
1.3 KiB
Lua
Raw Normal View History

2022-01-28 02:25:18 +08:00
-- 指定模块引用目录,否则无法加载同目录下的其他文件
2022-02-08 13:57:15 +08:00
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
2022-02-08 14:14:57 +08:00
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
2022-02-08 13:57:15 +08:00
2021-12-30 22:04:57 +08:00
local cjson = require "cjson"
2022-01-14 14:11:17 +08:00
local uuid = require "resty.jit-uuid"
2022-02-08 00:25:20 +08:00
local upyun_upload = require("upyun_upload")
local get_wx_media = require("wx_download")
2021-12-30 22:04:57 +08:00
2021-12-30 22:22:15 +08:00
local function convert()
2022-01-14 14:11:17 +08:00
2021-12-30 22:22:15 +08:00
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
local req_body = cjson.decode(ngx.req.get_body_data())
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
2021-12-30 22:04:57 +08:00
2022-02-11 22:16:59 +08:00
local input_audio_file = get_wx_media(req_body["payload"]["media_id"]);
2021-12-30 23:00:43 +08:00
local converted_audio_file = file_dir .. "/" .. uuid.generate_v4() .. ".mp3";
ngx.log(ngx.INFO, "Input audio file -> ", input_audio_file);
ngx.log(ngx.INFO, "Converted audio file -> ", converted_audio_file);
local cmd = "/usr/local/bin/file_convert" .. " " .. input_audio_file .. " " .. converted_audio_file
2021-12-30 22:04:57 +08:00
2021-12-30 23:00:43 +08:00
ngx.log(ngx.INFO, 'Audio convert command -> ', cmd)
local result, _, code = os.execute(cmd)
if result and code == 0 then
ngx.log(ngx.INFO, "result -> ", result);
2022-02-08 00:25:20 +08:00
upyun_upload.upload(converted_audio_file)
2021-12-30 23:00:43 +08:00
else
ngx.status = 500
ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*")
end
2021-12-30 22:22:15 +08:00
end
2021-12-30 22:04:57 +08:00
2021-12-30 22:22:15 +08:00
convert();