Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

175 lines
6.3KB

  1. import json
  2. import time
  3. import subprocess as sp
  4. import ffmpeg
  5. import cv2
  6. import sys
  7. import numpy as np
  8. """
  9. 获取视频基本信息
  10. """
  11. def get_video_info(in_file):
  12. try:
  13. probe = ffmpeg.probe('https://vod.play.t-aaron.com/customerTrans/edc96ea2115a0723a003730956208134/55547af9-184f0827dae-0004-f90c-f2c-7ec68.mp4')
  14. # format = probe['format']
  15. # size = int(format['size'])/1024/1024
  16. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  17. if video_stream is None:
  18. print('No video stream found', file=sys.stderr)
  19. return
  20. width = int(video_stream['width'])
  21. height = int(video_stream['height'])
  22. num_frames = int(video_stream['nb_frames'])
  23. up, down = str(video_stream['r_frame_rate']).split('/')
  24. fps = eval(up) / eval(down)
  25. print("fbs:", fps)
  26. # duration = float(video_stream['duration'])
  27. bit_rate = int(video_stream['bit_rate'])/1000
  28. print('width: {}'.format(width))
  29. print('height: {}'.format(height))
  30. # print('num_frames: {}'.format(num_frames))
  31. print('bit_rate: {}k'.format(bit_rate))
  32. # print('fps: {}'.format(fps))
  33. # print('size: {}MB'.format(size))
  34. # print('duration: {}'.format(duration))
  35. return video_stream
  36. except Exception as err:
  37. if isinstance(err, ffmpeg._run.Error):
  38. print(err.stderr.decode(encoding = 'utf-8'))
  39. raise err
  40. if __name__ == '__main__':
  41. file_path = r'D:\shipin\777.mp4'
  42. video_info = get_video_info(file_path)
  43. print(json.dumps(video_info))
  44. # total_frames = int(video_info['nb_frames'])
  45. # print('总帧数:' + str(total_frames))
  46. # random_frame = random.randint(1, total_frames)
  47. # print('随机帧:' + str(random_frame))
  48. # out = read_frame_as_jpeg(file_path, i)
  49. # image_array = numpy.asarray(bytearray(out), dtype="uint8")
  50. # image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
  51. # kwargs={'fflags': 'nobuffer', 'flags': 'low_delay'}
  52. width = int(video_info['width'])
  53. height = int(video_info['height'])
  54. width_2_1 = int(width/2)
  55. height_2_1 = int(height/2)
  56. print("长:", width, "宽:", height)
  57. text = ''
  58. command = ['ffmpeg',
  59. # '-hwaccel', 'cuvid',
  60. '-c:v', 'h264_cuvid',
  61. # '-hwaccel_output_format', 'cuda',
  62. # '-resize', '%sx%s' % (width_2_1, height_2_1),
  63. '-i', file_path,
  64. # '-c:v', 'hevc_nvenc',
  65. # '-pix_fmt', 'yuv420p',
  66. '-f', 'rawvideo',
  67. '-pix_fmt', 'bgr24',
  68. '-an',
  69. '-']
  70. p = sp.Popen(command, stdout=sp.PIPE)
  71. # for line in p.stderr:
  72. # # print(line.strip())
  73. # for line in p.stdout:
  74. # print(text)
  75. command1 = ['ffmpeg',
  76. '-report',
  77. # '-loglevel', 'debug',
  78. '-y', # 不经过确认,输出时直接覆盖同名文件。
  79. '-f', 'rawvideo',
  80. '-vcodec', 'rawvideo',
  81. '-pix_fmt', 'bgr24',
  82. # '-hwaccel', 'cuvid',
  83. # '-c:v', 'h264_cuvid',
  84. # '-hwaccel_output_format', 'cuda',
  85. # '-s', "{}x{}".format(self.width * 2, self.height),
  86. '-s', "{}x{}".format(width, height),
  87. '-r', str(25),
  88. '-i', '-', # 指定输入文件
  89. '-g', str(5),
  90. # '-maxrate', '15000k',
  91. # '-profile:v', 'high',
  92. # '-b:v', '4000k',
  93. # '-crf', '18',
  94. '-rc:v', 'vbr',
  95. '-cq:v', '30',
  96. '-qmin', '30',
  97. '-qmax', '30',
  98. '-bufsize', '1',
  99. '-c:v', 'h264_nvenc', #
  100. # '-c:v', 'libx264', # 指定视频编码器
  101. # '-tune', 'zerolatency', # 加速编码速度
  102. # '-sc_threshold', '0',
  103. '-pix_fmt', 'yuv420p',
  104. "-an",
  105. # '-flvflags', 'no_duration_filesize',
  106. '-preset', 'fast',
  107. # '-f', 'flv',ags', 'no_duration_filesize',
  108. # '-preset', 'fast', # 指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast,
  109. # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
  110. '-f', 'flv',
  111. 'rtmp://live.push.t-aaron.com/live/THSAr']
  112. # # 管道配置
  113. p1 = sp.Popen(command1, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, )
  114. start1 = time.time()
  115. num = 0
  116. while True:
  117. # num += 1
  118. # # if num ==100:
  119. # # print(time.time()-start1)
  120. # # break
  121. # start = time.time()
  122. in_bytes = p.stdout.read(int(width * height*3))
  123. # # print(type(in_bytes))
  124. img = (np.frombuffer(in_bytes, np.uint8)).reshape((height, width, 3))
  125. # # result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12)
  126. # # result = cv2.resize(result, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_LINEAR)
  127. # # result = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
  128. for line in iter(p1.stdout.readline, b''):
  129. print(line)
  130. # if in_bytes:
  131. # p1.stdin.write(img.tostring())
  132. # print(p1.stderr.read().decode('utf-8'))
  133. # for line in iter(p1.std
  134. #
  135. #
  136. # out.readline, ''):
  137. # line = line.decode('utf-8')
  138. # print(line)
  139. # print(num)
  140. # time.sleep(0.001)
  141. # p1.stdin.write(in_frame.tostring())
  142. # frame
  143. # .astype(np.uint8)
  144. # .tobytes()
  145. # frame = cv2.resize(in_frame, (1280, 720)) # 改变图片尺寸
  146. # frame = cv2.cvtColor(in_frame, cv2.COLOR_RGB2BGR) # 转成BGR
  147. # i += 1
  148. # print(round(time.time()-start, 5))
  149. #
  150. # ai_video_file.write(in_frame)
  151. # if time.time() - start1 > 60:
  152. # ai_video_file.release()
  153. # p.stdout.close()
  154. # p.wait()
  155. # break
  156. # cv2.imshow('frame', frame)
  157. # time.sleep(1111)
  158. # p.kill()