使用带缓冲区的节点视频库

问题描述

我正在尝试使用库 node-video-lib获取有关在服务器上收到的视频的信息, 但是在文档中,我看到了仅使用文件系统中的文件的示例,

这是文档中的示例:

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);
    }
});

我的问题:我怎样才能像示例一样获得 fd 编号,

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

来自文件缓冲区?

像这样:

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:',ex);
    } finally {
        fs.closeSync(fd);
    }
});
  });

在从 videoLib 获取信息之前,我必须将文件 - 我的视频写入我的文件系统吗?

解决方法

VideoLib.MovieParser.parse 方法接受文件处理程序或缓冲区参数:

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

因此您可以根据您提供的代码将 file.databuffer 传递给此方法,如下所示:

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);
    }
});