node.jsnpmvideoreadfilevideo-library

use node-video-lib with buffer


I'm trying to use the libary node-video-lib and get information about the video received on the server, But in the documentation I see examples of using a file from the file system only,

this is the example from the documentation:

const fs = require('fs');
const VideoLib = require('node-video-lib');

fs.open('/path/to/file', 'r', function(err, fd) {
    try {
        let movie = VideoLib.MovieParser.parse(fd);
        // Work with movie
        console.log('Duration:', movie.relativeDuration());
    } catch (ex) {
        console.error('Error:', ex);
    } finally {
        fs.closeSync(fd);
    }
});

my question: How can I get the fd number like the example,

fs.open('/path/to/file', 'r', function(err, fd)

from file Buffer?

something like this:

const VideoLib = require('node-video-lib');

    app.post(route + '/upload', (req, response) => {
        let file = req.files.file;
        let buffer=file.data;
        let fd=?
    try {
        let movie = VideoLib.MovieParser.parse(fd);
        // Work with movie
        console.log('Duration:', movie.relativeDuration());
    } catch (ex) {
        console.error('Error:', ex);
    } finally {
        fs.closeSync(fd);
    }
});
  });

must I write the file - my video, to my files system before I get the information from videoLib?


Solution

  • The VideoLib.MovieParser.parse method accepts both file handler or Buffer parameter:

    https://github.com/gkozlenko/node-video-lib#movieparser

    So you can pass the file.data or buffer to this method according your provided code, something like this:

    app.post(route + '/upload', (req, response) => {
        let file = req.files.file;
        let buffer = file.data;
        try {
            let movie = VideoLib.MovieParser.parse(buffer);
            // Work with movie
            console.log('Duration:', movie.relativeDuration());
        } catch (ex) {
            console.error('Error:', ex);
        }
    });