Siddharth Kumar
Siddharth Kumar

Reputation: 168

How to create a Video URL Blob in NodeJS?

Can you please help me in create a Video URL Blob in NodeJS?

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';

    xhr.onload = function() {
        var reader = new FileReader();

        reader.onloadend = function() {
            var byteCharacters = atob(reader.result.slice(reader.result.indexOf(',') + 1));

            var byteNumbers = new Array(byteCharacters.length);

            for (var i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);
            var blob = new Blob([ byteArray ], { type: 'video/ogg' });
            var url = URL.createObjectURL(blob);

            console.log(url);
        };

        reader.readAsDataURL(xhr.response);
    };

    xhr.open(
        'GET',
        '...Video URL...'
    );
    xhr.send();

Error Output:

  throw new Error('cannot read as File: ' + JSON.stringify(file));   
  Error: cannot read as File: undefined

I have used the packages XMLHttpRequest, URL, FileReader and Blob

Please Help, Thanks

Upvotes: 2

Views: 2339

Answers (2)

Test
Test

Reputation: 13

You can Use Azure Cloud for storing the videos and then us Azure BLOB

Upvotes: 1

theusaf
theusaf

Reputation: 1812

Node.JS does not have XMLHttpRequest, FileReader, Blob, etc. Instead, it uses the fs, http modules and other built-in classes, like Buffer

Trying to use these browser features on the server, even if using packages that try to emulate them, may not always work. In fact, many of those libraries which emulate those features do not include everything. For example, the xmlhttprequest node package does not use .response, but only the text response.

In Node.JS, there is no need for blob URLs (where would you even use that?), you just interact with the Buffers directly.

// making a http request to fetch something
const http = require("https");

const request = https.request({
  method: "GET",
  url: "https://example.com/video.ogg"
}, (res) => {
  const chunks = [];

  res.on("data", (chunk) => {
    chunks.push(chunk);
  });

  res.on("end", () => {
    // raw byte data
    const buffer = Buffer.concat(chunks);

    /*
     * Interact with Buffer here
     */

  });
});

request.end();

Upvotes: 1

Related Questions