yuv-rx
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

yuv-rx

JavaScript library for reading videos as Rx Observables of decoded frames

Example usage: reading a frame at 0:42, converting to RGB, and encoding it as a base64 encoded PNG image with sharp

import {Observable, from, take, map, concatMap} from 'rxjs';
import {toRGBInterleaved, YuvParser} from 'yuv-rx';
const sharp = require('sharp');

const yuvParser = new YuvParser({
    ffmpeg: '/usr/local/bin/ffmpeg', // optional path, defaults to 'ffmpeg'
    verbose: true
});
yuvParser.read('input.mp4', {
    videoStream: 1,  // select second video stream of input file
    seekSeconds: 42, // skip 42 seconds without decoding
    vframes: 1       // only decode 1 frame
}).pipe(
    take(1),
    map(toRGBInterleaved),
    concatMap(frame => {
        return from(sharp(frame.colorPlanes.rgb.data, {
            raw: {
                width: frame.header.width,
                height: frame.header.height,
                channels: 3,
                premultiplied: false
            }
        }).png().toBuffer().then(b => b.toString('base64')));
    })
).subscribe({
    next: base64frame => console.log(base64frame)
});

Example of usage with custom ffmpeg command line options to find frames with difference at pixel (42,42)

import {Observable, from, take, map, concatMap} from 'rxjs';

const yuvParser = new YuvParser({
    ffmpeg: '/usr/local/bin/ffmpeg', // optional path, defaults to 'ffmpeg'
    verbose: false
});

yuvParser.readCustom([
    '-i',
    'input1.mp4',
    '-i',
    'input2.mp4',
    '-filter_complex',
    '[0:v]format=gray[2];[1:v]format=gray[3];[2][3]blend=all_mode=difference[d];[d]format=yuv420p[v]',
    '-map',
    '[v]',
]).pipe(
    filter(diffFrame => diffFrame.colorPlanes.y.data[42*diffFrame.header.width + 42] > 10),
    map(diffFrame => diffFrame.header.fn),
    toArray()
).subscribe({
    next: frameNumbers => console.log(`Frames with difference at pixel (42,42): ${frameNumbers}`)
});

© VideoGorillas LLC 2022

Package Sidebar

Install

npm i yuv-rx

Weekly Downloads

18

Version

0.3.2

License

MIT

Unpacked Size

40.9 kB

Total Files

21

Last publish

Collaborators

  • andrew-vg