Skip to content

Instantly share code, notes, and snippets.

@riderjensen
Created November 22, 2023 07:15
Show Gist options
  • Save riderjensen/2964e526cc9a902784f52ffb9cc60973 to your computer and use it in GitHub Desktop.
Save riderjensen/2964e526cc9a902784f52ffb9cc60973 to your computer and use it in GitHub Desktop.
Stream mp4s to twitch using FFMPEG
const PATH_TO_VIDEOS = ''; // Where your show is stored
const STREAM_KEY = ''; // https://dashboard.twitch.tv/u/YOUR_USERNAME/settings/stream
const RTMP_SERVER = ''; // https://help.twitch.tv/s/twitch-ingest-recommendation?language=en_US
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const video = ffmpeg();
const contents = fs.readdirSync(PATH_TO_VIDEOS, { recursive: true, withFileTypes: true });
const cleaned = contents.filter(file => {
return file.isFile();
});
cleaned.forEach(item => {
video.addInput(`${item.path}/${item.name}`)
});
function stream() {
video
.videoBitrate('2500k')
.videoCodec('libx264')
.audioBitrate('128k')
.audioCodec('aac')
.inputOptions([
'-re',
])
.output(`${RTMP_SERVER}${STREAM_KEY}`)
.outputOptions([
'-ar 44100',
'-pix_fmt yuvj420p',
'-x264-params keyint=48:min-keyint=48:scenecut=-1',
'-crf 28',
'-threads 4',
'-f flv',
'-preset medium'
])
.on('start', (commandLine) => {
console.log('Spawned Ffmpeg with command: ' + commandLine);
})
.on('end', () => {
stream();
})
.run()
}
stream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment