question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

livestreaming to twitch using ffmpeg

See original GitHub issue

Hi there. I have a webRTC application that I am trying to add livestreaming to. I am trying to set up a small test where I can livestream to twitch using puppeteer-stream and ffmpeg. It seems as if I am only sending data to twitch right before the script ends. Do you know what I am doing wrong in the code below? Thank you!

const { launch, getStream } = require('puppeteer-stream')
const fs = require('fs')
const { exec } = require('child_process')

require('dotenv').config()

// puppeteer stream + FFmpeg example: https://github.com/Flam3rboy/puppeteer-stream/blob/main/examples/ffmpeg.js

const ffmpegConfig = (twitch) => {
  return `ffmpeg -i - -v error -c:v libx264 -preset veryfast -tune zerolatency -c:a aac -strict -2 -ar 44100 -b:a 64k -y -use_wallclock_as_timestamps 1 -async 1 -bufsize 1000 -f flv ${twitch}`
}

let twitch =
  'rtmp://dfw.contribute.live-video.net/app/' + process.env.TWITCH_STREAM_KEY

async function test() {
  const browser = await launch({
    executablePath:
      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    defaultViewport: {
      width: 1920,
      height: 1080,
    },
  })

  const page = await browser.newPage()
  await page.goto('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
  const stream = await getStream(page, {
    audio: true,
    video: true,
    frameSize: 1000,
  })
  console.log('recording')
  // this will pipe the stream to ffmpeg and convert the webm to mp4 format
  const ffmpeg = exec(ffmpegConfig(twitch))

  ffmpeg.stderr.on('data', (chunk) => {
    console.log(chunk.toString())
  })

  stream.pipe(ffmpeg.stdin)

  setTimeout(async () => {
    await stream.destroy()
    stream.on('end', () => {
      console.log('stream has ended')
    })
    ffmpeg.stdin.setEncoding('utf8')
    ffmpeg.stdin.write('q')
    ffmpeg.stdin.end()
    ffmpeg.kill()

    console.log('finished')
  }, 1000 * 40)
}

test()

Right before the script ends loading indicator shows up Screen Shot 2022-02-18 at 10 35 38 PM

Screen Shot 2022-02-18 at 11 01 39 PM

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
toshvelagacommented, Feb 19, 2022

ok awesome, thanks so much! Both our configs seem to be working but yours seems to be producing more reliable results i think. If I can get this to work in production I’ll right another blog post about it 😃.

Screen Shot 2022-02-19 at 2 17 37 AM
1reaction
SamuelScheitcommented, Feb 19, 2022

It works when I change the ffmpeg config to:

return `ffmpeg -i - -v error -c:v libx264 -preset veryfast -tune zerolatency -c:a aac -f flv ${twitch}`;

and remove the frameSize

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stream to twitch via ffmpeg - API - Twitch Developer Forums
But then, just today… it stopped and i always got a I/O Error from ffmpeg as soon as it came to streaming to...
Read more >
Streaming a stream from website to Twitch with FFMPEG
Is it possible to stream a website with a livestream (i.e. ip-camera) via FFMPEG to Twitch? If yes, does anybody know how to...
Read more >
Streaming an MP4 to Twitch and YouTube with FFMPEG
In this post, I discuss how I was able to 'stream' an MP4 LIVE to YouTube and Twitch using FFMPEG.
Read more >
Stream to Twitch with FFMPEG - YouTube
This is a live stream recording about how I figured out how to share my screen to Twitch with only FFMPEG. ffmpeg -f...
Read more >
Streaming to twitch.tv - ArchWiki
Twitch Broadcast Requirements ... From Twitch.tv support: Video Requirements. Codec: H.264 (x264); Mode: Strict CBR; Keyframe Interval: 2 seconds.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found