fix: azure语音云转码

This commit is contained in:
ikechan8370 2023-04-23 15:26:15 +08:00
parent 2cafdcaa82
commit 1f50e1b7a5

View file

@ -1,6 +1,6 @@
// import Contactable, { core } from 'oicq' // import Contactable, { core } from 'oicq'
import querystring from 'querystring' import querystring from 'querystring'
import fetch, { File, FormData } from 'node-fetch' import fetch, { File, fileFromSync, FormData } from 'node-fetch'
import fs from 'fs' import fs from 'fs'
import os from 'os' import os from 'os'
import util from 'util' import util from 'util'
@ -36,11 +36,12 @@ async function uploadRecord (recordUrl) {
result = await getPttBuffer(recordUrl, Bot.config.ffmpeg_path) result = await getPttBuffer(recordUrl, Bot.config.ffmpeg_path)
} else if (Config.cloudTranscode) { } else if (Config.cloudTranscode) {
try { try {
if (Config.cloudMode === 'buffer' || Config.cloudMode === 'file') { if (Config.cloudMode === 'file') {
const formData = new FormData()
let buffer let buffer
if (!recordUrl.startsWith('http')) { if (!recordUrl.startsWith('http')) {
// 本地文件 // 本地文件
buffer = fs.readFileSync(recordUrl) formData.append('file', fileFromSync(recordUrl))
} else { } else {
let response = await fetch(recordUrl, { let response = await fetch(recordUrl, {
method: 'GET', method: 'GET',
@ -51,21 +52,18 @@ async function uploadRecord (recordUrl) {
const blob = await response.blob() const blob = await response.blob()
const arrayBuffer = await blob.arrayBuffer() const arrayBuffer = await blob.arrayBuffer()
buffer = Buffer.from(arrayBuffer) buffer = Buffer.from(arrayBuffer)
}
if (Config.cloudMode === 'file') {
const formData = new FormData()
formData.append('file', new File([buffer], 'audio.wav')) formData.append('file', new File([buffer], 'audio.wav'))
const resultres = await fetch(`${Config.cloudTranscode}/audio`, { }
method: 'POST', const resultres = await fetch(`${Config.cloudTranscode}/audio`, {
body: formData method: 'POST',
}) body: formData
let t = await resultres.text() })
try { let t = await resultres.text()
result = JSON.parse(t) try {
} catch (e) { result = JSON.parse(t)
logger.error(t) } catch (e) {
throw e logger.error(t)
} throw e
} }
} else { } else {
const resultres = await fetch(`${Config.cloudTranscode}/audio`, { const resultres = await fetch(`${Config.cloudTranscode}/audio`, {