code
第一版可以参考https://blog.csdn.net/u011622208/article/details/95368929

import subprocess as sp
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))        # 视频流的参数
    sizeStr = str(size[0]) + 'x' + str(size[1])
    fps = cap.get(cv2.CAP_PROP_FPS)                            # 30p/self
    fps = int(fps)
    rtmpUrl = 'rtmp://localhost/Vehicle'
    command = ['ffmpeg',
    '-y',
    '-c', 'copy',                   # copy图像的质量会更好
    '-f', 'rawvideo',
    '-vcodec','rawvideo',
    '-pix_fmt', 'bgr24',
    '-max_delay', str(100),
    '-s', sizeStr,
    '-r', str(fps),
    '-i', '-',
    '-c:v', 'libx264',
    '-pix_fmt', 'yuv420p',
    '-preset', 'ultrafast',
    '-f', 'flv',
    '-g', '5',
    '-b', '7000000',
    rtmpUrl]
    pipe_push = sp.Popen(command, stdin=sp.PIPE, shell=False)

    while cap.isOpened():
        tet = cap.grab()
        i += 1
        if i > 1000000000000:
            i = 0
            start = time.time()
        if tet and i % interval == 0:
            det, frame = cap.retrieve()
            if det:
                try:
                    fps = i / (time.time() - start)
                    res_img = DR_model.vehicle_detect(frame, fps)
                    # # debug show
                    # cv2.imshow('mat', res_img)
                    # cv2.waitKey(1)
                    # ffmpeg image to video
                    if ffmpeg_flage:
                        pipe_push.stdin.write(res_img.tostring())

推流成功之后用vlc进行播放的时候,点开视频,延迟很高

解决方案
加上’-g’, '5’可以降低延时
‘b’,‘7000000’,设置的码率,和图像质量有关,数字越小,图片质量越差。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐