【发布时间】:2022-11-04 14:41:51
【问题描述】:
所以本质上,我的 api 调用所做的是 1) 使用 parse multipart 接收视频数据,2) 使用 ffmpeg 将该视频数据转换为真实的 mp4 文件,然后 3) 应该将视频数据发送回客户端响应体。
第 1 步和第 2 步完美运行 - 这是我坚持的第三步。
api 调用创建 Out.mp4 文件,但是当我尝试使用 createReadStream 读取其信息时,不会填充块数组,并返回 null context.res 正文。
请让我知道我做错了什么以及如何正确传回视频信息,以便能够在客户端将视频信息转换回可播放的 mp4 文件。
另外,如果您有任何问题或我可以澄清的事情,请联系我。
这是api调用index.js文件
const fs = require("fs");
module.exports=async function(context, req){
try{
//Get the input file setup
context.log("Javascript HTTP trigger function processed a request.");
var bodyBuffer=Buffer.from(req.body);
var boundary=multipart.getBoundary(req.headers['content-type']);
var parts=multipart.Parse(bodyBuffer, boundary);
var temp = "C:/home/site/wwwroot/In.mp4";
fs.writeFileSync(temp, Buffer(parts[0].data));
//Actually execute the ffmpeg script
var execLineBuilder= "C:/home/site/wwwroot/ffmpeg-5.1.2-essentials_build/bin/ffmpeg.exe -i C:/home/site/wwwroot/In.mp4 C:/home/site/wwwroot/Out.mp4"
var execSync = require('child_process').execSync;
//Executing the script
execSync(execLineBuilder)
//EVERYTHING WORKS UP UNTIL HERE (chunks array seems to be empty, even though outputting chunk to a file populates
//That file with data)
//Storing the chunks of the output mp4 into chunks array
execSync.on('exit', ()=>{
chunks = [];
const myPromise = new Promise((resolve, reject) => {
var readStream = fs.createReadStream("C:/home/site/wwwroot/Out.mp4");
readStream.on('data', (chunk)=> {
chunks.push(chunk);
resolve("foo");
});
})
})
myPromise.then(()=>{
context.res={
status:200,
body:chunks
}
})
}catch (e){
context.res={
status:500,
body:e
}
}
}```
【问题讨论】:
标签: node.js video promise azure-functions chunks