本文介绍了在python中从jpeg图像创建一个mjpeg流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供实时图表,我想通过http传递一个mjpeg流(因此很容易通过使用普通标记将图表包含在网页中)。

I need to serve real-time graphs and I would like to deliver a mjpeg stream over http (so that it is easy to include the graphs in a web-page by using a plain tag).

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

Is it possible to create an mjpeg stream from multiple jpeg images, in realtime ?

我的策略是:


  1. 输出正确的http标头:

  1. Output the correct http headers:

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 {在mjpeg-streamer实例上获得} ,但这看起来很奇怪)

(got it from a curl -I {on a mjpeg-streamer instance}, but this seems strange)

简单地生成连续的jp​​eg图像二进制文件,注意:

Simply yield the successive jpeg images binaries, taking care to:


  • 在流的开头添加正确的标题(如mjpeg-streamer那样):

  • prepend the correct headers at the beginning of the stream (as mjpeg-streamer does):

Content-Type: image/jpeg
Content-Length: 5427
X-Timestamp: 3927662.086099


  • 在每个jpeg流的末尾附加边界字符串。

  • append the boundary string at the end of each jpeg streams.

    --boudary--
    







  • 问题:

    你做过吗,

    你知道一个python模块吗?

    do you know a python module that does that,

    你觉得它会起作用,

    你有什么建议吗?

    推荐答案

    我认为它是一个概念验证:

    I got it working as a proof-of-concept: 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中从jpeg图像创建一个mjpeg流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-12 14:55