import time
import cv2
import subprocess as sp
# 推流
import ffmpeg
import numpy as np

if __name__== "__main__":


    # with open(str(cv2.__file__),"r") as f:
    #     print (f.read())

    # cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAs")
    # # Get video information
    # fps = int(cap.get(cv2.CAP_PROP_FPS))
    # print(fps)
    # width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    # print(width)
    # height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    # print(height)
    # fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
    # print(fourcc)
    # # print(cv2.getBuildInformation())
    # # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
    # # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
    # # cap.set(cv2.CAP_PROP_FPS, 60)
    # # cap.setExceptionMode(True)
    # print(cap.getExceptionMode())


    # ffmpeg command
    # command = ['ffmpeg',
    #            '-y', # 不经过确认，输出时直接覆盖同名文件。
    #            '-f', 'rawvideo',
    #            '-vcodec','rawvideo',
    #            '-pix_fmt', 'bgr24',
    #             '-s', "{}x{}".format(1920, 1080),
    #            # '-s', "{}x{}".format(1280, 720),
    #            '-i', '-', # 指定输入文件
    #            '-c:v', 'libx264', # 指定视频编码器
    #            '-pix_fmt', 'yuv420p',
    #             '-g', '5',
    #             "-an",
    #             '-b:v', '3000k',
    #            '-preset', 'ultrafast', # 指定输出的视频质量，会影响文件的生成速度，有以下几个可用的值 ultrafast,
    #                                 # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
    #            '-f', 'flv',
    #            "rtmp://live.push.t-aaron.com/live/THSAk"]
    kwargs = {'format': 'rawvideo',
              'vcodec': 'rawvideo',
              'pix_fmt': 'bgr24',
              's': '{}x{}'.format(int(1920), int(1080))}
    out = {
        'g': str(25),
        'b:v': '6000k',
        # '-bufsize': '3000k',
        'tune': 'zerolatency', # 加速编码速度
        'c:v': 'libx264',  # 指定视频编码器
        'sc_threshold': '0',
        'pix_fmt': 'yuv420p',
        'flvflags': 'no_duration_filesize',
        'preset': 'ultrafast',  # 指定输出的视频质量，会影响文件的生成速度，有以下几个可用的值 ultrafast,
        # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
        'format': 'flv'}
    # 管道配置
    process2 = (
        ffmpeg
            .input('pipe:', **kwargs)
            .output('rtmp://live.push.t-aaron.com/live/THSAk', **out)
            .global_args('-y', '-an')
            .overwrite_output()
            .run_async(pipe_stdin=True)
    )
    # 管道配置
    # p = sp.Popen(command, stdin=sp.PIPE, shell=False)

    process = (
        ffmpeg
            .input('rtmp://221.226.114.142:19350/rlive/stream_4?sign=PZIGmXvw')
            .output('pipe:', format='rawvideo', pix_fmt='bgr24')
            .overwrite_output()
            .global_args('-an')
            .run_async(pipe_stdout=True)
    )
    # while(cap.isOpened()):
    while True:
        start =time.time()
        # ret, frame = cap.read()
        # print(cap.grab())
        in_bytes = process.stdout.read(1920 * 1080 * 3)
        # print("aaaaaaaaaaaaaaa", time.time()-start)
        # print("aaaaaaaaaaaaaa", time.time()-start)
        # start =time.time()
        # a,b = cap.retrieve()
        if not in_bytes:
            print("Opening camera is failed")
            break
        frame = (np.frombuffer(in_bytes, np.uint8).reshape([1080, 1920, 3]))
        print("ccccccccccccccc", time.time()-start)
        # process2.stdin.write(frame.tostring())
        # print("bbbbbbbbbbbbbb", time.time()-start)

