provide YT sounds local, for GreaseMonkey Scripts
Today someone asked for a Script which detects a Message for the new MPGH IM and plays a Sound and i've written a lil Script for it.
Maybe it's interessting for something/someone else too:
The NodeJS Part is well coded, the Grease Monkey Part, only an example and can be improved very much
It needs ffmpeg.exe in the same folder as the server for work.
NodeJS:
Code:
const http = require('http'),
fs = require('fs'),
ytdl = require('ytdl-core'),
cp = require('child_process'),
port = 1337;
const VideoURL = 'https://www.youtube.com/watch?v=gycSZoYzEks',
saveVideoAs = 'video.flv',
saveAudioAs = 'sound.mp3';
// This is the Response if you call localhost:1337
const requestAnswer = (req, res) => {
const path = `./${saveAudioAs}`,
stat = fs.statSync(path),
total = stat.size;
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'audio/mp3' });
fs.createReadStream(path).pipe(res);
};
// It runs the HTTP Server
const startServer = code => {
http.createServer(requestAnswer).listen(port);
console.log(`Server is listening on Port ${port}`);
};
// It spawns the HTTP Server after it extracted the Audio from the Video
const onDownloadEnd = () => {
const ls = cp.spawn('ffmpeg', ['-i', saveVideoAs, saveAudioAs]);
ls.on('close', startServer);
};
// This is the Second Task, download the Video, save it as 'video.flv' to get the Audio File from it
const afterFileDelete = err => {
const vidRequest = ytdl(VideoURL);
vidRequest.on('end', onDownloadEnd);
vidRequest.pipe(fs.createWriteStream(saveVideoAs));
};
// Just a stupid Stub
const warnSuppress= () => {};
// Program Main
fs.unlink(`./${saveAudioAs}`, warnSuppress);
fs.unlink(`./${saveVideoAs}`, afterFileDelete);
GreaseMonkey:
Code:
var intervalTimer = setInterval(() => {
// can we Find any Container with Class Name 'messagesContainer'
var header = document.getElementsByClassName('messagesContainer');
if (typeof header !== 'object' || header.length === 0) {
return;
}
// Yes, so play a Sound
var audio = new Audio('https://localhost:1337/sound.mp3');
if (!!audio && typeof audio === 'object') {
audio.play();
}
clearInterval(intervalTimer);
}, 300);