从快递到烧瓶发送一组大缓冲区

时间:2018-10-15 17:50:04

标签: python express flask encoding multer

我正尝试先从网络浏览器客户端发送一组.mp4文件,然后进行表达,然后发送至flask。这段代码看起来很简单,但是要花很长时间才能理解,因为互联网上没有人发布过有关如何处理大文件(.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的快递服务器使用“ multer”库接收发布请求。数据作为缓冲区存储在内存中。

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 flask服务器中,.mp4文件将作为缓冲区接收并写入.mp4文件。但是,.mp4文件无法通过ffmpeg“未知的EBML文档类型'(无)'0/0 0_video.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);

0 个答案:

没有答案