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.

80 lines
3.5KB

  1. from traceback import format_exc
  2. import cv2
  3. import numpy as np
  4. from loguru import logger
  5. import subprocess as sp
  6. from common import Constant
  7. from enums.ExceptionEnum import ExceptionType
  8. from exception.CustomerException import ServiceException
  9. from util.Cv2Utils import push_video_stream, clear_pull_p
  10. pull_p = None
  11. push_p =None
  12. p_push_status=[0,0]
  13. def start_pull_p(pull_url, requestId):
  14. try:
  15. command = ['ffmpeg']
  16. # if pull_url.startswith("rtsp://"):
  17. # command.extend(['-timeout', '20000000', '-rtsp_transport', 'tcp'])
  18. # if pull_url.startswith("http") or pull_url.startswith("rtmp"):
  19. # command.extend(['-rw_timeout', '20000000'])
  20. command.extend(['-re',
  21. '-y',
  22. '-an',
  23. # '-hwaccel', 'cuda', cuvid
  24. '-c:v', 'h264_cuvid',
  25. # '-resize', self.wah,
  26. '-i', pull_url,
  27. '-f', 'rawvideo',
  28. # '-pix_fmt', 'bgr24',
  29. '-r', '25',
  30. '-'])
  31. return sp.Popen(command, stdout=sp.PIPE)
  32. except ServiceException as s:
  33. logger.error("构建拉流管道异常: {}, requestId:{}", s.msg, requestId)
  34. raise s
  35. except Exception as e:
  36. logger.error("构建拉流管道异常:{}, requestId:{}", format_exc(), requestId)
  37. raise e
  38. def pull_read_video_stream(pull_p, pull_url, width, height, width_height_3, w_2, h_2, requestId):
  39. result = None
  40. try:
  41. if pull_p is None:
  42. pull_p = start_pull_p(pull_url, requestId)
  43. in_bytes = pull_p.stdout.read(width_height_3)
  44. if in_bytes is not None and len(in_bytes) > 0:
  45. try:
  46. # result = (np.frombuffer(in_bytes, np.uint8).reshape([height * 3 // 2, width, 3]))
  47. # ValueError: cannot reshape array of size 3110400 into shape (1080,1920)
  48. result = (np.frombuffer(in_bytes, np.uint8)).reshape((height, width))
  49. result = cv2.cvtColor(result, cv2.COLOR_YUV2BGR_NV12)
  50. # result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
  51. if result.shape[1] > Constant.width:
  52. result = cv2.resize(result, (result.shape[1] // 2, result.shape[0] // 2), interpolation=cv2.INTER_LINEAR)
  53. except Exception:
  54. logger.error("视频格式异常:{}, requestId:{}", format_exc(), requestId)
  55. raise ServiceException(ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[0],
  56. ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[1])
  57. except ServiceException as s:
  58. clear_pull_p(pull_p, requestId)
  59. raise s
  60. except Exception:
  61. clear_pull_p(pull_p, requestId)
  62. pull_p = None
  63. width = None
  64. height = None
  65. width_height_3 = None
  66. logger.error("读流异常:{}, requestId:{}", format_exc(), requestId)
  67. return result, pull_p, width, height, width_height_3
  68. while True:
  69. frame, pull_p, width, height, width_height_3 = pull_read_video_stream(pull_p, 'rtmp://live.play.t-aaron.com/live/THSAr', 1920,
  70. 1080*3//2, 1920*1080*3//2, 960, 540,
  71. '111')
  72. if frame is not None:
  73. push_p = push_video_stream(frame, push_p, 'rtmp://live.push.t-aaron.com/live/THSAs', p_push_status, '11')
  74. clear_pull_p(pull_p, "11111")
  75. close_all_p(push_p, None, None, "11111")