Select Sidearea

Populate the sidearea with useful widgets. It’s simple to add images, categories, latest post, social media icon links, tag clouds, and more.

[email protected]
+1234567890

Stream live webcam to YouTube using Node js

Stream live webcam to YouTube using Node js

Requirement:

  1. Youtube channel
  2. Youtube stream key
  3. Node

 

Steps to get youtube stream key

 

1) Go to youtube and click on Go Live as shown in the image below:

Go live

 

2) Copy the stream key. We need to use this in our project.

Stream key

 

Steps to create a node project.

 

1) Create a folder and open it in terminal. Run the following commands.

npm init

npm install --save @ffmpeg-installer/ffmpeg

2) Open the code in an editor. Create a file and name it server.js

3) Enter the following code. We will use ffmpeg to stream webcam. It takes input, name of the video and audio device of our system.

const spawn = require('child_process').spawn,
{ exec } = require('child_process');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;

const args = ['-loglevel', 'debug', '-threads:v', '2', '-threads:a', '8', '-filter_threads', '2', '-thread_queue_size', '512', '-f', 'dshow', '-i', 'video=YOUR_VIDEO_DEVICE_NAME','-f', 'dshow', '-i','audio=YOUR_AUDIO_DEVICE_NAME', '-pix_fmt', 'yuv420p', 'c:v', 'libx264', 'qp:v', '19','-profile:v', 'high', '-rc:v', 'cbr_ld_hq', '-level:v','4.2', '-r:v', '60', '-g:v', '120', 'bf:v','3', '-refs:v','16', '-f', 'flv', 'rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY'];

var proc = spawn(ffmpegPath, args);
proc.stdout.on('data', function(data) {
     console.log(data);
});
proc.stderr.setEncoding("utf8")
proc.stderr.on('data', function(data) {
    console.log(data);
});
proc.on('close', function() {
    console.log('finished');
});

You need to provide your audio and video details in the code. For example: ‘video=HP TrueVision HD Camera’ and ‘audio=Microphone (Realtek High Definition Audio)’ .

4) Open the terminal and enter the following command to run your project.

node server.js

To learn more about ffmpeg please refer this site.

admin
No Comments

Post a Comment