2

I have the following code to download a specific part (range) of a video using 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)
})
})

the problem is when I set 'range' header to 'range': 'bytes=0-anyValue' the downloaded video can be played normally, but when I set 'range' to 'range': 'bytes=[anyValue>0]-anyValue' the downloaded video is corrupted and can't be played.

when 'range': 'bytes=0-anyValue' incoming response headers are:

{

'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'

}

and the downloaded video is playable

but when 'range': 'bytes=[anyValue>0]-anyValue incoming response headers are

{

'content-length': '443890',

'content-range': 'bytes 555-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': 'tx00000000000000e8c5225-006020613d-1afef1a-default',

'content-type': 'application/octet-stream',

date: 'Sun, 07 Feb 2021 21:53:01 GMT',

connection: 'close'

}

and the downloaded video is CORRUPTED and NOT playable

What I'm I doing wrong?

and How to achieve my goal properly?

Thanks in advance.

6
  • So, wouldn't you need at least the first handful of bytes that include the video stream's header information? Commented Feb 8, 2021 at 22:49
  • 1
    @MarkStewart exactly, but I don't know how to download the first bytes and then merge them with the range I want to download originally. Commented Feb 9, 2021 at 19:23
  • Looks like Wikipedia at en.wikipedia.org/wiki/MPEG-4_Part_14#Data_streams says that one piece of metadata, the "moov atom" that contains information on length and location of video data can be at the beginning or end of the file! Might make it difficult. Commented Feb 9, 2021 at 22:44
  • @MarkStewart So, what do you think I should do as a solution? Commented Feb 11, 2021 at 21:43
  • @MarkStewart let's say I got the metadata using ffmpeg or any other method, How do I download a specific PLAYABLE range of that video? Commented Feb 11, 2021 at 22:06

1 Answer 1

2

you can do just what you asked using ffmpeg command line tool like this:

ffmpeg -ss [start timestamp] -i [video path or url] -t [duation] [outputname.mp4] or other format

in your case, in nodejs you should use fluent-ffmpeg module:

const ffmpeg = require('fluent-ffmpeg')
const url = 'www.example.com/video.mp4'

ffmpeg(url).seekInput(30)      //cut from the first 30th second
        .duration(10)          //duration I want to cut 
        .output('video.mp4)    //output video name
        .run()                 //run the process

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.