Flash Video format support for legacy applications. The FLV module provides FLV transmuxing and inspection capabilities. Note that this module is in maintenance mode and will not receive further major development.
Transmuxer class for converting MPEG-2 transport streams to FLV format. Primarily used for legacy Flash-based video players.
/**
* FLV Transmuxer for converting transport streams to FLV format
* Note: This module is in maintenance mode
*/
class Transmuxer {
constructor();
/** Add transport stream data for processing */
push(data: Uint8Array): void;
/** Flush any remaining data and finalize processing */
flush(): void;
/** Reset transmuxer state */
reset(): void;
/** Register event listeners for processed FLV segments */
on(event: 'data', callback: (flvData: FlvSegment) => void): void;
on(event: 'done', callback: () => void): void;
}
interface FlvSegment {
data: Uint8Array;
tags: FlvTag[];
}Usage Examples:
const muxjs = require("mux.js");
// Create FLV transmuxer
const flvTransmuxer = new muxjs.flv.Transmuxer();
// Handle processed FLV data
flvTransmuxer.on('data', (flvData) => {
console.log(`FLV segment: ${flvData.data.length} bytes`);
console.log(`FLV tags: ${flvData.tags.length}`);
// Process FLV tags
flvData.tags.forEach(tag => {
console.log(`Tag type: ${tag.tagType}, size: ${tag.dataSize}`);
});
// Use FLV data (e.g., send to Flash player)
processFlvData(flvData.data);
});
// Process transport stream data
flvTransmuxer.push(transportStreamData);
flvTransmuxer.flush();Class for representing and processing individual FLV tags within FLV files.
/**
* FLV Tag representation and processing
* Handles audio, video, and script data tags
*/
class FlvTag {
constructor();
/** Tag type (audio, video, script) */
tagType: number;
/** Tag data size */
dataSize: number;
/** Timestamp for the tag */
timestamp: number;
/** Tag data payload */
data: Uint8Array;
/** Parse tag from binary data */
static parse(data: Uint8Array): FlvTag;
/** Serialize tag to binary data */
serialize(): Uint8Array;
}
// FLV tag type constants
const FLV_TAG_TYPES = {
AUDIO: 8,
VIDEO: 9,
SCRIPT_DATA: 18
};Usage Examples:
const muxjs = require("mux.js");
// Parse FLV tags from data
const flvData = new Uint8Array([/* FLV file data */]);
let offset = 13; // Skip FLV header
while (offset < flvData.length) {
const tag = muxjs.flv.tag.parse(flvData.subarray(offset));
console.log(`FLV Tag: type ${tag.tagType}, size ${tag.dataSize}`);
switch (tag.tagType) {
case 8: // Audio
console.log(`Audio tag at ${tag.timestamp}ms`);
break;
case 9: // Video
console.log(`Video tag at ${tag.timestamp}ms`);
break;
case 18: // Script data
console.log(`Script data at ${tag.timestamp}ms`);
break;
}
// Move to next tag
offset += tag.dataSize + 11; // Tag header (11 bytes) + data + previous tag size (4 bytes)
}Function for generating FLV file headers with appropriate format specifications.
/**
* Generate FLV file header
* @returns FLV header bytes including signature and format flags
*/
function getFlvHeader(): Uint8Array;Usage Examples:
const muxjs = require("mux.js");
// Generate FLV header
const flvHeader = muxjs.flv.getFlvHeader();
console.log(`FLV header: ${flvHeader.length} bytes`);
// Typical FLV header structure:
// - FLV signature (3 bytes): 'F', 'L', 'V'
// - Version (1 byte): 0x01
// - Flags (1 byte): audio/video presence flags
// - Header size (4 bytes): typically 9
// - Previous tag size (4 bytes): 0 for first tag
// Use header to start FLV file
const flvFile = new Uint8Array(flvHeader.length + flvData.length);
flvFile.set(flvHeader, 0);
flvFile.set(flvData, flvHeader.length);Complete pipeline for transport stream to FLV conversion.
const muxjs = require("mux.js");
// Create FLV processing pipeline
const packetStream = new muxjs.mp2t.TransportPacketStream();
const parseStream = new muxjs.mp2t.TransportParseStream();
const elementaryStream = new muxjs.mp2t.ElementaryStream();
const flvTransmuxer = new muxjs.flv.Transmuxer();
// Connect transport stream processing
packetStream
.pipe(parseStream)
.pipe(elementaryStream);
// Route elementary stream data to FLV transmuxer
elementaryStream.on('data', (pesPacket) => {
if (pesPacket.type === 'video' || pesPacket.type === 'audio') {
flvTransmuxer.push(pesPacket.data);
}
});
// Handle FLV output
flvTransmuxer.on('data', (flvSegment) => {
// Create complete FLV file
const header = muxjs.flv.getFlvHeader();
const complete = new Uint8Array(header.length + flvSegment.data.length);
complete.set(header, 0);
complete.set(flvSegment.data, header.length);
// Use FLV data
processFlvFile(complete);
});
// Process original transport stream
packetStream.push(transportStreamData);
packetStream.flush();
flvTransmuxer.flush();Debugging utilities for FLV file inspection and analysis.
/**
* FLV inspection and debugging tools
*/
const tools: {
/** Inspect individual FLV tag structure */
inspectTag(data: Uint8Array): FlvTagInfo;
/** Inspect complete FLV file structure */
inspect(data: Uint8Array): FlvStructure;
/** Convert FLV structure to human-readable text */
textify(structure: FlvStructure): string;
};
interface FlvTagInfo {
tagType: number;
dataSize: number;
timestamp: number;
streamId: number;
data: Uint8Array;
}
interface FlvStructure {
header: FlvHeader;
tags: FlvTagInfo[];
totalSize: number;
}
interface FlvHeader {
signature: string;
version: number;
flags: {
audio: boolean;
video: boolean;
};
headerSize: number;
}Usage Examples:
const muxjs = require("mux.js");
// Inspect FLV file
const flvData = new Uint8Array([/* FLV file data */]);
const flvStructure = muxjs.flv.tools.inspect(flvData);
console.log('FLV Analysis:');
console.log(`Version: ${flvStructure.header.version}`);
console.log(`Has audio: ${flvStructure.header.flags.audio}`);
console.log(`Has video: ${flvStructure.header.flags.video}`);
console.log(`Total tags: ${flvStructure.tags.length}`);
// Analyze tags
let audioTags = 0;
let videoTags = 0;
let scriptTags = 0;
flvStructure.tags.forEach(tag => {
switch (tag.tagType) {
case 8: audioTags++; break;
case 9: videoTags++; break;
case 18: scriptTags++; break;
}
});
console.log(`Audio tags: ${audioTags}`);
console.log(`Video tags: ${videoTags}`);
console.log(`Script tags: ${scriptTags}`);
// Get human-readable representation
const textOutput = muxjs.flv.tools.textify(flvStructure);
console.log('FLV Structure:');
console.log(textOutput);
// Inspect individual tag
const firstTag = flvStructure.tags[0];
if (firstTag) {
const tagInfo = muxjs.flv.tools.inspectTag(firstTag.data);
console.log('First tag details:', tagInfo);
}Since the FLV module is in maintenance mode, consider these points:
For new applications, use MP4 transmuxing instead:
// Instead of FLV transmuxing
const flvTransmuxer = new muxjs.flv.Transmuxer();
// Use MP4 transmuxing for better compatibility
const mp4Transmuxer = new muxjs.mp4.Transmuxer();The FLV module will not receive new features. For advanced video processing needs, use the MP4 module which provides:
The FLV module should only be used when:
interface FlvSegment {
data: Uint8Array;
tags: FlvTag[];
}
interface FlvTag {
tagType: number;
dataSize: number;
timestamp: number;
streamId: number;
data: Uint8Array;
}
interface FlvHeader {
signature: string;
version: number;
flags: {
audio: boolean;
video: boolean;
};
headerSize: number;
}
interface FlvStructure {
header: FlvHeader;
tags: FlvTagInfo[];
totalSize: number;
}
interface FlvTagInfo {
tagType: number;
dataSize: number;
timestamp: number;
streamId: number;
data: Uint8Array;
}