<commit_before>// jshint esversion: 6, globalstrict: true, strict: true
'use strict';
const P2P = require('pipe2pam');
const PamDiff = require('../index');
const ChildProcess = require('child_process');
const spawn = ChildProcess.spawn;
const execFile = ChildProcess.execFile;

const params = [
    '-loglevel',
    'quiet',

    /* use hardware acceleration */
    '-hwaccel',
    'auto',//vda, videotoolbox, none, auto

    /* use a list of mp4 videos as input */
    //'-re',//uncomment to have ffmpeg read videos at a slower realtime rate
    '-i',
    `${__dirname}/in/ffconcat.txt`,
    '-f',
    'concat',

    /* set output flags */
    '-an',
    '-c:v',
    'pam',
    '-pix_fmt',
    'gray',
    //'rgba',
    //'rgb24',
    //'monob',
    '-f',
    'image2pipe',
    '-vf',
    'fps=2,scale=640:360',//1920:1080 scaled down = 640:360, 400:225, 384:216, 368:207, 352:198, 336:189, 320:180
    //'fps=1,scale=iw*1/6:ih*1/6',
    '-frames',
    '2000',
    'pipe:1'
];

const ffmpeg = spawn('ffmpeg', params, {
    stdio: ['ignore', 'pipe', 'ignore']
});

console.log(ffmpeg.spawnargs.join(' '));

ffmpeg.on('error', (error) => {
    console.log(error);
});

ffmpeg.on('exit', (code, signal) => {
    console.log('exit', code, signal);
    console.timeEnd('grayOut.js');
});

const p2p = new P2P();

let counter = 0;

p2p.on('pam', (data) => {
    //you do not have to listen to this event if you are just piping this data to pam-diff
    console.log(`received pam ${++counter}`);
});

const region1 = {name: 'region1', difference: 10, percent: 11, polygon: [{x: 0, y: 0}, {x: 0, y:360}, {x: 160, y: 360}, {x: 160, y: 0}]};

const region2 = {name: 'region2', difference: 10, percent: 10, polygon: [{x: 160, y: 0}, {x: 160, y: 360}, {x: 320, y: 360}, {x: 320, y: 0}]};

const region3 = {name: 'region3', difference: 10, percent: 10, polygon: [{x: 320, y: 0}, {x: 320, y: 360}, {x: 480, y: 360}, {x: 480, y: 0}]};

const region4 = {name: 'region4', difference: 10, percent: 9, polygon: [{x: 480, y: 0}, {x: 480, y: 360}, {x: 640, y: 360}, {x: 640, y: 0}]};

const regions = [region1, region2, region3, region4];

const pamDiff = new PamDiff({grayscale: 'average', regions : regions});

pamDiff.on('diff', (data) => {
    console.log(data);

    //comment out the following line if you want to use ffmpeg to create a jpeg from the pam image that triggered an image difference event
    //if(true){return;}

    const date = new Date();
    let name = `${date.getUTCFullYear()}-${date.getUTCMonth() + 1}-${date.getUTCDate()}_${date.getHours()}-${date.getUTCMinutes()}-${date.getUTCSeconds()}-${date.getUTCMilliseconds()}`;
    for (const region of data.trigger) {
        name += `(${region.name}=${region.percent})`;
    }
    const jpeg = `${name}.jpeg`;
    const pathToJpeg = `${__dirname}/out/gray/${jpeg}`;
    const ff = execFile('ffmpeg', ['-f', 'pam_pipe', '-c:v', 'pam', '-i', 'pipe:0', '-c:v', 'mjpeg', '-pix_fmt', 'yuvj422p', '-q:v', '1', '-huffman', 'optimal', pathToJpeg]);
    ff.stdin.end(data.pam);
    ff.on('exit', (data) => {
        if (data === 0) {
            console.log(`FFMPEG clean exit after creating ${jpeg}`);
        } else {
            throw new Error('FFMPEG is not working with current parameters');
        }
    });
});
<commit_msg>removed grayscale setting since it is using gray pams and does not need to convert from rgb<commit_after>// jshint esversion: 6, globalstrict: true, strict: true
'use strict';
const P2P = require('pipe2pam');
const PamDiff = require('../index');
const ChildProcess = require('child_process');
const spawn = ChildProcess.spawn;
const execFile = ChildProcess.execFile;

const params = [
    '-loglevel',
    'quiet',

    /* use hardware acceleration */
    '-hwaccel',
    'auto',//vda, videotoolbox, none, auto

    /* use a list of mp4 videos as input */
    //'-re',//uncomment to have ffmpeg read videos at a slower realtime rate
    '-i',
    `${__dirname}/in/ffconcat.txt`,
    '-f',
    'concat',

    /* set output flags */
    '-an',
    '-c:v',
    'pam',
    '-pix_fmt',
    'gray',
    //'rgba',
    //'rgb24',
    //'monob',
    '-f',
    'image2pipe',
    '-vf',
    'fps=2,scale=640:360',//1920:1080 scaled down = 640:360, 400:225, 384:216, 368:207, 352:198, 336:189, 320:180
    //'fps=1,scale=iw*1/6:ih*1/6',
    '-frames',
    '2000',
    'pipe:1'
];

const ffmpeg = spawn('ffmpeg', params, {
    stdio: ['ignore', 'pipe', 'ignore']
});

console.log(ffmpeg.spawnargs.join(' '));

ffmpeg.on('error', (error) => {
    console.log(error);
});

ffmpeg.on('exit', (code, signal) => {
    console.log('exit', code, signal);
    console.timeEnd('grayOut.js');
});

const p2p = new P2P();

let counter = 0;

p2p.on('pam', (data) => {
    //you do not have to listen to this event if you are just piping this data to pam-diff
    console.log(`received pam ${++counter}`);
});

const region1 = {name: 'region1', difference: 10, percent: 11, polygon: [{x: 0, y: 0}, {x: 0, y:360}, {x: 160, y: 360}, {x: 160, y: 0}]};

const region2 = {name: 'region2', difference: 10, percent: 10, polygon: [{x: 160, y: 0}, {x: 160, y: 360}, {x: 320, y: 360}, {x: 320, y: 0}]};

const region3 = {name: 'region3', difference: 10, percent: 10, polygon: [{x: 320, y: 0}, {x: 320, y: 360}, {x: 480, y: 360}, {x: 480, y: 0}]};

const region4 = {name: 'region4', difference: 10, percent: 9, polygon: [{x: 480, y: 0}, {x: 480, y: 360}, {x: 640, y: 360}, {x: 640, y: 0}]};

const regions = [region1, region2, region3, region4];

const pamDiff = new PamDiff({/*grayscale: 'luminosity', rgb grayscale conversion does not need to be set if using grayscale pams*/regions : regions});

pamDiff.on('diff', (data) => {
    console.log(data);

    //comment out the following line if you want to use ffmpeg to create a jpeg from the pam image that triggered an image difference event
    //if(true){return;}

    const date = new Date();
    let name = `${date.getUTCFullYear()}-${date.getUTCMonth() + 1}-${date.getUTCDate()}_${date.getHours()}-${date.getUTCMinutes()}-${date.getUTCSeconds()}-${date.getUTCMilliseconds()}`;
    for (const region of data.trigger) {
        name += `(${region.name}=${region.percent})`;
    }
    const jpeg = `${name}.jpeg`;
    const pathToJpeg = `${__dirname}/out/gray/${jpeg}`;
    const ff = execFile('ffmpeg', ['-f', 'pam_pipe', '-c:v', 'pam', '-i', 'pipe:0', '-c:v', 'mjpeg', '-pix_fmt', 'yuvj422p', '-q:v', '1', '-huffman', 'optimal', pathToJpeg]);
    ff.stdin.end(data.pam);
    ff.on('exit', (data) => {
        if (data === 0) {
            console.log(`FFMPEG clean exit after creating ${jpeg}`);
        } else {
            throw new Error('FFMPEG is not working with current parameters');
        }
    });
});

{'fraction_non_alphanumeric': 0.21022811987475773, 'fraction_numerical': 0.05606083196660206, 'mean_word_length': 3.3473752430330523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6468567', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2464, 'n_words': 706}