【问题标题】:Node JS/azure functions passing video information back from api callNode JS/azure 函数从 api 调用传回视频信息
【发布时间】: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


    【解决方案1】:
    • 你可以使用一个名为azure-function-express 的 npm 包,这个包基本上会将你的 azure 函数转换为 express

    • 这样就可以直接读取自己保存的mp3文件,直接发送。

    const  createHandler = require("azure-function-express").createHandler;
    const  express = require("express");
    const  fs = require('fs');
    const  app = express();
    
    app.get("/api/HttpTrigger1", (req, res) => {
        res.writeHead(200, {'Content-Type':  'video/mp4'});
    
        let  open = fs.createReadStream('./test.mp3');
        res.send(open);
    });
    
    

    这样您就可以共享视频,同时运行ffmpeg 也可能很简单

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 2014-05-05
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      相关资源
      最近更新 更多