George timmermans
  • About Me
    • Resume
    • Press releases
  • Blog
  • Projects
    • Long Range WiFi Link
    • Embedded Systems >
      • Photo Booth
      • Classroom VU meter
      • USB Media Controller
      • Weather Station
      • Arduino Swimming Pool Controller
      • GRBL Android Controller
      • Arduino Serial Driver
      • Arduino Multitasking
      • 8x8x8 LED Cube
      • Analog -> Digital Geiger Counter
    • 3D Printing >
      • Simplify3d Configuration
      • 3D print repairs
    • Home Brewing >
      • Mead Recipes >
        • Cinnamon Mead
        • Sweet Chili Mead
        • Lemon & Ginger Mead
        • Mead Liqueur
        • Honey Beer
      • Glühwein (Spiced Wine)
      • Limoncello
    • House
    • Toolmaking
    • Laser Cutting
  • Weather Cam
  • Contact

Streaming RTSP to YouTube Live

15/6/2016

22 Comments

 
​I have wanted to stream the camera feeds from the cameras overlooking Picton and Waikawa since I installed them. At the time there was no easy way to do this without paying for a streaming service that would convert and host the stream for you. Now we have YouTube Live. A free host for RTMP streams. There are a few challenges that I had to overcome related to my system. For example:
  • The cameras provide RTSP streams
  • Reduce bandwidth usage because of data cap

I will quickly start with the easy ones. 

Reducing Bandwidth

I reduce the bandwidth and data usage over the course of a day in three different ways:
  • Reduced resolution from 1080p to 720p
  • Reduce 30FPS to 6FPS
  • Run cameras from 7AM to 7PM
The resolution and FPS I configured on the camera to reduce traffic within my network.

Converting RTSP to RTMP with FFMPEG

To create compatible stream formats we capture the RTSP stream and convert it into RTMP with the help of a (in my case Windows) server running ffmpeg. The following example is the command I use for my cameras. You might have to check how to capture your RTSP stream. (I purposely don't stream audio captured by the camera.)

    
A few notes:
  • In this example it's missing a valid camera username, password, IP address and Youtube stream key. I will explain how to get your Youtube stream key further on.
  • In this example you will find the extra argument "-t 12:00:00" This tells it to run for a duration of 12 hours. Remove if you want to stream continuously. 

Setting up YouTube Live

Follow the instructions here. After you're done, set up a live stream, giving it a title and a description and near the bottom of the page under "ENCODER SETUP", copy the "Stream name/key" and paste it in the ffmpeg command.

Optional - Schedule Automatic Start Time

As stated before I only stream between certain hours. As you have seen earlier in the blog post I give a duration that the stream is going to be online for. But we also need a way to start it. Because I run it on a Windows server I create a .bat file and use "Task Scheduler" to run it. 

The batch file only contains the following three lines:

    
Actually I have a "PAUSE" commented out in case you want to debug. 

To create an automated task use windows search to find "Task Scheduler".
  • ​Click tab "Action" -> "Create a Basic Task".
  • Give it a name and description. -> :"Next"
  • Trigger "Daily" -> Next
  • Set Start date and Time -> "Next"
  • Select "Start a program" -> "Next"
  • Browse and select your .bat file -> "Next"
  • Click "Finish"
  • Double click your newly created task..
  • Under tap "General" enable "Run Whether user is logged on or not"

Summary

This is missing some information but since everybody their equipment is most likely slightly different and so will be there requirements I decided to give enough info to get you started. You might have to do some research on which changes to make to make it work for you. Off course you want to see some examples.

Update: Stream links have been removed since I don't use RTSP with my latest cameras anymore. The instructions should still give you a good starting point for getting your own stream going. 
22 Comments
Ironman
6/8/2016 11:14:52 pm

This is a great post! My IP cam is working perfectly.
Realy appreciate it. Thanks!

Reply
Lambert Wixson
14/10/2016 05:02:52 pm

Nice. In my case the RTSP input also contains an audio stream. The -i anullsrc argument you show is necessary but will cause the resulting output to use that empty audio input, Input 0.

To avoid this, in addition to the arguments you show, one should specify -map 1:v:0 -map 1:a:0 on the command line.

This specifies that the audio track of the output should come from the first audio stream of Input 1 (the RTSP stream).

Reply
Matias Paz link
4/4/2017 02:58:19 am

Can you post an example with the audio file. i have no success with my cameras to upload the mic audio.

c:\ffmpeg -f lavfi -i anullsrc -rtsp_transport tcp -i rtsp://admin:admin@192.168.2.177:554/cam/realmonitor?channel=1subtype=0 -tune zerolatency -vcodec libx264 -t 12:00:00 -pix_fmt + -c:v copy -c:a aac -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/

Tank you!

Reply
autohtoni
14/2/2017 12:14:45 am

How to stream audio too from ip camera ?

Reply
George
14/2/2017 08:55:17 pm

See the comment kindly provided by Lambert Wixson.

Reply
Bob Daly
18/2/2017 07:44:24 am

Hi George,
Forgive me if this is an elementary question: I've got an IP camera and the login information (it's viewable on my laptop), and I've downloaded the FFMPEG code but I don't know what to do with it. Do I have to be running some sort of server software for this to work?
I can stream to my youtube channel easily from my DSLR via HDMI and an encoder box, but I have no idea how to get the IP camera signal into a youtube stream.
Also, are you running seval IP camera streams from one comuter, or just one?
Any help you can provide would be great as I'm trying to help a wildlife rehab set up some live cams.
Thanks
Bob Daly

Reply
George
18/2/2017 07:52:26 am

Hello Bob,

You don't need a special server OS. Put the ffmpeg folder somewhere where you can find it and update the location in the script. it is this line (windows example): cd C:\Users\XXXXX\Documents\ffmpeg\bin
One way you can create a windows script is by created a windows .text file copy and past the script in it, save it and then change the extension from .txt to .bat. Now you can run it. You will need onde script per camera.
Cheers, George

Reply
chris
19/2/2017 12:56:22 pm

Any thoughts on how to add an overlay image to the feed? png, jpeg or gif would be fine

Reply
George
19/2/2017 04:43:08 pm

If it's just a logo and you are streaming to YouTube live use the "add watermark" under the channel tab -> branding in YouTube.

Or if the camera firmware supports it, add have the camera add the logo/watermark.

Because in the script above we define that we want to copy the video feed we aren't allowed to use filters at the same time. So unless you know which coded the camera uses you won't be able to add an overlay using ffmpeg.

Reply
Алексей
2/3/2017 11:02:20 am

Ты красава Джордж!! Долго искал вменяемые флаги к ffmpeg чтобы отправить поток на Youtube. Огрномное спасибо. Подписался!

Reply
JohnnyBravo
25/4/2017 01:54:03 am

Awesome thanks! Been wanting to do the same thing with my ip cameras I am using Foscam cam that provide only RTSP streams. Can't wait to try this.

Reply
maram
16/5/2017 09:16:05 am

but it is reencoding, why? it same h264

Reply
Pierre D link
23/5/2017 04:28:32 am

First of all, thank you George for your generous information and help.

I have used your example to setup an IP camera for a project, in which the process of encoding must absolutely be autonomous and self restarting to be perfect.
My problem is, the stream is rock solid , but only for a random period of time ranging from 10 hours up to as much as 54 hours, after which it halts the terminal with a "broken pipeline" conversion failed error, for example:
(((( frame= 3588 fps= 30 q=-1.0 size= 52529kB time=00:02:00.60 bitrate=3568.0kbits/frame= 3602 fps= 30 q=-1.0 size= 52719kB time=00:02:01.09 bitrate=3566.5kbits/av_interleaved_write_frame(): Broken pipe
Last message repeated 1 times
Error writing trailer of rtmp://a.rtmp.youtube.com/live2/sfup-4bm5-fb32-4fc3: Broken pipeframe= 3603 fps= 30 q=-1.0 Lsize= 53008kB time=00:02:01.11 bitrate=3585.3kbits/s speed=1.01x
video:52819kB audio:31kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.297736%
[aac @ 0x7f86e9819200] Qavg: 65536.000
Conversion failed!

)))))

My questions is:
Can you help me or point me in a direction that will help me find a way to prevent this from happening, or find a way to automatically refresh the connection or restart it at every 30 minute interval or whatever...

Thank you in advance for ANY help

Pierre D

Reply
Ramziddin
5/7/2017 08:21:07 am

Hi George
Thanks for the helpful article.
Can you also give advice how to rewrite the code if I am using more than one camera

Reply
George
5/7/2017 08:31:42 am

Hello, for each camera I create a seperate bat file and create a new rule to start the stream.

Reply
Ramziddin
5/7/2017 08:57:15 am

Can they run simultaneously?

Richard (Rich) Luth link
12/7/2018 11:02:02 am

Do you have any help available to set up a couple IP cameras on one youtube channel?

Reply
Chila link
26/7/2018 10:31:39 pm

You should check out https://castr.io , using castr you can ingest rtsp camera feed url and you can send it to youtube.

Thanks

Reply
Aaron Techyfish link
11/11/2018 12:24:28 pm

Thank you for this write-up, I've found it very helpful in automating live IP camera streams to YouTube. I would like to add that if your IP cam doesn't have audio there's no need to create a null audio. To disregard the audio completely use the '-an' flag and also remove everything audio related, like this...

ffmpeg -rtsp_transport tcp -i "rtsp://<username>:<password>@<ip address>/cam/realmonitor?channel=1&subtype=0" -tune zerolatency -vcodec libx264 -t 12:00:00 -pix_fmt + -c:v copy -f flv rtmp://a.rtmp.youtube.com/live2/<YouTubeStreamKey>

Hope this helps!

Reply
George
11/11/2018 02:42:10 pm

Thank you, I have updated my instructions following your comment.

Reply
Aaron Techyfish link
11/11/2018 03:01:00 pm

George, sorry but for some reason I left off the "-an" flag in my above post.

Should be this...

ffmpeg -an -rtsp_transport tcp -i "rtsp://<username>:<password>@<ip address>/cam/realmonitor?channel=1&subtype=0" -tune zerolatency -vcodec libx264 -t 12:00:00 -pix_fmt + -c:v copy -f flv rtmp://a.rtmp.youtube.com/live2/<YouTubeStreamKey>

Diego Urrutia-Astorga link
7/8/2020 10:15:05 am

Watch out with the removal the audio track '-an': YouTube don't like streaming without audio track.


Your comment will be posted after it is approved.


Leave a Reply.

    Author

    George Timmermans, Research Toolmaker, Software Engineer and Tinkerer

    View my profile on LinkedIn

    Archives

    January 2021
    November 2020
    July 2020
    May 2020
    November 2019
    August 2019
    January 2019
    October 2018
    January 2018
    November 2017
    October 2017
    August 2017
    June 2017
    May 2017
    April 2017
    March 2017
    January 2017
    December 2016
    November 2016
    October 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    December 2015
    November 2015
    October 2015
    September 2015
    August 2015

    Categories

    All
    3d Printing
    Photobooth
    Wisp

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • About Me
    • Resume
    • Press releases
  • Blog
  • Projects
    • Long Range WiFi Link
    • Embedded Systems >
      • Photo Booth
      • Classroom VU meter
      • USB Media Controller
      • Weather Station
      • Arduino Swimming Pool Controller
      • GRBL Android Controller
      • Arduino Serial Driver
      • Arduino Multitasking
      • 8x8x8 LED Cube
      • Analog -> Digital Geiger Counter
    • 3D Printing >
      • Simplify3d Configuration
      • 3D print repairs
    • Home Brewing >
      • Mead Recipes >
        • Cinnamon Mead
        • Sweet Chili Mead
        • Lemon & Ginger Mead
        • Mead Liqueur
        • Honey Beer
      • Glühwein (Spiced Wine)
      • Limoncello
    • House
    • Toolmaking
    • Laser Cutting
  • Weather Cam
  • Contact