[英]How to play a video file in browser after fetching from s3?
I want to play a video in browser after fetching it from s3 bucket.从 s3 存储桶中获取视频后,我想在浏览器中播放视频。 I am using aws's getObject API to get the file.
我正在使用 aws 的 getObject API 来获取文件。 The file's body which I am receiving from s3 has array buffer which I am unable to stream in browser.
我从 s3 接收到的文件主体具有我无法在浏览器中流式传输的数组缓冲区。
I have tried converting unit8Array into base64 encoded string to get the url.我尝试将 unit8Array 转换为 base64 编码的字符串以获取 url。
downloadVideo() {
var AWS_BUCKET_NAME = this.props.awsService.bucket;
var $this = this;
var bucket = new AWS.S3({params: {Bucket: AWS_BUCKET_NAME}});
bucket.getObject({Key: this.props.fileName},function(err,file){
if(err) {
console.log('err : ', err);
} else {
console.log('file : ', file);
var srcUrl = "data:video/mp4;base64," + $this.encode(file.Body);
$this.setState({fileSrc: srcUrl});
}
});
}
encode(data){
var str = data.reduce(function(a,b){ return a+String.fromCharCode(b) },'');
return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
}
However, for large video the url being formed are very large and video crashes after some time.但是,对于大型视频,形成的 url 非常大,一段时间后视频崩溃。 I am attaching screenshot of the file object received from s3 for reference.
我附上了从 s3 收到的文件对象的屏幕截图以供参考。
Is there a way to directly stream video without downloading ?有没有办法直接流式传输视频而不下载?
base64-encoding the video is very wasteful and inefficient as you have noticed.正如您所注意到的那样,对视频进行 base64 编码非常浪费且效率低下。 You could try something like this:
你可以尝试这样的事情:
var blob = new Blob([file.Body.buffer]);
var srcUrl = URL.createObjectURL(blob);
$this.setState({fileSrc: srcUrl});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.