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.

преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 = 'https://vod.play.t-aaron.com/customerTrans/edc96ea2115a0723a003730956208134/55547af9-184f0827dae-0004-f90c-f2c-7ec68.mp4'
  42. #file_path = 'https://vod.play.t-aaron.com/customerTrans/edc96ea2115a0723a003730956208134/40b416f7-183b57f6be0-0004-f90c-f2c-7ec68.mp4'
  43. #file_path = 'https://vod.play.t-aaron.com/3301fc8e166f45be88f2214e7a8f4a9d/e29535365b54434d9ed2e8c3b0a175da-fba35541b31a1049ca05b145a283c33a-hd.mp4'
  44. video_info = get_video_info(file_path)
  45. print(json.dumps(video_info))
  46. # total_frames = int(video_info['nb_frames'])
  47. # print('总帧数:' + str(total_frames))
  48. # random_frame = random.randint(1, total_frames)
  49. # print('随机帧:' + str(random_frame))
  50. # out = read_frame_as_jpeg(file_path, i)
  51. # image_array = numpy.asarray(bytearray(out), dtype="uint8")
  52. # image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
  53. # kwargs={'fflags': 'nobuffer', 'flags': 'low_delay'}
  54. kwargs={
  55. # "hwaccel": "nvdec",
  56. # "vcodec": "h264_cuvid",
  57. # "c:v": "h264_cuvid"
  58. }
  59. output_args = {
  60. # "vcodec": "hevc_nvenc",
  61. # "c:v": "hevc_nvenc",
  62. # "preset": "fast",
  63. }
  64. # i = 1
  65. # process1 = (
  66. # ffmpeg
  67. # .input(file_path, **kwargs)
  68. # .output('pipe:', format='rawvideo', pix_fmt='rgb24', **output_args)
  69. # # .global_args("-an")
  70. # .overwrite_output()
  71. # .run_async(pipe_stdout=True, pipe_stderr=True)
  72. # )
  73. width = int(video_info['width'])
  74. height = int(video_info['height'])
  75. width_2_1 = int(width/2)
  76. height_2_1 = int(height/2)
  77. print("长:", width, "宽:", height)
  78. command = ['ffmpeg',
  79. # '-hwaccel', 'cuvid',
  80. '-c:v', 'h264_cuvid',
  81. # '-hwaccel_output_format', 'cuda',
  82. '-resize', '%sx%s' % (width_2_1, height_2_1),
  83. '-i', file_path,
  84. # '-c:v', 'hevc_nvenc',
  85. # '-pix_fmt', 'yuv420p',
  86. '-f', 'rawvideo',
  87. # '-pix_fmt', 'bgr24',
  88. '-an',
  89. '-']
  90. p = sp.Popen(command, stdout=sp.PIPE)
  91. # ai_video_file = cv2.VideoWriter(r"C:\Users\chenyukun\Desktop\shipin\aa.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30,
  92. # (width, height))
  93. command1 = ['ffmpeg',
  94. # '-loglevel', 'debug',
  95. '-y', # 不经过确认,输出时直接覆盖同名文件。
  96. '-f', 'rawvideo',
  97. '-vcodec', 'rawvideo',
  98. '-pix_fmt', 'bgr24',
  99. # '-hwaccel', 'cuvid',
  100. # '-c:v', 'h264_cuvid',
  101. # '-hwaccel_output_format', 'cuda',
  102. # '-s', "{}x{}".format(self.width * 2, self.height),
  103. '-s', "{}x{}".format(width_2_1, height_2_1),
  104. '-r', str(25),
  105. '-i', '-', # 指定输入文件
  106. '-g', str(5),
  107. # '-maxrate', '15000k',
  108. # '-profile:v', 'high',
  109. # '-b:v', '4000k',
  110. # '-crf', '18',
  111. '-rc:v', 'vbr',
  112. '-cq:v', '30',
  113. '-qmin', '30',
  114. '-qmax', '30',
  115. '-bufsize', '1',
  116. '-c:v', 'h264_nvenc', #
  117. # '-c:v', 'libx264', # 指定视频编码器
  118. # '-tune', 'zerolatency', # 加速编码速度
  119. # '-sc_threshold', '0',
  120. '-pix_fmt', 'yuv420p',
  121. "-an",
  122. # '-flvflags', 'no_duration_filesize',
  123. '-preset', 'fast',
  124. # '-f', 'flv',ags', 'no_duration_filesize',
  125. # '-preset', 'fast', # 指定输出的视频质量,会影响文件的生成速度,有以下几个可用的值 ultrafast,
  126. # superfast, veryfast, faster, fast, medium, slow, slower, veryslow。
  127. '-f', 'flv',
  128. 'rtmp://live.push.t-aaron.com/live/THSAr']
  129. # # 管道配置
  130. p1 = sp.Popen(command1, stdin=sp.PIPE)
  131. start1 = time.time()
  132. num = 0
  133. while True:
  134. num += 1
  135. # if num ==100:
  136. # print(time.time()-start1)
  137. # break
  138. start = time.time()
  139. in_bytes = p.stdout.read(int(width * height*3//8))
  140. # print(type(in_bytes))
  141. # img = (np.frombuffer(in_bytes, np.uint8)).reshape((height*3//4, width//2))
  142. # result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12)
  143. # result = cv2.resize(result, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_LINEAR)
  144. # result = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
  145. if not in_bytes:
  146. print(in_bytes)
  147. # ai_video_file.release()
  148. p.stdout.close()
  149. p.wait()
  150. break
  151. print(num)
  152. time.sleep(0.001)
  153. # p1.stdin.write(result.tostring())
  154. # p1.stdin.write(in_frame.tostring())
  155. # frame
  156. # .astype(np.uint8)
  157. # .tobytes()
  158. # frame = cv2.resize(in_frame, (1280, 720)) # 改变图片尺寸
  159. # frame = cv2.cvtColor(in_frame, cv2.COLOR_RGB2BGR) # 转成BGR
  160. # i += 1
  161. # print(round(time.time()-start, 5))
  162. #
  163. # ai_video_file.write(in_frame)
  164. # if time.time() - start1 > 60:
  165. # ai_video_file.release()
  166. # p.stdout.close()
  167. # p.wait()
  168. # break
  169. # cv2.imshow('frame', frame)
  170. # time.sleep(1111)
  171. p.kill()