Skip to content

Instantly share code, notes, and snippets.

@PSingletary
Last active January 10, 2025 13:06
Show Gist options
  • Save PSingletary/e6e0e3a622752c6f26ad6c397c40484a to your computer and use it in GitHub Desktop.
Save PSingletary/e6e0e3a622752c6f26ad6c397c40484a to your computer and use it in GitHub Desktop.
tampermonkey scripts for streamlink

Streamlink Tampermonkey Scripts

This repository contains a collection of scripts designed to be used with Tampermonkey to launch Streamlink for recording streams. The scripts are compatible with both macOS and Windows, assuming that Streamlink is already installed on the system.

Scripts

streamlink.js

This script detects the operating system and the presence of Streamlink, then launches Streamlink to record the stream.

macos_script.sh

A shell script for macOS that launches Streamlink to record a stream.

windows_script.bat

A batch script for Windows that launches Streamlink to record a stream.

macos.js

assumes using macOS and streamlink is alreaady installed

windows.js

assumes using windows and streamlink is alreaady installed

Prerequisites

Usage

  1. Open the stream you want to record in your browser.
  2. The Tampermonkey script will provide a button to help launch streamlink for that URL to start recording.
// ==UserScript==
// @name Launch Terminal and Streamlink
// @namespace http://tampermonkey.net/
// @version 0.1
// @description button for twitch that launches terminal and streamlink
// ==/UserScript==
(function() {
'use strict';
// Get auth token from cookies
const authToken = document.cookie.split("; ").find(item => item.startsWith("auth-token="))?.split("=")[1];
// Get URL (you might want to modify this part to accurately grab the correct URL)
const url = window.location.href;
// Create an AppleScript to launch Terminal and execute streamlink command
const appleScript = `
tell application "Terminal"
activate
do script "streamlink --twitch-api-header=Authorization=OAuth ${authToken} --record '/Users/{path}/streamlink/{author}-{category}-{title}-{time:%Y%m%d}.ts' ${url} best"
end tell
`;
// Use a hidden form to submit the AppleScript to the OS
const form = document.createElement('form');
form.method = 'POST';
form.action = 'javascript:(function(){var w=window.open();w.document.open();w.document.write(\'<script>\' + appleScript + \'</script>\');})()';
document.body.appendChild(form);
form.submit();
})();
const { exec } = require('child_process');
const readline = require('readline');
const path = require('path');
// Function to prompt user for input
const prompt = (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => rl.question(query, (ans) => {
rl.close();
resolve(ans);
}));
};
(async () => {
try {
// Prompt for Twitch URL
const twitchUrl = await new Promise((resolve, reject) => {
exec('powershell -command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetText()"', (error, stdout, stderr) => {
if (error) {
reject(`Error: ${error.message}`);
return;
}
if (stderr) {
reject(`Stderr: ${stderr}`);
return;
}
const url = stdout.trim();
if (url.startsWith('https://www.twitch.tv/')) {
resolve(url);
} else {
reject('The URL is not a valid Twitch URL.');
}
});
});
// Prompt for Twitch Auth Code
const authCode = await new Promise((resolve, reject) => {
exec('powershell -command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetText()"', (error, stdout, stderr) => {
if (error) {
reject(`Error: ${error.message}`);
return;
}
if (stderr) {
reject(`Stderr: ${stderr}`);
return;
}
const code = stdout.trim();
if (code) {
resolve(code);
} else {
reject('Failed to retrieve the auth code from the clipboard.');
}
});
});
// Prompt for download path
const downloadPath = await prompt('Enter the download path: ');
// Construct the streamlink command
const command = `streamlink --twitch-api-header "Authorization=OAuth ${authCode}" ${twitchUrl} best -o ${path.join(downloadPath, 'recording.ts')}`;
// Execute the command
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`Stderr: ${stderr}`);
return;
}
console.log(`Output: ${stdout}`);
});
} catch (error) {
console.error(`Error: ${error.message}`);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment