0.2.2 • Published 1 year ago

@mediafish/flv v0.2.2

Weekly downloads
13
License
MIT
Repository
github
Last release
1 year ago

Build Status Coverage Status Dependency Status Development Dependency Status Known Vulnerabilities npm Downloads XO code style

flv

A library to read/write Flash Video file format (only supports AAC/AVC)

Install

NPM

Usage

Example of reading FLV file

const {readFile, print} = require('@mediafish/flv');

const buf = fs.readFileSync('test.flv');
const [offset, flv] = readFile(buf, offset);
print(flv);
/*
FLVFile {
  FLVHeader: {
    version: 1,
    hasAudio: true,
    hasVideo: true
  },
  body: [
    FLVTag: {
      timestamp: 0,
      AVC: {
        frameType: 'keyframe',
        codec: 'AVC',
        packetType: 'NALU',
        data: <Buffer length=1024 >
      }
    },
    FLVTag: {
      timestamp: 0,
      AAC: {
        format: 'AAC',
        sampleRate: '44kHz',
        size: '16Bit',
        isStereo: true,
        packetType: 'raw',
        data: <Buffer length=1024 >
      }
    },
    ...
  ]
}
*/

Example of reading Video and Audio

const {readVideo, readAudio, print} = require('@mediafish/flv');

const [offset, video] = readVideo(buf1, offset);
print(video);
/*
AVC: {
  frameType: 'keyframe',
  codec: 'AVC',
  packetType: 'NALU',
  data: <Buffer length=1024 >
}
*/

const [offset, audio] = readAudio(buf2, offset);
print(audio);
/*
AAC: {
  format: 'AAC',
  sampleRate: '44kHz',
  size: '16Bit',
  isStereo: true,
  packetType: 'raw',
  data: <Buffer length=1024 >
}
*/

Example of writing FLV

const {writeData, type: {Video, AVC, Audio, AAC, FLVFile, FLVHeader, FLVTag}} = require('@mediafish/flv');

const video = new AVC({
  frameType: Video.FrameType.keyframe,
  codec: Video.Codec.AVC,
  packetType: AVC.PacketType.NALU,
  compositionTimeOffset: 0,
  data: buf1
});


const audio = new AAC({
  format: Audio.SoundFormat.AAC,
  sampleRate: Audio.SampleRate._44kHz,
  size: Audio.SampleLength._16Bit,
  isStereo: true,
  packetType: AAC.PacketType.Raw,
  data: buf2
});

const header = new FLVHeader({version: 1, hasAudio: true, hasVideo: true});

const tags = [
  new FLVTag({type: FLVTag.TagType.audio, timestamp: 0, data: audio}),
  new FLVTag({type: FLVTag.TagType.video, timestamp: 0, data: video})
];

const flv = new FLVFile(header, tags);

// First, pass null instead of a buffer to detect how many bytes are needed
const byteLength = writeData(flv, null, 0);
// And then alloc a buff
const buffer = Buffer.alloc(byteLength);
// Finally, write the data actually to the buffer
writeData(flv, buffer, 0);

Example of writing Video and Audio

const {writeData, type: {Video, AVC, Audio, AAC}} = require('@mediafish/flv');

const video = new AVC({
  frameType: Video.FrameType.keyframe,
  codec: Video.Codec.AVC,
  packetType: AVC.PacketType.NALU,
  compositionTimeOffset: 0,
  data: buf1
});


const audio = new AAC({
  format: Audio.SoundFormat.AAC,
  sampleRate: Audio.SampleRate._44kHz,
  size: Audio.SampleLength._16Bit,
  isStereo: true,
  packetType: AAC.PacketType.Raw,
  data: buf2
});

// First, pass null instead of a buffer to detect how many bytes are needed
const videoLength = writeData(video, null, 0);
const audioLength = writeData(audio, null, 0);
// And then alloc a buff
const buffer = Buffer.alloc(videoLength + audioLength);
// Finally, write the data actually to the buffer
let offset = 0;
offset = writeData(video, buffer, offset);
offset = writeData(audio, buffer, offset);

API

readFile(buffer, offset)

Read FLV file from the buffer

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the data is read
offsetnumberYesN/AAn integer to specify the position within the buffer

return value

An array containing the following pair of values

IndexTypeDescription
0numberAn integer to indicate the position from which the next data should be read
1FLVFileThe read data (See Data format)

readVideo(buffer, offset, length)

Read video data from the buffer

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the data is read
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberYesN/AAn integer to specify how many bytes to read

return value

An array containing the following pair of values

IndexTypeDescription
0numberAn integer to indicate the position from which the next data should be read
1AVCThe read data (See Data format)

readAudio(buffer, offset)

Read audio data from the buffer

params

NameTypeRequiredDefaultDescription
bufferBuffer or Uint8ArrayYesN/AThe buffer from which the data is read
offsetnumberYesN/AAn integer to specify the position within the buffer
lengthnumberYesN/AAn integer to specify how many bytes to read

return value

An array containing the following pair of values

IndexTypeDescription
0numberAn integer to indicate the position from which the next data should be read
1AACThe read data (See Data format)

writeData(data, buffer, offset)

Write data to the buffer

params

NameTypeRequiredDefaultDescription
dataAVC/AAC/FLVHeader/FLVTag/FLVFileYesN/AThe data to be written to the buffer
bufferBufferNonullThe buffer to which the data is written. If null, only the necessary buffer size is calculated
offsetnumberYesN/AAn integer to specify the position within the buffer

return value

An integer to indicate the position from which the next data should be read

setOptions(obj)

Updates the option values

params

NameTypeRequiredDefaultDescription
objObjectYes{}An object holding option values which will be used to overwrite the internal option values.
supported options
NameTypeDefaultDescription
strictModebooleanfalseIf true, the function throws an error when the method invocations failed. If false, the function just logs the error and continues to run.

getOptions()

Retrieves the current option values

return value

A cloned object containing the current option values

Data format

This section describes the structure of the data that can be read / written using readFile/readAudio/readVideo

FLVFile

PropertyTypeDescription
headerFLVHeaderAn instance of FLVHeader
bodyFLVTagAn array of FLVTag

FLVHeader

PropertyTypeDescription
versionnumberFLV version
hasAudiobooleanAudio tags are present
hasVideobooleanVideo tags are present

FLVTag

PropertyTypeDescription
typeenum FLVTag.TagTypeType of this tag
timestampnumberTime in milliseconds at which the data in this tag applies
dataAudio or VideoAn instance of Audio / Video

Audio

PropertyTypeDescription
formatenum Audio.SoundFormatType of this tag. Only AAC is supported.
sampleRateenum Audio.SampleRatesample rate
sizeenum Audio.SampleLengthbits per sample
isStereobooleanmono / stereo
dataBuffer or Uint8ArrayFormat specific data.

AAC extends Audio

PropertyTypeDescription
packetTypeenum AAC.PacketTypeAAC packet type

Video

PropertyTypeDescription
frameTypeenum Video.FrameTypeType of the frame included in this tag
codecenum Video.CodecType of codec used to compress the frame. Only AVC is supported.
dataBuffer or Uint8ArrayCodec specific data.

AVC extends Video

PropertyTypeDescription
packetTypeenum AVC.PacketTypeAVC packet type
compositionTimeOffsetnumberComposition time offset
0.2.1

1 year ago

0.2.2

1 year ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago