118

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.

3
  • did you try mjpgstreamer? Shell I post the solution?
    – jaromrax
    Apr 29, 2015 at 7:32
  • 4
    People who arelooking for zero latency solution should check raspberrypi.stackexchange.com/a/32521/30681 Nov 7, 2016 at 17:10
  • 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). Nov 23, 2018 at 21:37

15 Answers 15

91

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

13
  • 2
    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.
    – Jaime M.
    May 7, 2015 at 20:01
  • 8
    In the provided link, instead of autoreconf -vfi you should now use ./bootstrap.sh
    – JuJoDi
    Jun 1, 2015 at 16:35
  • 1
    Is there a way that will allow me to both stream the video and also save it on the RPi SD card? Dec 1, 2015 at 13:03
  • 4
    The videolan.org/doc/streaming-howto/en/ch03.html link is dead.
    – Kal
    Jul 2, 2016 at 1:55
  • 6
    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
    Jan 11, 2017 at 19:52
45

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)

3
  • 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!
    – Aurora0001
    Aug 25, 2017 at 10:18
  • @Per Hansen Not working for me...There might be something else that needs to be done apart from assigning a port.
    – Amit Ray
    Oct 30, 2017 at 17:39
  • 2
    This works great ! Using a RPi3B+ and Raspbian 9.4 (stretch) headless.
    – dparkar
    Jul 13, 2018 at 5:51
42

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.

7
  • 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
    Mar 7, 2015 at 22:54
  • @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.
    – Diego
    Apr 23, 2015 at 12:25
  • @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/ Nov 5, 2016 at 6:04
  • Thanks @Diego this worked great for me but how do I quit my stream?
    – Denoteone
    Jan 14, 2017 at 16:27
  • 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.
    – Mostafa
    May 30, 2017 at 23:01
32

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 :)

9
  • 6
    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. Nov 7, 2016 at 16:57
  • 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.
    – Nick
    Jan 11, 2017 at 20:46
  • This is awesome! Apr 9, 2017 at 16:29
  • 1
    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.
    – Gabber
    Jun 18, 2017 at 19:10
  • woah, i must admit this one rocks! Jan 20, 2018 at 22:08
9

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
4
  • what about the time lag?
    – kuhajeyan
    Jun 28, 2016 at 17:43
  • 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).
    – MoonCactus
    Sep 15, 2016 at 7:06
  • 1
    oh, and it works pretty well (lightweight solution), thanks. Time lag is decent, about 2 seconds si it compares favorably with other solutions imho.
    – MoonCactus
    Sep 15, 2016 at 7:14
  • I agree with @MoonCactus with an RPi2B I was able to stream a 1920x1080@30 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.
    – SlySven
    Apr 3, 2020 at 13:59
5

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.

1
  • 1
    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.
    – Svartalf
    Oct 29, 2016 at 13:02
3

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):

# stream with default options (port=8080) (works for USB cameras too)
./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so"

# set resolution to 1920x1080 and 180° rotation
./mjpg_streamer -i "./input_uvc.so -rot 180 -r 1920x1080" -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.

If the input_uvc.so driver is not working for you or you don't want to configure the raspberry pi camera as a UVC device, then you can also try the input_raspi.so driver.

Custom config options supported by each driver:

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 # when 'on' others hosts will NOT see the server
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.

1

I don't know if you are looking for a surveillance software, but I have succeeded in installing and running motion, a software motion detector. You should have a look in the official guide.

2
  • Motion only applies to USB cameras
    – geneorama
    Feb 21, 2015 at 20:44
  • 3
    @geneorama motion use video4linux interface that is not restricted to USB camera, the bcm2835-v4l2 module give access to the camera board through this interface.
    – mpromonet
    Apr 13, 2015 at 6:42
1

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.

2
  • Please include relevant parts of the tutorial here, as to prevent link rot.
    – Bex
    Jul 12, 2015 at 19:09
  • 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...
    – Svartalf
    Oct 29, 2016 at 13:00
1

I was having trouble getting the video stream on windows 7. There were many linux solutions, not I couldn't replicate them on windows until now. I got a pretty quick solution eventually:

  1. get a windows version of netcat: http://joncraton.org/files/nc111nt.zip, unzip it and add to path
  2. get a new version of mpc-hc, which may read from stdin: https://mpc-hc.org/downloads/ install and add its path to path as well (mine is 64-bit, but 32-bit will hopefully do as well)

On raspberry pi:

raspivid -t 0 -w 1024 -h 768 -fps 20 -b 1000000 -o - | nc your.receiver.ip.address 5001

On windows machine:

nc -l -p 5001 | mpc-hc64 -

Thus, i deem, nothing gets repacked or restreamed, remuxed or what so ever. Binary stream from raspvid is feeded into mpc and shown. My raspberry operates over wifi, so at first I get a fair delay, which is in a matter of half a minute reduced to those 0.2-0.3 seconds delay.

Unfortunately, I couldn't use vlc in the same manner, as nc -l -p 5001 | vlc - just opens vlc window without any video.

1

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

2
  • Good one, would you share about any experience video time lag?
    – kuhajeyan
    Jun 28, 2016 at 17:55
  • 2
    Yes, there was a lag of about 5 seconds.
    – TTT
    Jul 25, 2016 at 10:07
1

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

0

While Peter Hansen's code quite possibly works fine on a LAN, for completeness sake : TCP is about data integrity as against real-time. In a real-time stream some data loss may be acceptable for the sake of timely display. In this case UDP may be better. I haven't tested that code.

If it is a security camera, multicasting may be considered. Modern routers can do it even the $100 models.

0

A while a go had a similar problem came up with this:

https://www.npmjs.com/package/ws-avc-player

Its more barebone than other proposed solutions, but I think it works well if low latency over web is the priority.

Works on most browsers (even mobile at 480p) without any plugins needed on the browser.

5
  • I did not write the decoder itself, its a separate project github.com/mbebenita/Broadway However is surprisingly well optimised, and as I said in the answer it can easly handle a 480p on a droid. The reason I went with Brodway is because there is no vp8/9 hardware encoder on rpi so h264 is really the only decent option. But if you want to use native h264 decoder in browser you can forget the low latency.
    – MatijaG
    Mar 7, 2018 at 10:52
  • It sounds so incredible that I have to give it a try.
    – kwasmich
    Mar 7, 2018 at 11:02
  • Any feedback will be very welcome :)
    – MatijaG
    Mar 7, 2018 at 11:03
  • Only now I had some spare time. I'm having trouble getting the example to run with the raspivid camera feed. With ffmpeg on the other hand I'm just getting tenth of frames per second. I used a clean Raspbian and a Pi Zero. Do you have some hints for me?
    – kwasmich
    Apr 18, 2018 at 19:45
  • Yeah with ffmpeg will be a no-go becaouse of lack of hardware acceleration. Try running the example with: node example/index.js raspivid It should work out of the box if you have issues try running just node example/index.js and separately /opt/vc/bin/raspivid -pf baseline -ih -t 0 -w 640 -h 480 -hf -fps 15 -g 30 -o - | nc localhost 5000 and see if you get any errors from raspivid
    – MatijaG
    May 24, 2018 at 8:20
0

Try my lightweight python solution. It uses the camera's V4L2 h264 stream, adds some (f)mp4 header and streams it over http.

https://github.com/soyersoyer/fmp4streamer

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Chenmunka
    Oct 9, 2021 at 9:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.