Opencv流媒体网址python

时间:2018-03-08 23:10:31

标签: python opencv stream

我想用opencv处理流式网址,所以请阅读它。 首先,我尝试阅读并在窗口中显示视频,但我无法做到...... 我尝试了几种方法,但都没有效果:

1。文件缓冲

此方法包括使用其他库播放视频,将其保存到文件并使用opencv实时读取

问题:Opencv无法打开文件并引发错误

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fa6f3825a00] moov atom not found
OpenCV: Couldn't read video stream from file "tmp.mp4"

2。使用opencv打开流网址

此方法包括直接使用Opencv打开网址流。它工作但视频不是以正常速度播放,而是以数据输入的速度读取,因此如果视频质量较低(数据较少),则加载速度更快,读取速度更快。 ..

另外,我在打开时收到错误:

[AVBSFContext @ 0x7fe9f71e6920] Invalid NAL unit 0, skipping.

第3。使用Opencv解码字节

此方法包括使用外部库读取数据,然后使用opencv解码数据。但Opencv永远无法解码图像。

这是我的代码:

import numpy as np
import cv2
import time
import requests
import streamlink


class Cam:

    def __init__(self, url):
        self.stream = None
        self.stream_url = url

    def choose_stream(self):
        print("Loading stream...")
        streams = streamlink.streams(self.stream_url)
        stream = streams['360p']
        print("Loaded !")
        return stream

    def show_img(self, ret, img):
        try:
            if ret:
                cv2.imshow('result', img)
        except:
            print("DERP")

    def run_with_file_buffer(self):
        self.stream = self.choose_stream()
        buffer_file_name = "tmp.mp4"
        video_file = open(buffer_file_name, "wb")
        fd = self.stream.open()
        for i in range(0, 512):
            if (i % 256) == 0:
                print("Buffering {i}...".format(i=i), end="\r")
            new_bytes = fd.read(1024)
            video_file.write(new_bytes)
        print("Done buffering.")

        cam = cv2.VideoCapture(buffer_file_name)
        while True:
            new_bytes = fd.read(1024 * 16)
            video_file.write(new_bytes)
            ret, img = cam.read()
            self.show_img(ret, img)
            if cv2.waitKey(10) == ord('q'):
                break
        video_file.close()
        fd.close()

    def run_with_stream_url(self):
        self.stream = self.choose_stream()
        print("Reading : " + self.stream.url)
        cam = cv2.VideoCapture(self.stream.url)
        if not cam.isOpened():
            print("Error opening video stream or file")
        while cam.isOpened():
            ret, img = cam.read()
            self.show_img(ret, img)
            if cv2.waitKey(10) == ord('q'):
                break

    def run_with_bytes(self):
        self.stream = self.choose_stream()
        fd = self.stream.open()
        data = b''
        while True:
            data += fd.read(1024)
            a = data.find(b'\xff\xd8')
            b = data.find(b'\xff\xd9')
            if a != -1 and b != -1:
                img_data = data[a:b + 2]
                data = data[b + 2:]
                np_data = np.frombuffer(img_data, dtype=np.uint8)
                # np_data = np.fromstring(img_data, dtype=np.uint8)
                if len(np_data) != 0: # Always false
                    img = cv2.imdecode(np_data, cv2.IMREAD_COLOR)
                    if img:
                        cv2.imshow('cam', img)
            if cv2.waitKey(10) == ord('q'):
                break


if __name__ == "__main__":
    url = 'https://www.twitch.tv/{id}'
    cam = Cam(url)
    cam.run_with_bytes()
    cv2.destroyAllWindows()

0 个答案:

没有答案
相关问题