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.

123 lines
4.0KB

  1. import json
  2. import time
  3. import subprocess as sp
  4. from concurrent.futures import ThreadPoolExecutor
  5. from traceback import format_exc
  6. import ffmpeg
  7. import sys
  8. import numpy as np
  9. """
  10. 获取视频基本信息
  11. """
  12. def get_video_info(in_file):
  13. try:
  14. probe = ffmpeg.probe(
  15. 'https://vod.play.t-aaron.com/customerTrans/edc96ea2115a0723a003730956208134/55547af9-184f0827dae-0004-f90c-f2c-7ec68.mp4')
  16. # format = probe['format']
  17. # size = int(format['size'])/1024/1024
  18. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  19. if video_stream is None:
  20. print('No video stream found', file=sys.stderr)
  21. return
  22. width = int(video_stream['width'])
  23. height = int(video_stream['height'])
  24. num_frames = int(video_stream['nb_frames'])
  25. up, down = str(video_stream['r_frame_rate']).split('/')
  26. fps = eval(up) / eval(down)
  27. print("fbs:", fps)
  28. # duration = float(video_stream['duration'])
  29. bit_rate = int(video_stream['bit_rate']) / 1000
  30. print('width: {}'.format(width))
  31. print('height: {}'.format(height))
  32. # print('num_frames: {}'.format(num_frames))
  33. print('bit_rate: {}k'.format(bit_rate))
  34. # print('fps: {}'.format(fps))
  35. # print('size: {}MB'.format(size))
  36. # print('duration: {}'.format(duration))
  37. return video_stream
  38. except Exception as err:
  39. if isinstance(err, ffmpeg._run.Error):
  40. print(err.stderr.decode(encoding='utf-8'))
  41. raise err
  42. def aa(p1, in_bytes):
  43. try:
  44. p1.stdin.write(in_bytes)
  45. except Exception:
  46. print(format_exc())
  47. if __name__ == '__main__':
  48. file_path = 'rtsp://localhost:8554/live'
  49. command = ['ffmpeg',
  50. '-c:v', 'h264_cuvid',
  51. '-i', file_path,
  52. '-f', 'rawvideo',
  53. '-pix_fmt', 'bgr24',
  54. '-an',
  55. '-']
  56. p = sp.Popen(command, stdout=sp.PIPE)
  57. command1 = ['ffmpeg',
  58. '-y',
  59. "-an",
  60. '-f', 'rawvideo',
  61. '-vcodec', 'rawvideo',
  62. '-pix_fmt', 'bgr24',
  63. '-thread_queue_size', '1024',
  64. '-s', "{}x{}".format(1280, 720),
  65. '-i', '-', # 指定输入文件
  66. '-r', str(25),
  67. '-g', str(25),
  68. '-maxrate', '6000k',
  69. '-b:v', '4000k',
  70. '-c:v', 'h264_nvenc', #
  71. '-bufsize', '4000k',
  72. '-pix_fmt', 'yuv420p',
  73. '-preset', 'p6',
  74. '-tune', 'll',
  75. '-f', 'flv',
  76. "rtmp://192.168.10.101:19350/rlive/stream_124?sign=YJ8aBPFp"]
  77. # # 管道配置
  78. p1 = sp.Popen(command1, stdin=sp.PIPE, shell=False)
  79. command2 = ['ffmpeg',
  80. '-y',
  81. "-an",
  82. '-f', 'rawvideo',
  83. '-vcodec', 'rawvideo',
  84. '-pix_fmt', 'bgr24',
  85. '-thread_queue_size', '1024',
  86. '-s', "{}x{}".format(1280, 720),
  87. '-i', '-', # 指定输入文件
  88. '-r', str(25),
  89. '-g', str(25),
  90. '-maxrate', '6000k',
  91. '-b:v', '4000k',
  92. '-c:v', 'h264_nvenc', #
  93. '-bufsize', '4000k',
  94. '-pix_fmt', 'yuv420p',
  95. '-preset', 'p6',
  96. '-tune', 'll',
  97. '-f', 'flv',
  98. "rtmp://192.168.10.101:19350/rlive/stream_125?sign=uMdRHj9R"]
  99. # # 管道配置
  100. p2 = sp.Popen(command1, stdin=sp.PIPE, shell=False)
  101. start1 = time.time()
  102. num = 0
  103. with ThreadPoolExecutor(max_workers=100) as t:
  104. while True:
  105. in_bytes = p.stdout.read(1280 * 720 * 3)
  106. if in_bytes:
  107. img = (np.frombuffer(in_bytes, np.uint8)).reshape((720, 1280, 3))
  108. for i in range(1):
  109. t.submit(aa, p1, in_bytes)
  110. t.submit(aa, p2, in_bytes)
  111. else:
  112. break