我正在尝试先从web浏览器客户端发送一组.mp4文件,然后发送到express,再发送到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处的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 flask服务器中,.mp4文件作为缓冲区接收并写入.mp4文件。但是,ffmpeg“Unknown EBML doctype'(none)'0/0无法播放.mp4文件_视频.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);
目前没有回答
相关问题 更多 >
编程相关推荐