python - 将一组大型缓冲区从 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);
解决方案
推荐阅读
- javascript - 如何在 Cloudant 的 _user 数据库中分配角色
- ios - 缓存一个网页(Typeform),离线时在WKWebView中打开
- python - 为什么我的变量导入会导致代码冲突?
- java - Android 11 错误:应用程序因 java.lang.SecurityException 崩溃:uid xxxxx 无法显式添加类型为:package.name 的帐户
- node.js - 嵌套参考使用猫鼬和 Nodejs 填充
- load-balancing - 负载均衡器的负载均衡
- php - 419 会话在设置“SESSION_DOMAIN”环境值后过期
- apache-kafka - 是否建议在消费者读取 kstream 消息密钥或消息后更改它以存储内部状态
- r - 在 ggplot/ggplotly 中使用 facet_wrap 时如何防止 y 轴挤压标签?
- javascript - 如何获得具有预定义值的顶级父级的所需类别树