Numeric Production Mechanism

    node-video-lib

    2.2.1 • Public • Published

    node-video-lib

    Build Status npm Version Maintainability Test Coverage GitHub License Donate using PayPal Buy me a Coffee

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

    Create MPEG-TS chunks

    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);
            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');
    
    fs.open('/path/to/file', 'r', function(err, fd) {
        try {
            let movie = VideoLib.MovieParser.parse(fd);
            fs.open('/path/to/output.mp4', 'w', function(err, fw) {
                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');
    
    fs.open('/path/to/file', 'r', function(err, fd) {
        try {
            let movie = VideoLib.MovieParser.parse(fd);
            let fragmentList = VideoLib.FragmentListBuilder.build(movie, 5);
            console.log('Duration:', fragmentList.relativeDuration());
            fs.open('/path/to/index.idx', 'w', function(err, fdi) {
                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');
    
    fs.open('/path/to/file', 'r', function(err, fd) {
        fs.open('/path/to/index.idx', 'r', function(err, fdi) {
            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:

    • parse(source) - Parse video file
      • source <Integer>|<Buffer> - Source (File descriptor or Buffer)
      • Return: <Movie>

    MP4Parser

    A tool for parsing MP4 video files.

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

    Methods:

    • parse(source) - Parse MP4 file
      • source <Integer>|<Buffer> - Source (File descriptor or Buffer)
      • Return: <Movie>
    • check(buffer) - Check MP4 header
      • buffer <Buffer> - File header (first 8 bytes)
      • Return: <boolean>

    FLVParser

    A tool for parsing FLV video files.

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

    Methods:

    • parse(source) - Parse FLV file
      • source <Integer>|<Buffer> - Source (File descriptor or Buffer)
      • Return: <Movie>
    • check(buffer) - Check FLV header
      • buffer <Buffer> - File header (first 8 bytes)
      • Return: <boolean>

    MP4Builder

    A tool for building MP4 video files.

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

    Methods:

    • build(movie, source, fd) - Build MP4 file
      • movie <Movie> - Movie
      • source <Integer>|<Buffer> - Source (File descriptor or Buffer)
      • fd <Integer> - File descriptor

    HLSPacketizer

    A tool for creating MPEG-TS chunks.

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

    Methods:

    • packetize(fragment, sampleBuffers) - Create MPEG-TS chunk from movie fragment
      • fragment <Fragment> - Movie fragment
      • sampleBuffers <Array> - Array of buffers
      • Return: <Buffer>

    FragmentListBuilder

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

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

    Methods:

    • build(movie, fragmentDuration) - Split the movie to a list of fragments with an appropriate duration

    FragmentListIndexer

    A tool to work with index files.

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

    Methods:

    • index(fragmentList, fd) - Index fragment list
      • fragmentList <FragmentList> - Fragment list
      • fd <Integer> - File descriptor
    • read(fd) - Read fragment list from index

    FragmentReader

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

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

    Methods:

    • readSamples(fragment, source) - Read samples data
      • fragment <Fragment> - Movie fragment
      • source <Integer>|<Buffer> - Source (File descriptor or Buffer)
      • Return: <Array> Array of buffers

    Movie

    A movie class

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

    Properties:

    • duration <Integer> - Movie duration
    • timescale <Integer> - Movie timescale
    • tracks <Array> - List of movie tracks

    Methods:

    • relativeDuration() - Movie duration in seconds
      • Return: <Number>
    • resolution() - Video resolution
      • Return: <String>
    • size() - Samples size
      • Return: <Integer>
    • addTrack(track) - Add a track to the tracks list
      • track <Track> - Track
    • videoTrack() - Get the first video track
      • Return: <VideoTrack>
    • audioTrack() - Get the first audio track
      • Return: <AudioTrack>
    • samples() - Get a list of movie samples ordered by relative timestamp
      • Return: <Array>
    • ensureDuration() - Calculate and set duration based on the track durations (only if duration is zero)
      • Return: <Number>

    FragmentList

    A list of movie fragments class.

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

    Properties:

    • fragmentDuration <Integer> - Target fragment duration
    • duration <Integer> - Movie duration
    • timescale <Integer> - Movie timescale
    • video <Object> - Video info
      • timescale <Integer> - Video timescale
      • codec <String> - Codec string
      • extraData <Buffer> - Video codec information
      • size <Integer> - Video samples size
      • width <Integer> - Video width
      • height <Integer> - Video height
    • audio <Object> - Audio info
      • timescale <Integer> - Audio timescale
      • codec <String> - Codec string
      • extraData <Buffer> - Audio codec information
      • size <Integer> - Audio samples size

    Methods:

    • relativeDuration() - Movie duration in seconds
      • Return: <Number>
    • count() - Fragments count
      • Return: <Integer>
    • size() - Samples size
      • Return: <Integer>
    • get(index) - Get fragment by index

    Fragment

    A movie fragment class

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

    Properties:

    • timestamp <Integer> - Fragment timestamp
    • duration <Integer> - Fragment duration
    • timescale <Integer> - Fragment timescale
    • videoExtraData <Buffer> - Video codec information
    • audioExtraData <Buffer> - Audio codec information
    • samples <Array> - List of fragment samples

    Methods:

    • relativeTimestamp() - Fragment timestamp in seconds
      • Return: <Number>
    • relativeDuration() - Fragment duration in seconds
      • Return: <Number>
    • hasVideo() - Fragment has a video track
      • Return: <Boolean>
    • hasAudio() - Fragment has an audio track
      • Return: <Boolean>

    Track

    A general track class

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

    Properties:

    • duration <Integer> - Track duration
    • timescale <Integer> - Track timescale
    • codec <String> - Codec string
    • extraData <Buffer> - Codec information
    • samples <Array> - List of track samples

    Methods:

    • relativeDuration() - Track duration in seconds
      • Return: <Number>
    • ensureDuration() - Calculate and set duration based on the sample durations (only if duration is zero)
      • Return: <Number>
    • size() - Samples size
      • Return: <Integer>

    AudioTrack

    An audio track class. Extends the general track class

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

    Properties:

    • channels <Integer> - Number of audio channels
    • sampleRate <Integer> - Audio sample rate
    • sampleSize <Integer> - Audio sample size

    VideoTrack

    A video track class. Extends the general track class

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

    Properties:

    • width <Integer> - Video width
    • height <Integer> - Video height

    Methods:

    • resolution() - Video resolution
      • Return: <String>

    Sample

    A general video sample class

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

    Properties:

    • timestamp <Integer> - Sample timestamp
    • timescale <Integer> - Sample timescale
    • size <Integer> - Sample size
    • offset <Integer> - Sample offset in the file

    Methods:

    • relativeTimestamp() - Sample timestamp in seconds
      • Return: <Number>

    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:

    • compositionOffset <Integer> - Composition offset
    • keyframe <Boolean> - Keyframe flag

    Install

    npm i node-video-lib

    DownloadsWeekly Downloads

    275

    Version

    2.2.1

    License

    MIT

    Unpacked Size

    125 kB

    Total Files

    74

    Last publish

    Collaborators

    • avatar