Home

Awesome

node-video-lib

build status test coverage npm version license

Node.js Video Library / MP4 & FLV parser / MP4 builder / HLS muxer

Limitations

This library works only with MP4 and FLV video files encoded using H.264/H.265 video codecs and AAC audio codec.

Installation

$ npm install node-video-lib

Usage

Parse video file

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

let fd = fs.openSync('/path/to/file', 'r');
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);
}

Create MPEG-TS chunks

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

let fd = fs.openSync('/path/to/file', 'r');
try {
    let movie = VideoLib.MovieParser.parse(fd);
    let fragmentList = VideoLib.FragmentListBuilder.build(movie, 5);
    for (let i = 0; i < fragmentList.count(); i++) {
        let fragment = fragmentList.get(i);
        let sampleBuffers = VideoLib.FragmentReader.readSamples(fragment, fd);
        let buffer = VideoLib.HLSPacketizer.packetize(fragment, sampleBuffers);
        // Now buffer contains MPEG-TS chunk
    }
} catch (ex) {
    console.error('Error:', ex);
} finally {
    fs.closeSync(fd);
}

Build MP4 file

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

let fd = fs.openSync('/path/to/file', 'r');
try {
    let movie = VideoLib.MovieParser.parse(fd);
    let fw = fs.openSync('/path/to/output.mp4', 'w');
    try {
        VideoLib.MP4Builder.build(movie, fd, fw);
    } catch (ex) {
        console.error('Error:', ex);
    } finally {
        fs.closeSync(fw);
    }
} catch (ex) {
    console.error('Error:', ex);
} finally {
    fs.closeSync(fd);
}

Create index file

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

let fd = fs.openSync('/path/to/file', 'r');
try {
    let movie = VideoLib.MovieParser.parse(fd);
    let fragmentList = VideoLib.FragmentListBuilder.build(movie, 5);
    console.log('Duration:', fragmentList.relativeDuration());
    let fdi = fs.openSync('/path/to/index.idx', 'w');
    try {
        VideoLib.FragmentListIndexer.index(fragmentList, fdi);
    } catch (ex) {
        console.error('Error:', ex);
    } finally {
        fs.closeSync(fdi);
    }
} catch (ex) {
    console.error('Error:', ex);
} finally {
    fs.closeSync(fd);
}

Create MPEG-TS chunks using index file

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

let fd = fs.openSync('/path/to/file', 'r');
let fdi = fs.openSync('/path/to/index.idx', 'r');
try {
    let fragmentList = VideoLib.FragmentListIndexer.read(fdi);
    console.log('Duration:', fragmentList.relativeDuration());
    for (let i = 0; i < fragmentList.count(); i++) {
        let fragment = fragmentList.get(i);
        let sampleBuffers = VideoLib.FragmentReader.readSamples(fragment, fd);
        let buffer = VideoLib.HLSPacketizer.packetize(fragment, sampleBuffers);
        // Now buffer contains MPEG-TS chunk
    }
} catch (ex) {
    console.error('Error:', ex);
} finally {
    fs.closeSync(fd);
    fs.closeSync(fdi);
}

Classes

MovieParser

A tool for parsing video files (MP4 or FLV).

const MovieParser = require('node-video-lib').MovieParser

Methods:

MP4Parser

A tool for parsing MP4 video files.

const MP4Parser = require('node-video-lib').MP4Parser

Methods:

FLVParser

A tool for parsing FLV video files.

const FLVParser = require('node-video-lib').FLVParser

Methods:

MP4Builder

A tool for building MP4 video files.

const MP4Builder = require('node-video-lib').MP4Builder

Methods:

HLSPacketizer

A tool for creating MPEG-TS chunks.

const HLSPacketizer = require('node-video-lib').HLSPacketizer

Methods:

FragmentListBuilder

A tool for splitting the movie into a list of fragments.

const FragmentListBuilder = require('node-video-lib').FragmentListBuilder

Methods:

FragmentListIndexer

A tool to work with index files.

const FragmentListIndexer = require('node-video-lib').FragmentListIndexer

Methods:

FragmentReader

A tool for reading samples data of the given movie fragment.

const FragmentReader = require('node-video-lib').FragmentReader

Methods:

Movie

A movie class

const Movie = require('node-video-lib').Movie

Properties:

Methods:

FragmentList

A list of movie fragments class.

const FragmentList = require('node-video-lib').FragmentList

Properties:

Methods:

Fragment

A movie fragment class

const Fragment = require('node-video-lib').Fragment

Properties:

Methods:

Track

A general track class

const Track = require('node-video-lib').Track

Properties:

Methods:

AudioTrack

An audio track class. Extends the general track class

const AudioTrack = require('node-video-lib').AudioTrack

Properties:

VideoTrack

A video track class. Extends the general track class

const VideoTrack = require('node-video-lib').VideoTrack

Properties:

Methods:

Sample

A general video sample class

const Sample = require('node-video-lib').Sample

Properties:

Methods:

AudioSample

An audio sample class. Extends the general sample class

const AudioSample = require('node-video-lib').AudioSample

VideoSample

A video sample class. Extends the general sample class

const VideoSample = require('node-video-lib').VideoSample

Properties: