Caltrop
Caltrop

Reputation: 935

Streaming a programatically created video to youtube using node and ffmpeg

I've been trying to Livestream a programmatically created image to youtube using node. I've had very limited success using FFmpeg. While I have managed to create and save an image thanks to this insightful thread, I have yet to make the code work for streaming to an RTMP server.

const cp = require('child_process'),
    destination = 'rtmp://a.rtmp.youtube.com/live2/[redacted]', //stream token redacted
    proc = cp.spawn('./ffmpeg/bin/ffmpeg.exe', [
        '-f', 'rawvideo',
        '-pix_fmt', 'rgb24',
        '-s', '426x240',
        '-i', '-', //allow us to insert a buffer through stdin
        '-f', 'flv',
        destination
    ]);

proc.stderr.pipe(process.stdout);

(function loop() {
    setTimeout(loop, 1000 / 30); //run loop at 30 fps
    const data = Array.from({length: 426 * 240 * 4}, () => ~~(Math.random() * 0xff)); //create array with random data
    proc.stdin.write(Buffer.from(data)); //convert array to buffer and send it to ffmpeg
})();

When running this code no errors appear and everything appears to be working, however, YouTube reports that no data is being received. Does anybody know what is going wrong here?

Update: This is really counter-intuitive but adding a slash to the destination like this 'rtmp://a.rtmp.youtube.com/live2/[redacted]/' causes ffmpeg to throw a generic I/O error. This is really weird to me. Apologies if the answer to this is obvious, I'm really inexperienced with ffmpeg.

Upvotes: 4

Views: 2089

Answers (1)

llogan
llogan

Reputation: 133713

This is specifically a ffmpeg issue, so the node and javascript stuff is an unnecessary complication (unless ffmpeg isn't even executing). It's best to get the ffmpeg command working in a command-line interface before adding it to any code.

  • YouTube requires an audio stream, so add -re -f lavfi -i anullsrc input option to generate silent audio.

  • Output must be YUV 4:2:0 for live streaming, so add -vf format=yuv420p output option.

  • It is also recommended to add -g, -b:v, -maxrate, and -bufsize output options when streaming (examples).

  • Set the encoders with -c:v libx264 -c:a aac output options to make sure it doesn't automatically choose the crappy, old encoder named flv1 which is the default for flv muxer (-f flv).

Upvotes: 5

Related Questions