0

I am trying to stream Raspberry Pi camera video using VLC. The code is as follows:

import subprocess
import picamera
camera = picamera.PiCamera()
camera.resolution = (640,480)
cmdline = ['cvlc','-vvv','stream:///dev/stdin','--sout','#rtp{sdp=rtsp://:8554/}',':demux=h264' ]
myvlc = subprocess.Popen(cmdline, stdin=subprocess.PIPE)
camera.start_recording(myvlc.stdin, format = 'h264')
camera.wait_recroding(60)
camera.stop_recording()
myvlc.stdin.close()
myvlc.wait()

However, the code does not produce the desired output. Please guide me what's wrong with this code snippet.

0

Do you have a debugger?

Are you using an IDE?

Check out Spyder from Anaconda

If you're a a beginner maybe Thonny

But, try breaking your code up into functions, so you more easily diagnose where the problem is. Try adding:

stdout=subprocess.PIPE, stderr=subprocess.PIPE

to you Popen() arguments, and read their outputs with

print('pass through:', repr(stdout_value.decode('utf-8'))) print('stderr :', repr(stderr_value.decode('utf-8')))

you might also want to try passing shell=True to Popen.

0

I think this is my first solution to stack overflow instead of a question :).

What worked for me was to type your command as a string instead of a list, then use shlex.split() on that string for the Popen command. If anyone can explain why shlex works where techraf's code doesn't, please do as I got here through mostly blind trial and error.

import subprocess
import picamera
import json
import shlex

vlcCommand="cvlc -vvv  stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst:8080}' :demux=h264"
conf = json.load(open('conf.json'))

with picamera.PiCamera() as camera:
   camera.resolution = tuple(conf["resolution"])
   camera.framerate = conf["fps"]
   camera.vflip = True
   camera.hflip = True
   cvlc = subprocess.Popen(shlex.split(vlcCommand), stdin=subprocess.PIPE)
   camera.start_recording(cvlc.stdin, format=conf["codec"])
   camera.wait_recording(30)
   camera.stop_recording()

#Connect to network stream on VLC with http://ip:8080 on client
0

This is my first answer I have used this on my raspberry pi and there is almost no noticeable latency when less than 2 meters away from router, here is the code I got it from this https://randomnerdtutorials.com/video-streaming-with-raspberry-pi-camera/ I would make my own script but I have been focusing mostly on setting it up. If I am doing anything wrong in this answer please tell me and I will try and correct anything I have done wrong. This is my first answe on stack exchange. This could be an alternative to vlc which when I have tried has really high latency

# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming

import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

PAGE="""\
    <html>
    <head>
    <title>Raspberry Pi - Surveillance Camera</title>
    </head>
    <body>
    <center><h1>Raspberry Pi - Surveillance Camera</h1></center>
    <center><img src="stream.mjpg" width="640" height="480"></center>
    </body>
    </html>
"""

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True

with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
    output = StreamingOutput()
    #Uncomment the next line to change your Pi's Camera rotation (in degrees)
    #camera.rotation = 90
    camera.start_recording(output, format='mjpeg')
    try:
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)
        server.serve_forever()
    finally:
        camera.stop_recording()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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