kkroening/ffmpeg-python

pull rtsp stream is not continuously using demo code.

Dian-Yi opened this issue · 0 comments

import ffmpeg
import numpy as np
import time

if __name__ == '__main__':
    user = ''
    password = ''  # 访问摄像机需要密码
    ip = '192.168.2.91'
    camera = "rtsp://" + user + ":" + password + "@" + ip + ":554/h264/ch1/main/av_stream"

    probe = ffmpeg.probe(camera)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    width = int(video_stream['width'])
    height = int(video_stream['height'])
    fps = video_stream['r_frame_rate']


    process1 = (
        ffmpeg
        .input(camera)
        .output('pipe:', format='rawvideo', pix_fmt='rgb24')
        .run_async(pipe_stdout=True)
    )
    s = 0
    frame_idx = 0
    while True:
        t1 = time.time()
        in_bytes = process1.stdout.read(width * height * 3)
        if not in_bytes:
            break
        t_cost = time.time() - t1
        if t_cost > 0.2:
            print(frame_idx, frame_idx - s, '--->', t_cost)
            s = frame_idx
        in_frame = (
            np
            .frombuffer(in_bytes, np.uint8)
            .reshape([height, width, 3])
        )
        frame_idx += 1

    process1.wait()

use this code, you can find stdout.read will be blocked after 2s. Can everyoen solver this problems?

0 0 ---> 1.2755885124206543
36 36 ---> 0.27614760398864746
86 50 ---> 0.2705404758453369
136 50 ---> 0.27320241928100586