Skip to content

Instantly share code, notes, and snippets.

@rsheldiii
Created April 24, 2022 19:03
Show Gist options
  • Save rsheldiii/9aeb6ab33636cec2eb0bfec307766493 to your computer and use it in GitHub Desktop.
Save rsheldiii/9aeb6ab33636cec2eb0bfec307766493 to your computer and use it in GitHub Desktop.
Monogram creator VLC Node client
{
"name": "YourAppNameHere",
"id": "com.example.your_app_name_here",
"exe": ["vlc.exe"],
"bundle": ["com.example.your_app_name_here"],
"connection": [
{
"name": "YourConnectionNameHereConnection",
"type": "websocket"
}
]
}
[
"Close",
"Next",
"Previous",
{
"name": "Skip",
"step": 1
},
{
"name": "Jump",
"range": [0, 100],
"step": 0.5,
"reset": 0
}
]
// This is a rather barebones VLC implementation for Monogram Creator, formerly Palettegear.
// This is mostly centered around using the slider to control video playhead position.
// You need to set VLC to have the "web" interface, and set a password on the "Lua" page in the "Lua HTTP" section
// You also need to set up your bundle and connection name in config.json
// Newer versions of Node cannot pick up the websocket connection in my experience. I'm running Node 14.19.1
// Open VLC and then run via `node main.js` and keep the window open
const {WebSocket} = require('ws');
const fetch = require('node-fetch');
const VLC_BASE_URL = 'http://localhost:8080/requests/'
const VLC_PLAYLIST_URL = `${VLC_BASE_URL}playlist.xml`
class VLC {
stop() {
this.get('pl_pause')
}
clear() {
this.get('pl_empty')
}
skip(seconds) {
// unsigned numbers are absolute. Signed numbers are relative
if (Number(seconds) > 0) {
seconds = `+${seconds}`
}
this.get('seek', {val: `${seconds}`})
}
jump(percent) {
this.get('seek', {val: `${percent}%`})
}
get(command, args = {}) {
const extraParams = Object.keys(args).map((key) => (`&${encodeURIComponent(key)}=${encodeURIComponent(args[key])}`)).join('')
return fetch(`${VLC_PLAYLIST_URL}?command=${command}${extraParams}`, {
method: 'GET',
// mode: 'no-cors', // no-cors does not allow auth; you cannot run this file in a web browser
headers: {
Authorization: 'Basic ' + Buffer.from(':YOUR_PASSWORD_HERE').toString('base64')
}
})
}
}
class MonogramController {
constructor() {
this.monogram = new WebSocket('ws://localhost:59177/com.example.your_app_name_here/YourConnectionNameHereConnection')
this.monogram.addEventListener("message", (event) => {
var data = JSON.parse(event.data);
console.log(data);
if (typeof this[data.input] === "function") {
this[data.input](...data.params);
}
})
this.vlc = new VLC();
}
Jump(percent) {
this.vlc.jump(percent);
}
Close() {
this.vlc.clear();
}
Skip(seconds) {
this.vlc.skip(seconds);
}
}
controller = new MonogramController();
{
"dependencies": {
"node-fetch": "^2.6.7",
"ws": "^8.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment