如何从nodejs中的远程视频url下载部分内容?

问题描述

我有以下代码可以使用 nodejs http.get() 下载视频的特定部分(范围),

const fileUrl = 'https://www.example.com/path/to/video.mp4'
const fs = require('fs')
const http = require('https')
const fileName = 'video.mp4'
const options = {
hostname: 'https://www.example.com',path: '/path/to/video.mp4',method: 'GET',headers: {
'range': 'bytes=0-444444',//the size I'm requesting is the first 444.4 kB of the video
}

const req = http.get(options)
req.on('response',(res) => {
console.log(res.headers) //just to see headers
})

req.on('response',(res) => {
let file = fs.createWriteStream(fileName)
let size
res.on('data',(chunk) => {
file.write(chunk)
size = fs.statSync(file.path).size
console.log(size)
})
})

问题是当我将 'range' 标头设置为 'range': 'bytes=0-anyValue' 时,下载的视频可以正常播放,但是当我将 'range' 设置为 'range': 'bytes=[anyValue>0]-anyValue' 时,下载的视频已损坏并且无法播放。

'range': 'bytes=0-anyValue' 传入响应标头为

{

'content-length': '444445','content-range': 'bytes 0-444444/17449469','accept-ranges': 'bytes','last-modified': 'Tue,07 May 2019 11:45:38 GMT',etag: '"f13a255d30ef81d2abf8ba2e4fefc2fd-1"','x-amz-meta-s3cmd-attrs': 'md5:4e3127acff74ac20b52e1680a5e0779d','cache-control': 'public,max-age=2592000','content-disposition': 'attachment; filename="Rim.Of.The.World.2019.720p.Trailer.mp4";','content-encoding': 'System.Text.UTF8Encoding','x-amz-request-id': 'tx0000000000000001bfccc-00602060b1-1b3f92b-default','content-type': 'application/octet-stream',date: 'Sun,07 Feb 2021 21:50:41 GMT',connection: 'close'

}

下载的视频可以播放

但是当 'range': 'bytes=[anyValue>0]-anyValue 传入响应标头为

{

'content-length': '443890','content-range': 'bytes 555-444444/17449469','x-amz-request-id': 'tx00000000000000e8c5225-006020613d-1afef1a-default',07 Feb 2021 21:53:01 GMT',connection: 'close'

}

下载的视频已损坏且无法播放

我做错了什么?

以及如何正确实现我的目标?

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)