我需要提供实时图形,并且我想通过http传递一个mjpeg流(以便通过使用普通标签将图形轻松地包含在网页中)。

是否可以从多个jpeg图像实时创建一个mjpeg流?

我的策略是:

  • 输出正确的http header :
    Cache-Control:no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0
    Connection:close
    Content-Type:multipart/x-mixed-replace;boundary=boundarydonotcross
    Expires:Mon, 3 Jan 2000 12:34:56 GMT
    Pragma:no-cache
    Server:MJPG-Streamer/0.2
    

    (是从curl -I {on a mjpeg-streamer instance}获得的,但这似乎很奇怪)
  • 只需生成连续的jp​​eg图像二进制文件,请注意以下几点:
  • 在流的开头添加正确的 header (如mjpeg-streamer一样):
    Content-Type: image/jpeg
    Content-Length: 5427
    X-Timestamp: 3927662.086099
    
  • 在每个jpeg流的末尾附加边界字符串。
    --boudary--
    


  • 问题:

    你做完了吗

    您知道执行此操作的python模块吗?

    你认为这行得通吗

    你有什么建议吗?

    最佳答案

    我将其用作概念验证:https://github.com/damiencorpataux/pymjpeg

    对于内存:

    import os, time
    from glob import glob
    import sys
    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
    
    boundary = '--boundarydonotcross'
    
    def request_headers():
        return {
            'Cache-Control': 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0',
            'Connection': 'close',
            'Content-Type': 'multipart/x-mixed-replace;boundary=%s' % boundary,
            'Expires': 'Mon, 3 Jan 2000 12:34:56 GMT',
            'Pragma': 'no-cache',
        }
    
    def image_headers(filename):
        return {
            'X-Timestamp': time.time(),
            'Content-Length': os.path.getsize(filename),
            #FIXME: mime-type must be set according file content
            'Content-Type': 'image/jpeg',
        }
    
    # FIXME: should take a binary stream
    def image(filename):
        with open(filename, "rb") as f:
            # for byte in f.read(1) while/if byte ?
            byte = f.read(1)
            while byte:
                yield byte
                # Next byte
                byte = f.read(1)
    
    # Basic HTTP server
    class MyHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(200)
            # Response headers (multipart)
            for k, v in pymjpeg.request_headers().items():
                self.send_header(k, v)
            # Multipart content
            for filename in glob('img/*'):
                # Part boundary string
                self.end_headers()
                self.wfile.write(pymjpeg.boundary)
                self.end_headers()
                # Part headers
                for k, v in pymjpeg.image_headers(filename).items():
                    self.send_header(k, v)
                self.end_headers()
                # Part binary
                for chunk in pymjpeg.image(filename):
                    self.wfile.write(chunk)
        def log_message(self, format, *args):
            return
    
    httpd = HTTPServer(('', 8001), MyHandler)
    httpd.serve_forever()
    

    关于python - 在python中从jpeg图像创建mjpeg流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21197638/

    10-09 12:48