Class: Rational
Defined in: src/lib/rational.ts:34
Rational number type for precise fractional representations.
Represents a rational number as a fraction (numerator/denominator). Used throughout FFmpeg for time bases, aspect ratios, frame rates, and other fractional values that require exact precision.
Example
import { Rational } from 'node-av';
// Common time bases
const timebase = new Rational(1, 90000); // 90kHz for MPEG-TS
const videoTimebase = new Rational(1, 25); // 25 fps
const audioTimebase = new Rational(1, 48000); // 48kHz audio
// Frame rates
const framerate = new Rational(30, 1); // 30 fps
const ntscFramerate = new Rational(30000, 1001); // 29.97 fps NTSC
const palFramerate = new Rational(25, 1); // 25 fps PAL
// Aspect ratios
const aspectRatio = new Rational(16, 9); // 16:9
const pixelAspect = new Rational(1, 1); // Square pixels
// Arithmetic operations
const doubled = timebase.mul(new Rational(2, 1));
const inverted = framerate.inv(); // Get frame duration
const sum = framerate.add(new Rational(5, 1)); // Add 5 fpsConstructors
Constructor
new Rational(
num,den):Rational
Defined in: src/lib/rational.ts:64
Create a new rational number.
Represents the fraction num/den.
Parameters
num
number
Numerator of the fraction
den
number
Denominator of the fraction (must not be 0)
Returns
Rational
Throws
If denominator is 0
Example
import { Rational } from 'node-av';
// Create time base for 25 fps video
const timebase = new Rational(1, 25);
// Create NTSC frame rate (29.97 fps)
const ntsc = new Rational(30000, 1001);
// Will throw error
try {
const invalid = new Rational(1, 0);
} catch (error) {
console.error('Cannot have zero denominator');
}Properties
den
readonlyden:number
Defined in: src/lib/rational.ts:66
Denominator of the fraction (must not be 0)
num
readonlynum:number
Defined in: src/lib/rational.ts:65
Numerator of the fraction
Methods
add()
add(
other):Rational
Defined in: src/lib/rational.ts:111
Add two rational numbers.
Performs addition: (a/b) + (c/d) = (ad + bc) / bd
Parameters
other
Rational
The rational number to add
Returns
Rational
A new Rational representing the sum
Example
const a = new Rational(1, 2); // 1/2
const b = new Rational(1, 3); // 1/3
const sum = a.add(b); // 5/6
console.log(sum.toString()); // "5/6"div()
div(
other):Rational
Defined in: src/lib/rational.ts:174
Divide two rational numbers.
Performs division: (a/b) ÷ (c/d) = (ad) / (bc)
Parameters
other
Rational
The rational number to divide by
Returns
Rational
A new Rational representing the quotient
Example
const pixels = new Rational(1920, 1); // 1920 pixels width
const aspect = new Rational(16, 9); // 16:9 aspect ratio
const height = pixels.div(aspect); // Calculate height
console.log(height.toDouble()); // 1080equals()
equals(
other):boolean
Defined in: src/lib/rational.ts:265
Check if this rational equals another.
Compares using cross-multiplication to avoid floating point errors. Two rationals a/b and c/d are equal if ad = bc.
Parameters
other
Rational
The rational to compare with
Returns
boolean
true if the rationals are equal, false otherwise
Example
const a = new Rational(2, 4);
const b = new Rational(1, 2);
const c = new Rational(3, 4);
console.log(a.equals(b)); // true (both are 1/2)
console.log(a.equals(c)); // falseinv()
inv():
Rational
Defined in: src/lib/rational.ts:217
Invert the rational number (reciprocal).
Returns the reciprocal: 1/(a/b) = b/a
Returns
Rational
A new Rational representing the reciprocal
Example
const framerate = new Rational(25, 1); // 25 fps
const frameDuration = framerate.inv(); // 1/25 seconds per frame
console.log(frameDuration.toString()); // "1/25"
console.log(frameDuration.toDouble()); // 0.04 secondsisValid()
isValid():
boolean
Defined in: src/lib/rational.ts:198
Check if this rational is valid (both num and den are positive).
FFmpeg uses { 0, 0 } as "undefined" and { num, 0 } as infinity. This method returns true only if both values are positive.
Returns
boolean
true if num > 0 and den > 0
Example
const valid = new Rational(25, 1);
console.log(valid.isValid()); // true
const undefined = new Rational(0, 0);
console.log(undefined.isValid()); // false
const infinity = new Rational(1, 0);
console.log(infinity.isValid()); // falsemul()
mul(
other):Rational
Defined in: src/lib/rational.ts:153
Multiply two rational numbers.
Performs multiplication: (a/b) × (c/d) = (ac) / (bd)
Parameters
other
Rational
The rational number to multiply by
Returns
Rational
A new Rational representing the product
Example
const framerate = new Rational(30, 1); // 30 fps
const duration = new Rational(5, 1); // 5 seconds
const frames = framerate.mul(duration); // 150/1 = 150 frames
console.log(frames.toDouble()); // 150sub()
sub(
other):Rational
Defined in: src/lib/rational.ts:132
Subtract two rational numbers.
Performs subtraction: (a/b) - (c/d) = (ad - bc) / bd
Parameters
other
Rational
The rational number to subtract
Returns
Rational
A new Rational representing the difference
Example
const a = new Rational(3, 4); // 3/4
const b = new Rational(1, 4); // 1/4
const diff = a.sub(b); // 2/4 = 1/2
console.log(diff.toString()); // "2/4"toDouble()
toDouble():
number
Defined in: src/lib/rational.ts:241
Convert rational to floating point number.
Calculates the decimal value: num / den Note: This may lose precision for some rational values.
Returns
number
The floating point representation (Infinity if den = 0, NaN if both = 0)
Example
const ntsc = new Rational(30000, 1001); // NTSC frame rate
console.log(ntsc.toDouble()); // 29.97002997...
const half = new Rational(1, 2);
console.log(half.toDouble()); // 0.5
const infinity = new Rational(1, 0);
console.log(infinity.toDouble()); // InfinitytoString()
toString():
string
Defined in: src/lib/rational.ts:285
Get string representation of the rational.
Returns the rational in "num/den" format.
Returns
string
String representation as "numerator/denominator"
Example
const framerate = new Rational(30000, 1001);
console.log(framerate.toString()); // "30000/1001"
const timebase = new Rational(1, 90000);
console.log(`Timebase: ${timebase}`); // "Timebase: 1/90000"fromObject()
staticfromObject(rational):Rational
Defined in: src/lib/rational.ts:90
Create a Rational from an IRational object.
Parameters
rational
The IRational object with num and den properties
Returns
Rational
A new Rational instance
Example
import { Rational } from 'node-av';
const obj = { num: 3, den: 4 };
const rational = Rational.fromObject(obj);
console.log(rational.toString()); // "3/4"