Skip to content

Instantly share code, notes, and snippets.

@sloev
Created February 29, 2024 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloev/52252d5dd584b72d2b10f47bc59d55fb to your computer and use it in GitHub Desktop.
Save sloev/52252d5dd584b72d2b10f47bc59d55fb to your computer and use it in GitHub Desktop.
microphone to spectrogram to phash realtime using ffmpeg and nodejs
import Pipe2Pam from 'pipe2pam';
import process from 'node:process';
import {spawn} from 'child_process';
import imghash from 'imghash';
let counter = 0;
const params = [
'-loglevel',
'quiet',
'-f',
'pulse',
'-i',
'default',
'-filter_complex',
'[0:a]showspectrum=s=32x32:mode=combined:slide=scroll:saturation=0.2:scale=log,format=gray[v]',
'-map',
'[v]',
'-c:v',
'pam',
'-r','10',
'-f',
'image2pipe',
'-pix_fmt',
//'rgb24',
//'rgba',
'gray',
//'monob',
'pipe:1'
];
const pipe2pam = new Pipe2Pam();
pipe2pam.on('data', (data) => {
const d = { width:data.width , height: data.height, data: data.pixels }
const h = imghash.hashRaw(d, 8)
process.stdout.write(h+"\n")
});
const ffmpeg = spawn('ffmpeg', params);
ffmpeg.on('error', (error) => {
console.log(error);
});
ffmpeg.on('exit', (code, signal) => {
console.log('exit', code, signal);
});
ffmpeg.stdout.pipe(pipe2pam);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment