Thursday, April 30, 2015

Stream RPi Camera Module Video to network, using Python

It's a example on Picamera docs - Recording to a network stream , to stream video of Raspberry Pi Camera Module to network using Python.

import socket
import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 24

    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 8000))
    server_socket.listen(0)

    # Accept a single connection and make a file-like object out of it
    connection = server_socket.accept()[0].makefile('wb')
    try:
        camera.start_recording(connection, format='h264')
        camera.wait_recording(60)
        camera.stop_recording()
    finally:
        connection.close()
        server_socket.close()


Run the Python on Raspberry Pi

View on desktop using VLC

In this scenario, the Pi acts as the server, waiting for a connection from the client. When it accepts a connection, it starts streaming video over it for 60 seconds.

Bo script is needed on the client side - we can simply use VLC with a network URL:

$ vlc tcp/h264://my_pi_address:8000/


There 2~3 seconds delay in my test on Raspberry Pi 2.

3 comments:

Unknown said...

Hi

Very good your post, I have a question, I can take this otherwise served to embed the streaming in an application, for example, an Apache Cordova.
I hope you answer my question
Thank you

Unknown said...

What python modules are needed to be loaded ?

apt-get what ?

Thanks

Unknown said...

how to view in browser