我正在努力实现以下目标:
- 通过 API 从另一台服务器下载视频
- 将视频保存到磁盘以在
<video>
中提供它标签
但我无助地陷入困境 - 我尝试下载的视频始终无效(并且大小错误)。
这是我的服务器方法:
getVideo: function() {
var fs = Npm.require('fs');
var response = HTTP.get('http://webserver.com/getvideo');
fs.writeFile('file.mp4',response.content, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
}
我尝试过的其他事情并呈现基本相同的结果:
使用createWriteStream:
var file = fs.createWriteStream('file.mp4');
var response = HTTP.get('http://webserver.com/getvideo');
response.content.pipe(file);
使用 Buffer():
var buffer = new Buffer(response.content)
fs.writeFile('file.mp4', buffer)
并且还具有不同的编码:base64, binary
网络服务器(用 Flask 编写)响应如下:
{
content: ........00+ilst\u0000#�too\u001bdata\u0000\u0000\u0000\u0000Lavf52.48.0',
headers:
{ 'content-disposition': 'attachment; filename=file.mp4',
'content-length': '632310',
'content-type': 'video/mp4',
'last-modified': 'Fri, 08 Jan 2016 00:45:49 GMT',
'cache-control': 'public, max-age=43200',
expires: 'Fri, 08 Jan 2016 20:21:08 GMT',
etag: '"flask-1452213949.65-632310-220532295"',
server: 'Werkzeug/0.11.3 Python/2.7.11',
date: 'Fri, 08 Jan 2016 08:21:08 GMT' },
data: null }
像curl
这样的工具或网络浏览器虽然可以正确下载(并放入磁盘)文件,但我缺少什么?
最佳答案
我设法让它发挥作用。
据我所知,内容已被编码,因此我需要删除该编码。
如[meteor][0.6.*] With meteorjs, how to download file with Meteor.http?所述在最底部:
The encoding option of meteor HTTP is a sub option of npmRequestOptions, so HTTP.get(url, {npmRequestOptions: {encoding:null}} which finally allowed me to get binary files like jpg – Bob Siefkes Nov 7 '15 at 18:48
感谢 #meteor 上的 lanmower 对我的帮助。
关于node.js - Meteor:通过 API 下载视频文件并将其保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34672506/