Nov 102016
 

I recently got my hands on a D-Link DCS-5222L IP Camera and thought it would be pretty cool to get it setup to do YouTube Live streaming. To configure the stream I would have to find a way to convert and encode the RTSP stream from the camera to a YouTube Live compatible RTMP stream.

The information in this post can be used for other streaming services that require an RTMP stream like Facebook!

I figured I would see if I could use VLC or FFMPEG to convert the stream. I had no luck with VLC however I found out ffmpeg should do what I needed to do. I couldn’t easily find the proper usage command and flags, however eventually after viewing multiple sites, mixing, matching, and testing, I found this command worked:

ffmpeg -rtsp_transport tcp -i rtsp://IP.CAM.ADD.RESS/live1.sdp -framerate 30 -video_size 1280x720 -vcodec libx264 -preset veryfast -maxrate 1984k -bufsize 3968k -vf "format=yuv420p" -g 60 -c:a aac -b:a 128k -ar 44100 -f flv rtmp://YOUTUBELIVE/DIR/StreamKey

Happy streaming! And if anyone can refine, or recommend better command usage, please post in the comments!

  4 Responses to “RTSP to RTMP (IP Camera to YouTube Live Streaming)”

  1. Thank you

  2. Thank you. Using you sample as a base I finally got this working with a cheap vstarcam brand IP camera

  3. Thanks! Found this and it works perfectly, just before finding what I originally was looking for which was to use gstreamer. YMMV on CPU use but on a Xeon 24 core the gstreamer approach uses less than %10. The ffmpeg uses about 400%. The ffmpeg method does look to be more accurate on playback speed. The gstreamer could probably be refined to help that.

    gst-launch-1.0 rtspsrc location=rtsp://admin:[email protected]:554//h264Preview_01_main latency=150 ! queue ! rtph264depay ! queue ! h264parse ! flvmux streamable=true name=mux ! rtmpsink location=rtmp://192.168.5.11/nhan/live

  4. ffmpeg issue
    trying to send rtsp stream to rtmp server it does the job but ffmpeg terminates after few minutes

    this is the command

    -rtsp_transport tcp -nostdin -i rtsp://admins:[email protected]:554/stream1 -framerate 60 -video_size 1920×1080 -vcodec libx264 -preset veryfast -maxrate 1984k -bufsize 3968k -vf ‘format=yuv420p’ -g 60 -c:a aac -b:a 128k -ar 44100 -f flv rtmp://a.rtmp.youtube.com/live2/STREAM_KEY

    Error log

    I/mobile-ffmpeg(29057): frame= 9961 fps= 15 q=27.0 size= 71442kB time=00:11:05.09 bitrate= 879.9kbits/s dup=0 drop=5 speed= 1x
    W/mobile-ffmpeg(29057): [flv @ 0x71ac58d600] Failed to update header with correct duration.
    W/mobile-ffmpeg(29057): [flv @ 0x71ac58d600] Failed to update header with correct filesize.
    I/mobile-ffmpeg(29057): frame= 3102 fps= 14 q=-1.0 Lsize= 37339kB time=00:03:45.06 bitrate=1359.1kbits/s dup=0 drop=5 speed=1.01x
    I/mobile-ffmpeg(29057): video:35252kB audio:1869kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
    I/mobile-ffmpeg(29057): 0.587192%
    I/mobile-ffmpeg(29057):
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] frame I:52 Avg QP:13.43 size:137821
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] frame P:2155 Avg QP:19.19 size: 12389
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] frame B:895 Avg QP:23.54 size: 2495
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] consecutive B-frames: 56.7% 10.7% 11.6% 21.0%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] mb I I16..4: 25.7% 28.3% 46.0%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] mb P I16..4: 2.2% 2.5% 0.3% P16..4: 15.9% 3.6% 2.2% 0.0% 0.0% skip:73.4%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] mb B I16..4: 0.4% 0.4% 0.0% B16..8: 5.3% 1.0% 0.1% direct: 2.0% skip:90.7% L0:43.1% L1:47.0% BI: 9.9%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] 8×8 transform intra:43.5% inter:57.3%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] coded y,uvDC,uvAC intra: 53.7% 22.3% 4.4% inter: 5.5% 2.4% 0.0%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] i16 v,h,dc,p: 46% 19% 22% 13%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 22% 28% 2% 4% 3% 3% 3% 4%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 20% 20% 3% 7% 5% 4% 4% 4%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] i8c dc,h,v,p: 59% 17% 18% 5%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] Weighted P-Frames: Y:0.8% UV:0.6%
    I/mobile-ffmpeg(29057): [libx264 @ 0x7238366300] kb/s:1285.75
    I/mobile-ffmpeg(29057): [aac @ 0x7238367200] Qavg: 65454.969
    D/flutter-ffmpeg(29057): FFmpeg exited with rc: 0
    D/ViewRootImpl[Toast](29057): hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
    I/InputTransport(29057): Create ARC handle: 0x722bad7000
    I/Choreographer(29057): Skipped 1 frames! The application may be doing too much work on its main thread.
    D/Surface (29057): Surface::connect(this=0x71ac1b1000,api=1)
    D/mali_winsys(29057): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
    D/Surface (29057): Surface::setBufferCount(this=0x71ac1b1000,bufferCount=3)
    D/Surface (29057): Surface::allocateBuffers(this=0x71ac1b1000)
    W/mobile-ffmpeg(29057): [flv @ 0x719ac23c00] Failed to update header with correct duration.
    W/mobile-ffmpeg(29057): [flv @ 0x719ac23c00] Failed to update header with correct filesize.
    I/mobile-ffmpeg(29057): frame= 9965 fps= 15 q=-1.0 Lsize= 71727kB time=00:11:05.39 bitrate= 883.1kbits/s dup=0 drop=5 speed= 1x
    I/mobile-ffmpeg(29057): video:61826kB audio:9230kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
    I/mobile-ffmpeg(29057): 0.944077%
    I/mobile-ffmpeg(29057):
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] frame I:167 Avg QP:16.00 size:211398
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] frame P:2683 Avg QP:17.38 size: 8947
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] frame B:7115 Avg QP:19.55 size: 562
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] consecutive B-frames: 3.6% 1.4% 6.5% 88.5%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] mb I I16..4: 20.9% 4.7% 74.4%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] mb P I16..4: 3.9% 2.2% 0.4% P16..4: 10.6% 2.3% 1.1% 0.0% 0.0% skip:79.6%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] mb B I16..4: 0.2% 0.1% 0.0% B16..8: 2.0% 0.2% 0.0% direct: 0.4% skip:97.1% L0:45.2% L1:52.4% BI: 2.4%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] 8×8 transform intra:19.7% inter:7.1%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] coded y,uvDC,uvAC intra: 64.4% 20.7% 3.3% inter: 1.1% 0.6% 0.0%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] i16 v,h,dc,p: 35% 18% 33% 13%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 42% 21% 27% 1% 1% 1% 1% 2% 2%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 45% 15% 20% 2% 5% 4% 3% 4% 3%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] i8c dc,h,v,p: 69% 8% 21% 2%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] Weighted P-Frames: Y:0.3% UV:0.0%
    I/mobile-ffmpeg(29057): [libx264 @ 0x71a6080100] kb/s:762.30
    I/mobile-ffmpeg(29057): [aac @ 0x71a6258000] Qavg: 61342.816
    I/mobile-ffmpeg(29057): Exiting normally, received cancel signal.
    D/flutter-ffmpeg(29057): FFmpeg exited with rc: 255
    D/Surface (29057): Surface::disconnect(this=0x71ac1b1000,api=1)

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)