Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FFMPEG to stream my WebCam's video to YouTube [closed]

I've been trying to use ffmpeg to stream my webcam's video to YouTube and so far all I've managed was an "acknowledgement" from YouTube saying 'Starting' in green. However it quickly goes gray and the actual stream never actually changes from offline.

I'm using this command to try to stream it:

ffmpeg -rtbufsize 1500M -r 25 -f dshow -s 1280x720 -i video="USB2.0 HD UVC WebCam" -vcodec h264 -b:a 600k -acodec libfaac -ab 128k -f flv "rtmp://a.rtmp.youtube.com/live2/user.useruser.codecodecode"

CMD

I've managed to stream videos to YouTube with ffmpeg, its just the camera that won't work. Any idea what I might be doing wrong?

This is the command I'm using now ffmpeg -r 18 -f dshow -s 1280x720 -i video="USB2.0 HD UVC WebCam":audio="Microphone (Realtek High Definition Audio)" -vcodec h264 -b:v 1800k -acodec aac -strict experimental -f flv "rtmp://a.rtmp.youtube.com/live2/useruser.useruser.codecodecodecode"

I'm now using -acodec aac instead of libfaac or libfdk_aac since I was getting "Unknown encoder" errors

It is streaming a few seconds however, now I'm getting these errors: WriteN, RTMP send error 10053

Any idea what these mean, or how to fix them?

like image 753
Indes Avatar asked Nov 13 '15 02:11

Indes


3 Answers

If you have a webcamera with IP address with full HD x264 video source, without audio and you want to stream your direct camera screen to YouTube, you do not need to transcode/reencode video to a lower resolution always. YouTube can accept some camera's direct video stream. The only thing is that you must do it add an extra empty/silent audio track. If you have enough bandwith at your network it should be work.

I am using this CLI command on a Pine64+ with Ubuntu 16.04 and it is working:

ffmpeg -re -rtsp_transport tcp -i "rtsp://<user>:<pass>@<camera_ip>:<port>" /
       -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 /
       -c:a libmp3lame -ab 128k -ar 44100 /
       -c:v copy -threads 2 -bufsize 512k /
       -f flv "rtmp://a.rtmp.youtube.com/live2/<your-live-stream-key>"

(Here is some info about Libavfilter input virtual device: https://www.ffmpeg.org/ffmpeg-devices.html#lavfi)

With this, you can forward the direct camera screen with silent audio to YouTube with minimal CPU usage.

Some restrictions:

  1. When your network is down (for example your ISP refresh the network, change your public IP or your router restarted) the ffmpeg will loose the signal (webcamera and/or YouTube RTSP).
  2. As I seen sometimes YouTube stop receiving stream and convert last data into video file (archive streamed video) and after than it create new identifier for the live stream. This affects the operation of the ffmpeg (RTSP connection lost) and affects to your webpage where you inserted the YouTube live stream code. (As I found YouTube handle maximum 12 hours videos in one piece only).

In both case ffpmeg could not detect the network or streams issue. So if you would like to stream a neverending stream it is hard to automatize correctly.

  • Somehow you need to detect the network errors.
  • Somehow you need to detect when YouTube stop listening on RTSP.
  • Somehow you need to detect if ffmpeg lost communication with the camera.
  • And finally somehow you need to restart ffmpeg when all service is working again.

Temporarily I am using a small cron job at the moment which restarting ffmpeg in every hours.

like image 32
William Avatar answered Nov 06 '22 10:11

William


There are a few things wrong with your command:

  • You don't have an audio stream. When using dshow you also need to specify audio as explained here.

    ffmpeg -f dshow -i video="Integrated Camera":audio="Microphone name here"

    If you don't intend to capture audio then put a silent audio track.

  • -b:a is the same thing as -ab - the audio bitrate. You must use -b:v to specify the video bitrate. 600k is too low for a 1280x720px @ 25fps H.264 video stream.

    YouTube recommends:

    720p

    Video Bitrate Range: 1,500 - 4,000 Kbps

    You should put at least 1800k.

  • Concerning the audio you're using libfaac. For the best results when encoding AAC it's recommended to use libfdk_aac

like image 84
aergistal Avatar answered Nov 06 '22 08:11

aergistal


Live streaming an IP Camera to Youtube using FFmpeg. - For Ubuntu users (what I did)

  1. Install latest ffmpeg on ubuntu 12.04 or 14.04 https://gist.github.com/xdamman/e4f713c8cd1a389a5917 and

  2. Compile FFmpeg on Ubuntu https://gist.github.com/faleev/3435377

  3. Open Youtube. Login. Click on My Channel > Video Manager > LIVE STREAMING . Note “Stream name/key” .

  4. For static video file:

    ffmpeg -re -i "(input-file)"  -acodec libmp3lame  -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline -s 426x240 -bufsize 2048k -vb 400k -maxrate 800k -deinterlace -vcodec libx264 -preset medium -g 30 -r 30 -f flv "rtmp://a.rtmp.youtube.com/live2/(Stream name/key)"
    
  5. For streaming video from IP camera with rtsp video only output:

    ffmpeg -re -i input-file  -rtsp_transport tcp -i "rtsp://password@(streamaddress with local IP)"  -acodec libmp3lame  -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline -s 426x240 -bufsize 2048k -vb 400k -maxrate 800k -deinterlace -vcodec libx264 -preset medium -g 30 -r 30 -f flv "rtmp://a.rtmp.youtube.com/live2/(Stream name/key)"
    
like image 1
Brijesh Verma Avatar answered Nov 06 '22 10:11

Brijesh Verma