const{writeData,type: {Video,AVC, Audio,AAC, FLVFile, FLVHeader, FLVTag}}=require('@mediafish/flv');constvideo=newAVC({frameType: Video.FrameType.keyframe,codec: Video.Codec.AVC,packetType: AVC.PacketType.NALU,compositionTimeOffset: 0,data: buf1});constaudio=newAAC({format: Audio.SoundFormat.AAC,sampleRate: Audio.SampleRate._44kHz,size: Audio.SampleLength._16Bit,isStereo: true,packetType: AAC.PacketType.Raw,data: buf2});constheader=newFLVHeader({version: 1,hasAudio: true,hasVideo: true});consttags=[newFLVTag({type: FLVTag.TagType.audio,timestamp: 0,data: audio}),newFLVTag({type: FLVTag.TagType.video,timestamp: 0,data: video})];constflv=newFLVFile(header,tags);// First, pass null instead of a buffer to detect how many bytes are neededconstbyteLength=writeData(flv,null,0);// And then alloc a buffconstbuffer=Buffer.alloc(byteLength);// Finally, write the data actually to the bufferwriteData(flv,buffer,0);
Example of writing Video and Audio
const{writeData,type: {Video,AVC, Audio,AAC}}=require('@mediafish/flv');constvideo=newAVC({frameType: Video.FrameType.keyframe,codec: Video.Codec.AVC,packetType: AVC.PacketType.NALU,compositionTimeOffset: 0,data: buf1});constaudio=newAAC({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 neededconstvideoLength=writeData(video,null,0);constaudioLength=writeData(audio,null,0);// And then alloc a buffconstbuffer=Buffer.alloc(videoLength+audioLength);// Finally, write the data actually to the bufferletoffset=0;offset=writeData(video,buffer,offset);offset=writeData(audio,buffer,offset);
API
readFile(buffer, offset)
Read FLV file from the buffer
params
Name
Type
Required
Default
Description
buffer
Buffer or Uint8Array
Yes
N/A
The buffer from which the data is read
offset
number
Yes
N/A
An integer to specify the position within the buffer
return value
An array containing the following pair of values
Index
Type
Description
[0]
number
An integer to indicate the position from which the next data should be read
[1]
FLVFile
The read data (See Data format)
readVideo(buffer, offset, length)
Read video data from the buffer
params
Name
Type
Required
Default
Description
buffer
Buffer or Uint8Array
Yes
N/A
The buffer from which the data is read
offset
number
Yes
N/A
An integer to specify the position within the buffer
length
number
Yes
N/A
An integer to specify how many bytes to read
return value
An array containing the following pair of values
Index
Type
Description
[0]
number
An integer to indicate the position from which the next data should be read
[1]
AVC
The read data (See Data format)
readAudio(buffer, offset)
Read audio data from the buffer
params
Name
Type
Required
Default
Description
buffer
Buffer or Uint8Array
Yes
N/A
The buffer from which the data is read
offset
number
Yes
N/A
An integer to specify the position within the buffer
length
number
Yes
N/A
An integer to specify how many bytes to read
return value
An array containing the following pair of values
Index
Type
Description
[0]
number
An integer to indicate the position from which the next data should be read
[1]
AAC
The read data (See Data format)
writeData(data, buffer, offset)
Write data to the buffer
params
Name
Type
Required
Default
Description
data
AVC/AAC/FLVHeader/FLVTag/FLVFile
Yes
N/A
The data to be written to the buffer
buffer
Buffer
No
null
The buffer to which the data is written. If null, only the necessary buffer size is calculated
offset
number
Yes
N/A
An 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
Name
Type
Required
Default
Description
obj
Object
Yes
{}
An object holding option values which will be used to overwrite the internal option values.
supported options
Name
Type
Default
Description
strictMode
boolean
false
If 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
Property
Type
Description
header
FLVHeader
An instance of FLVHeader
body
[FLVTag]
An array of FLVTag
FLVHeader
Property
Type
Description
version
number
FLV version
hasAudio
boolean
Audio tags are present
hasVideo
boolean
Video tags are present
FLVTag
Property
Type
Description
type
enum FLVTag.TagType
Type of this tag
timestamp
number
Time in milliseconds at which the data in this tag applies
data
Audio or Video
An instance of Audio / Video
Audio
Property
Type
Description
format
enum Audio.SoundFormat
Type of this tag. Only AAC is supported.
sampleRate
enum Audio.SampleRate
sample rate
size
enum Audio.SampleLength
bits per sample
isStereo
boolean
mono / stereo
data
Buffer or Uint8Array
Format specific data.
AAC extends Audio
Property
Type
Description
packetType
enum AAC.PacketType
AAC packet type
Video
Property
Type
Description
frameType
enum Video.FrameType
Type of the frame included in this tag
codec
enum Video.Codec
Type of codec used to compress the frame. Only AVC is supported.