Python MJPEG流在VLC外部不起作用

时间:2015-07-20 10:31:48

标签: python http stream vlc mjpeg

我正在从一些IP摄像机创建流媒体mjpeg,使用VLC的流媒体工作完美,只是不在网页和其他企业设备中工作,其他MJPEG流从互联网上完美运行

这是我的代码:

class Streamer(Thread):
    def __init__(self, *args, **kwargs):
        self.container = kwargs.pop("container", None)
        # self.q = kwargs.pop("queue", None)
        # self.stream = kwargs.pop("stream", None)
        super(Streamer, self).__init__(*args, **kwargs)



    def run(self):
        try:
            print("dichiarazione handler")
            server = ThreadedHTTPServer(('192.168.1.182', 8080), HandlerArguments(self.container))

            print('started httpserver...')
            server.serve_forever()
        except KeyboardInterrupt:
            print('^C received, shutting down server')
            server.socket.close()

def HandlerArguments(init_args):
    class MyHandler(BaseHTTPRequestHandler):
        def __init__(self, *args, **kwargs):
            self.container = init_args
            super(MyHandler, self).__init__(*args, **kwargs)

        def do_GET(self):
            print("GET")
            if self.path.endswith('.mjpeg'):
                self.container.stream = True
                self.send_response(200)
                self.send_header('Pragma:', 'no-cache')
                self.send_header('Cache-Control:', 'no-cache')
                self.send_header('Content-type:', 'multipart/x-mixed-replace;boundary=--myboundary')
                self.end_headers()
                try:
                    while self.container.stream == True:
                        # print(self.queue.qsize())
                        print("ciclo")
                        img = self.container.finalq.get()
                        ret, img_jpg = cv2.imencode(".jpeg", img, (cv2.IMWRITE_JPEG_QUALITY, 90))
                        self.wfile.write('--myboundary'.encode('utf-8'))
                        self.send_header('Content-type:','image/jpeg')
                        # self.send_header('Content-length:', str(len(img_jpg)))
                        self.end_headers()
                        img_str = img_jpg.tostring()
                        self.wfile.write(img_str)
                        # self.wfile.write('--myboundary\r\n'.encode('utf-8'))
                        # self.send_response(200)
                        # time.sleep(0.05)
                except:
                    self.container.stream = False
                    while not self.container.finalq.empty():
                        self.container.finalq.get()
            elif self.path.endswith('.jpg'):
                self.send_response(200)
                self.send_header('Pragma:', 'no-cache')
                self.send_header('Cache-Control:', 'no-cache')
                self.send_header('Content-type:','image/jpeg')
                self.end_headers()
                img = self.container.finalq.get()
                ret, img_jpg = cv2.imencode(".jpeg", img, (cv2.IMWRITE_JPEG_QUALITY, 90))
                # self.send_header('Content-length:', str(len(img_jpg)))
                img_str = img_jpg.tostring()
                self.wfile.write(img_str)


    return MyHandler

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
#class ThreadedHTTPServer(HTTPServer):
    """Handle requests in a separate thread."""

我认为问题出在请求标题中,特别是这是从我的流中获取的一个流来自

curl -I http://85.90.40.19/mjpg/video.mjpg
HTTP/1.0 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Connection: close
Content-Type: multipart/x-mixed-replace; boundary=--myboundary
///////
curl -I http://192.168.1.182:8080/stream.mjpeg
HTTP/1.0 501 Unsupported method ('HEAD')
Server: BaseHTTP/0.6 Python/3.4.0
Date: Mon, 20 Jul 2015 10:00:54 GMT
Content-Type: text/html;charset=utf-8
Connection: close
Content-Length: 474

特别是在我的要求中,我看到了:

Content-Type: text/html;charset=utf-8

在我的代码中运行

self.send_header('Content-type:','image/jpeg')

先谢谢你们:)

1 个答案:

答案 0 :(得分:0)

curl -I发送 HTTP HEAD 请求。

在您的代码中,您只处理 HTTP GET 请求:

def do_GET(self):

请参阅错误消息:HTTP/1.0 501 Unsupported method ('HEAD')

处理 HEAD 请求:

def do_HEAD(self):

并输出预期的标题,因为客户端可能会在GET之前发出HEAD。