pcm-extract0.1.0 • Public • Published
Extract PCM data from audio/video file with ffmpeg
This module lets you extract a PCM representation of the audio from any audio or video file using ffmpeg. You get access to every single PCM sample value on every available channels and audio tracks in the file as a native readable stream.
You need ffmpeg up and running to use this module.
/* get PCM samples as a readable stream */const readable =;/* consume stream */readable;
/* read only a subsection of the file */const readable =;/* read second audio track in file */const readable =;/* downmix all channels into one */const readable =;/* resample audio */const readable =;/* pre process samples */const readable =;
filepath string, required
Path to input file.
channels number, optional
Number of audio channels in the output stream.
By default you get all channels present in the input file. If you set this to a value less than the number of channels in the input, ffmpeg merges them in the way that makes the most sense.
Example uses cases :
- 5.1 (6 channels) -> standard stereo (2 channels)
- multiple channels -> all merged into 1 channel
sampleRate number, optional
Sample rate of the output in Hz, i.e. the number of samples per channel per second in the output stream.
By default the output stream has the same sample rate as the input. If you set this to a different value then the input will be resampled to the specified frequency.
start number, optional
Position in time to start reading the input, in milliseconds.
By default the input is read from the beginning. If you set this you only get samples after the specified time.
end number, optional
Position in time to stop reading the input, in milliseconds.
By default the input is read until the end. If you set this you only get samples before the specified time.
init function, optional
Initialization hook for the decoding stream.
The provided hooks let you customize how the samples are processed before they are output on the final stream. The functions you provide are called with
this set to the Transform stream object. Using these hooks to process the samples is more efficient than piping into an external transform stream.
Use this hook to perform any initializations you might need for your decoding logic.
processSample function, optional
Processing hook for the decoding stream.
Use this hook to process a sample that has just been decoded. The function receives the sample value in range [-1, 1] as first argument, and you must use
this.push(value) to output a value on the final stream. You do not have to output a value for each call of that function.
getStream() returns a standard readable stream in object mode where each item is a
number representing the sample value in the range [-1, 1].
You listen to the
readable event to know when data is available and call
readable.read() to get one sample as a number.
By default, all samples are returned and alternate between channels, i.e. this is what you get for classic stereo (2 channels) :
- sample 0: Left channel, sample 0
- sample 1: Right channel, sample 0
- sample 2: Left channel, sample 1
- sample 3: Right channel, sample 1