You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cv2test1.py 3.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import time
  2. import cv2
  3. import subprocess as sp
  4. # 推流
  5. import ffmpeg
  6. import numpy as np
  7. if __name__== "__main__":
  8. # with open(str(cv2.__file__),"r") as f:
  9. # print (f.read())
  10. # cap = cv2.VideoCapture("rtmp://live.play.t-aaron.com/live/THSAs")
  11. # # Get video information
  12. # fps = int(cap.get(cv2.CAP_PROP_FPS))
  13. # print(fps)
  14. # width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  15. # print(width)
  16. # height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  17. # print(height)
  18. # fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
  19. # print(fourcc)
  20. # # print(cv2.getBuildInformation())
  21. # # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
  22. # # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
  23. # # cap.set(cv2.CAP_PROP_FPS, 60)
  24. # # cap.setExceptionMode(True)
  25. # print(cap.getExceptionMode())
  26. # ffmpeg command
  27. # command = ['ffmpeg',
  28. # '-y', # 不经过确认,输出时直接覆盖同名文件。
  29. # '-f', 'rawvideo',
  30. # '-vcodec','rawvideo',
  31. # '-pix_fmt', 'bgr24',
  32. # '-s', "{}x{}".format(1920, 1080),
  33. # # '-s', "{}x{}".format(1280, 720),
  34. # '-i', '-', # 指定输入文件
  35. # '-c:v', 'libx264', # 指定视频编码器
  36. # '-pix_fmt', 'yuv420p',
  37. # '-g', '5',
  38. # "-an",
  39. # '-b:v', '3000k',
  40. # '-preset', 'ultrafast', # 指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast,
  41. # # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
  42. # '-f', 'flv',
  43. # "rtmp://live.push.t-aaron.com/live/THSAk"]
  44. kwargs = {'format': 'rawvideo',
  45. 'vcodec': 'rawvideo',
  46. 'pix_fmt': 'bgr24',
  47. 's': '{}x{}'.format(int(1920), int(1080))}
  48. out = {
  49. 'g': str(25),
  50. 'b:v': '6000k',
  51. # '-bufsize': '3000k',
  52. 'tune': 'zerolatency', # 加速编码速度
  53. 'c:v': 'libx264', # 指定视频编码器
  54. 'sc_threshold': '0',
  55. 'pix_fmt': 'yuv420p',
  56. 'flvflags': 'no_duration_filesize',
  57. 'preset': 'ultrafast', # 指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast,
  58. # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
  59. 'format': 'flv'}
  60. # 管道配置
  61. process2 = (
  62. ffmpeg
  63. .input('pipe:', **kwargs)
  64. .output('rtmp://live.push.t-aaron.com/live/THSAk', **out)
  65. .global_args('-y', '-an')
  66. .overwrite_output()
  67. .run_async(pipe_stdin=True)
  68. )
  69. # 管道配置
  70. # p = sp.Popen(command, stdin=sp.PIPE, shell=False)
  71. process = (
  72. ffmpeg
  73. .input('rtmp://221.226.114.142:19350/rlive/stream_4?sign=PZIGmXvw')
  74. .output('pipe:', format='rawvideo', pix_fmt='bgr24')
  75. .overwrite_output()
  76. .global_args('-an')
  77. .run_async(pipe_stdout=True)
  78. )
  79. # while(cap.isOpened()):
  80. while True:
  81. start =time.time()
  82. # ret, frame = cap.read()
  83. # print(cap.grab())
  84. in_bytes = process.stdout.read(1920 * 1080 * 3)
  85. # print("aaaaaaaaaaaaaaa", time.time()-start)
  86. # print("aaaaaaaaaaaaaa", time.time()-start)
  87. # start =time.time()
  88. # a,b = cap.retrieve()
  89. if not in_bytes:
  90. print("Opening camera is failed")
  91. break
  92. frame = (np.frombuffer(in_bytes, np.uint8).reshape([1080, 1920, 3]))
  93. print("ccccccccccccccc", time.time()-start)
  94. # process2.stdin.write(frame.tostring())
  95. # print("bbbbbbbbbbbbbb", time.time()-start)