ProductPromotion
Logo

Node.JS

made by https://0x3d.site

GitHub - gkozlenko/node-video-lib: Node.js Video Library / MP4 & FLV parser / MP4 builder / HLS muxer
Node.js Video Library / MP4 & FLV parser / MP4 builder / HLS muxer - gkozlenko/node-video-lib
Visit Site

GitHub - gkozlenko/node-video-lib: Node.js Video Library / MP4 & FLV parser / MP4 builder / HLS muxer

GitHub - gkozlenko/node-video-lib: Node.js Video Library / MP4 & FLV parser / MP4 builder / HLS muxer

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:

  • 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

Articles
to learn more about the nodejs concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about NodeJS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory