How to stream video from Raspberry Pi camera and watch it live

  • I am new to the Raspberry Pi and even new to the Raspberry Pi camera. I have seen a lot of tutorials online for the Raspberry Pi camera but have yet to find on that lets me stream straight from the Raspberry Pi camera like you can with a webcam (using SimpleCV or OpenCV). Was hoping someone could point me in the right direction.

    did you try mjpgstreamer? Shell I post the solution?

    People who arelooking for zero latency solution should check http://raspberrypi.stackexchange.com/a/32521/30681

    If you have ssh and mpv: `ssh raspberrypi raspivid -o - -t 0 -fps 25 -w 640 -h 360 | mpv - --speed 2 --fps 25` (the "--speed 2" keeps it from lagging too much).

  • Lao

    Lao Correct answer

    6 years ago

    The solution suggested by Diego is good except that it's pretty slow and has a huge video delay since the vlc there re-streams a stream of the raspvid. Since 12/2013 there is an official v4l2 driver available: http://www.ics.com/blog/raspberry-pi-camera-module#.VJFhbyvF-b8 This renders the mentioned re-streaming obsolete. Simply load the module and use it:

    sudo modprobe bcm2835-v4l2
    cvlc v4l2:///dev/video0 --v4l2-width 1920 --v4l2-height 1080 --v4l2-chroma h264 --sout '#standard{access=http,mux=ts,dst=0.0.0.0:12345}'
    

    This creates an http stream at port 12345, you can use other formats too, like the rtcp one from the Diego's answer. Read more on it here: https://web.archive.org/web/20151012014829/http://www.videolan.org:80/doc/streaming-howto/en/ch03.html

    In this answer, it's suggested to add `:demux=264` to cvlc because the stream is RAW h264 and you need to set the demuxer: `cvlc v4l2:///dev/video0 --v4l2-width 1920 --v4l2-height 1080 --v4l2-chroma h264 --sout '#standard{access=http,mux=ts,dst=0.0.0.0:12345}' :demux=264`. That's make possible to watch the stream in VLC for Android, for example.

    In the provided link, instead of `autoreconf -vfi` you should now use `./bootstrap.sh`

    @Lao is there any good way to reduce the lag?

    Is there a way that will allow me to both stream the video and also save it on the RPi SD card?

    Doesn't work for me on RPi 3 B :( (port is open, but no stream is being transferred, vlc do nothing, browser downloads empty file).

    This does work. However for me (using RPi B+ wheezy), it's way slower than Diego's method. This is round 3-4 secs lag whereas the latter is 2 sec. What am I doing wrong? (For others: even though it creates a http stream, I have to use VLC media stream to view it. Chrome or IE tries to download a file)

    I think it might be worth noting / confirming that this method does not work in a headless environment. I am getting errors, one of which talks about X11

    @Nick I tried on a headless environment and it works, errors talking about x11 usually are only saying that no graphical interface will be loaded, however the lag fot me is about 2s, unusable for a drone webcam

    is that 2nd command meant to be `vlc` instead of `cvlc`?

    This is an official about link for installing v4l2 driver https://git.linuxtv.org/v4l-utils.git/about/

  • There are several options you can choose between. At my work we are using VLC to stream video captured by Raspberry Pi Camera from our server-rooms to the office. One downside of this is that there are about 5 seconds delay and I haven't found a solution to this. The following is our setup:

    1. Have raspbian installed and updated and make sure your camera is enabled (you enable this by running sudo raspi-config and choose Enable Camera)

      sudo apt-get update
      sudo apt-get upgrade
      
    2. Install vlc

      sudo apt-get install vlc
      
    3. Create a script to start the stream with the following content, or run the command (you can't do anything else if you just run the command).

      sudo nano myscript.sh
      raspivid -o - -t 0 -hf -w 640 -h 360 -fps 25 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264
      
    4. Make the script runable

      sudo chmod +x myscript.sh
      
    5. If you want to start the stream automatically you have to add the script to crontab. To make this work I had to make another script runned by cron (OBS! VLC can't be run as sudo so make sure you're in the right cron). sudo nano myscript2.sh:

      #!/bin/bash
      /path/to/myscript.sh
      

      Then:

      sudo chmod +x myscript2.sh
      crontab -e
      @reboot /path/to/myscript2.sh
      
    6. To watch the videostream, open VLC on a computer on the same network as the raspberry pi you are using for streaming. Press Media -> Open Networkstream and paste the following in the field:

      rtsp://[IP].[TO].[THE].[PI]:8554/
      

    If you don't care about FPS (frames per second) and don't want any delay you could use MJPEG. You can read more about this HERE

    Watch THIS wiki about Raspberry Pi Camera Module. Hope you find what you're looking for.

    Any idea why I would be getting the following on VLC on Windows ? Your input can't be opened: VLC is unable to open the MRL 'rtsp://192.168.1.78:8554'. Check the log for details. The Pi-side seems to be ok - no errors and seems to reacting to the connected client?

    @monojohnny Is the raspberry pi streaming? Check if the video is actually working by plugging in a screen. If video is working you should see the stream on the screen plugged to the raspberry pi.

    @monojohnny Thanks for the write-up Diego... works perfectly on the first try! monojohnny ... you have to put the trailing "/" otherwise vlc complains. Try this: rtsp://192.168.1.78:8554/

    Thanks @Diego this worked great for me but how do I quit my stream?

    I have the same problem on windows and i am not able to stream the video from my pi. given i see the video streaming from pi camera on a screen connected to the pi but not through VLC installed on my windows 10 pc.

    Awesome. Works for me first try! Raspberry 3

    Just does not work. Rpi3B+ VLC on the client side says "VLC is unable to open the MRL 'rtsp://192.168.137.21:12345'. Check the log for details.". Port is connectable, RPI shows no errors.

  • I tried some options. However, I found the best result using that project: RPi-Cam-Web-Interface from eLinux community

    the stream has almost zero delay.

    good luck :)

    People looking for zero latency, check this for sure. Props to @user2998302. I can't believe, for one year this no upvotes(acknowledgment) on this.

    While this is a quick and easy setup, the software is far from optimized, so while its low latency (no such thing as zero latency), the interface is very chatty, and uses some extra CPU cycles. Not a problem if you aren't running anything else, but could be if you are, especially on a Pi 1.

    This is awesome!

    Fastest web video streaming so far among the ones i've tested (also native v4l2 driver streamed via vlc), almost zero latency. No point in complaining about optimization while every resource on the internet regarding this problem won't get you any better result than this application does.

    woah, i must admit this one rocks!

    This is the easiest one to configure, and has amazing performance. Thanks.

    Great, would be even better as a docker container

  • The easiest way I have found, to get a live videofeed from an headless raspberry pi, with a raspberry pi camera.

    This solution works right out of the box, without installing additional software on the PI.

    On the PI:

    raspivid -t 0 -l -o tcp://0.0.0.0:3333
    

    On the Computer, one can stream with VLC:

    vlc tcp/h264://192.168.66.154:3333
    

    (assuming 192.168.66.154 is the PI's IP address)

    Welcome to Raspberry Pi! We generally discourage copying your answers verbatim from one question to another—once you reach 15 rep, you will be able to flag questions that are identical as 'duplicate' so that there's only one copy your answer. Thanks for sharing your answer to the problem, though!

    @Per Hansen Not working for me...There might be something else that needs to be done apart from assigning a port.

    This works great ! Using a RPi3B+ and Raspbian 9.4 (stretch) headless.

  • I posted a possible solution in raspberry forum using a lightweight RTSP server based on live555 that is fed with a V4L2 driver supporting H264 capture :

    sudo modprobe -v bcm2835-v4l2
    git clone https://github.com/mpromonet/v4l2rtspserver.git
    cd v4l2rtspserver
    cmake .
    make install
    v4l2rtspserver -H <height> -W <width> -F <fps>
    

    Next you should be able to access to the RTSP stream using :

    vlc rtsp://<raspberry>:8554/unicast
    

    what about the time lag?

    cmake complained about "Could NOT find ALSA (missing: ALSA_LIBRARY ALSA_INCLUDE_DIR)", until I added a few more packages for my project. Solution is within these: "sudo apt-get install libopus-dev libexpat1-dev libasound2-dev libudev-dev libavahi-client-dev" (my guess is asound may be enough here).

    oh, and it works pretty well (lightweight solution), thanks. Time lag is decent, about 2 seconds si it compares favorably with other solutions imho.

    I agree with @MoonCactus with an RPi2B I was able to stream a [email protected] image with about 2 seconds lag, I noted that my (wired-LAN) network traffic got up to about 20Mbps but that shouldn't be a problem on most home Networks I guess.

  • UV4L (also) supports standard HTML5 video and audio streaming with no configuration required. Other options are possible too, like MJPEG over http.

    Update:

    UV4L now also supports live audio & video broadcasting to Jitsi Meet Rooms over the Web. No special configuration is required. It's as easy as filling your name, room and clicking on Start.

    2nd Update:

    UV4L now supports H264 hardware-encoded video too, at full fps, HD and possibly Full HD in the future.

    Only thing is, you're only able to get the elemental stream which pretty much everything DOESN'T understand. Not much use, sadly. You need something to emit WebRTC (Which if you've got a Pi2 and later, you can with UV4L. If you're needing (because of power/size constraint) a Zero to do this, your SOL.

  • Both mjpeg_streamer and motion seem to have better latency than cvlc.

    Using mjpg_streamer

    mjpeg_streamer requires compilation but the instructions on the github are straightforward.

    After compilation it can be run by launching start.sh from the project dir, or by running directly (in the project dir):

    ./mjpg_streamer -i "./input_uvc.so -rot 180" -o "./output_http.so -w ./www -p 8090"
    

    where 8090 is the TCP port. If not specified, then port 8080 will be used by default. The start.sh script contains some usage examples with extra options.

    Using motion

    Motion includes a systemd service which makes it easy to autostart at boot, stop or restart.

    To use it in IP webcam mode the following settings should be set in /etc/motion/motion.conf:

    daemon on
    stream_localhost off
    output_pictures off
    ffmpeg_output_movies off
    stream_maxrate 24
    framerate 24
    width 640
    height 480
    

    Next, in /etc/default/motion set:

    start_motion_daemon=yes
    

    To make the service autostart on boot:

    systemctl enable motion
    

    Use systemctl start/stop/restart motion for the corresponding action.

    Default streaming port is 8081. The port is set under stream_port in /etc/motion/motion.conf.

  • I've been able to stream video using uv4l WebRTC on Raspberry Pi 2. It's super easy. Just follow this tutorial. I hope this help.

    Please include relevant parts of the tutorial here, as to prevent link rot.

    Only applicable on the Pi2 and above. Doesn't work with a 1A/1A+/Zero. Not wholly sure why he can't since people seem to be able to do it, but hey...

  • I used Nginx with RTMP support for streaming to the world, together with avconv for processing camera input. Code is available on Github.

    Good one, would you share about any experience video time lag?

    Yes, there was a lag of about 5 seconds.

  • I have installed an ONVIF solution from https://github.com/BreeeZe/rpos on my pi, following the instructions on README.md.

    The only difference is that I have installed node v8.4.0. This allows me to see my camera output in a NVR connected on same network. Also, camera can be directly played through VLC player network stream using address rtsp://192.168.1.3:8554/h264

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM