首页 > 解决方案 > 将一组大型缓冲区从 express 发送到烧瓶

问题描述

我正在尝试首先从 Web 浏览器客户端发送一组 .mp4 文件,然后是表达,然后是烧瓶。这段代码可能看起来很简单,但实际上花了几天时间才走到这一步,因为互联网上没有人发布过如何处理大文件(.mp4 视频文件)的示例,老实说,有很多使用这些库的不一致。

但是,我认为这可能是从 express 到 Flask 的编码错误。实际数据似乎正在传输,但 ffmpeg 无法将接收到的 .mp4 文件识别为有效的视频文件。

从客户端,我发送一个 XHR 请求,如下所示:

var formData = new FormData();
for(var x=0; x< datas.videoDatas.length; x++){
    // data.videoDatas[x] is a blob of a .mp4 file. it can be played 
    formData.append(x+"_video.mp4",datas.videoDatas[x]); 

}
var xhr = new XMLHttpRequest();
xhr.open("POST", 'http://localhost:3000/uploadVid', true);
xhr.onreadystatechange = function() {
    if(this.readyState == XMLHttpRequest.DONE && this.status == 200) {
         console.log(xhr.response);
    }
}
xhr.onload=console.log;
xhr.send(formData);

端口 3000 的 express 服务器使用“multer”库接收 post 请求。数据作为缓冲区存储到内存中。

var express = require('express');
var multer=require("multer");
var request=require("request")
var app = express();
var storage = multer.memoryStorage();
const upload = multer({
    storage: storage
}).any();
app.post('/uploadVid',function(req,res){
    var originalResponse=res;
    console.log("begin upload");
    upload(req, res, function(err) {
            console.log("begin upload to flask server");
            var requestStruct={
              url: 'http://localhost:3001/receiveUpload2?numFiles='+req.files.length,
              method: 'POST',
              form: {
              }
            }
            for(var x=0; x < req.files.length; x++){
                var testBuffer=req.files[x].buffer; // should be a buffer of the .mp4 file
                //testBuffer = new Buffer(10);
                //testBuffer=testBuffer.toString();
                requestStruct.form[x+'_customBufferFile']= {
                      value: testBuffer,
                      options: {
                    filename: req.files[x].fieldname
                      }
                    }
            }
            request(requestStruct, function(err,res,body){
                originalResponse.send(body)
            });
    });
});
http.createServer(app).listen(3000)

在 python 烧瓶服务器中,.mp4 文件作为缓冲区接收并写入 .mp4 文件。但是,ffmpeg “Unknown EBML doctype '(none)'0/0 0_video.mp4: End of file”无法播放 .mp4 文件

from flask import Flask
from flask import request; 
app = Flask(__name__);
@app.route('/receiveUpload2', methods=["POST"])
def receiveUpload2():
    print("uploaded received....");
    numFiles=int(request.args.get("numFiles").encode('ascii', 'ignore'));
    print("numFiles",int(numFiles));
    for x in range(0,numFiles):
        name=request.form.getlist(str(x)+'_customBufferFile[options][filename]');
        name=name[0];
        print("writing",name);
        filebytes=request.form.getlist(str(x)+'_customBufferFile[value]');
        filebytes=filebytes[0];
        #print(filebytes);
        newFileByteArray = bytearray(filebytes,'utf8')
        with open("./uploads/"+name,"wb") as output:
            output.write(newFileByteArray);

app.run(host="localhost", port=3001);

标签: pythonexpressflaskencodingmulter

解决方案


推荐阅读