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

const {readFile, print} = require('@mediafish/flv');
const buf = fs.readFileSync('test.flv');
const [offset, flv] = readFile(buf, offset);
print(flv);
const {readVideo, readAudio, print} = require('@mediafish/flv');
const [offset, video] = readVideo(buf1, offset);
print(video);
const [offset, audio] = readAudio(buf2, offset);
print(audio);
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);
const byteLength = writeData(flv, null, 0);
const buffer = Buffer.alloc(byteLength);
writeData(flv, buffer, 0);
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
});
const videoLength = writeData(video, null, 0);
const audioLength = writeData(audio, null, 0);
const buffer = Buffer.alloc(videoLength + audioLength);
let offset = 0;
offset = writeData(video, buffer, offset);
offset = writeData(audio, buffer, offset);
Read FLV file from the buffer
| 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 |
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) |
Read video data from the buffer
| 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 |
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) |
Read audio data from the buffer
| 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 |
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) |
Write data to the buffer
| 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 |
An integer to indicate the position from which the next data should be read
Updates the option values
| Name |
Type |
Required |
Default |
Description |
| obj |
Object |
Yes |
{} |
An object holding option values which will be used to overwrite the internal option values. |
| 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. |
Retrieves the current option values
A cloned object containing the current option values
This section describes the structure of the data that can be read / written using readFile/readAudio/readVideo
| Property |
Type |
Description |
header |
FLVHeader |
An instance of FLVHeader |
body |
[FLVTag] |
An array of FLVTag |
| Property |
Type |
Description |
version |
number |
FLV version |
hasAudio |
boolean |
Audio tags are present |
hasVideo |
boolean |
Video tags are present |
| 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 |
| 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. |
| Property |
Type |
Description |
packetType |
enum AAC.PacketType |
AAC packet type |
| 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. |
data |
Buffer or Uint8Array |
Codec specific data. |
| Property |
Type |
Description |
packetType |
enum AVC.PacketType |
AVC packet type |
compositionTimeOffset |
number |
Composition time offset |