選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

101 行
3.2KB

  1. import json
  2. import time
  3. import subprocess as sp
  4. import ffmpeg
  5. import cv2
  6. import sys
  7. import random
  8. import numpy as np
  9. """
  10. 获取视频基本信息
  11. """
  12. def get_video_info(in_file):
  13. try:
  14. probe = ffmpeg.probe(in_file)
  15. # format = probe['format']
  16. # size = int(format['size'])/1024/1024
  17. video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
  18. if video_stream is None:
  19. print('No video stream found', file=sys.stderr)
  20. return
  21. width = int(video_stream['width'])
  22. height = int(video_stream['height'])
  23. # num_frames = int(video_stream['nb_frames'])
  24. up, down = str(video_stream['r_frame_rate']).split('/')
  25. video_stream['fps']=eval(up) / eval(down)
  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. print("aaaaaaaaaaaaaaaaaaaa", err)
  38. return None
  39. if __name__ == '__main__':
  40. file_path = "rtmp://live.play.t-aaron.com/live/THSAa"
  41. while True:
  42. video_info = get_video_info(file_path)
  43. print(json.dumps(video_info))
  44. if video_info is None:
  45. time.sleep(2)
  46. continue
  47. else:
  48. break
  49. command = ['ffmpeg',
  50. # '-re',
  51. '-y',
  52. '-c:v', 'h264_cuvid',
  53. '-i', file_path,
  54. '-pix_fmt', 'bgr24',
  55. '-f', 'rawvideo',
  56. '-an',
  57. '-']
  58. width = int(video_info['width'])
  59. height = int(video_info['height'])
  60. fps = int(video_info['fps'])
  61. p = sp.Popen(command, stdout=sp.PIPE)
  62. ai_video_file = cv2.VideoWriter(r"C:\Users\chenyukun\Desktop\shipin\aaaa.mp4", cv2.VideoWriter_fourcc(*'mp4v'), fps,
  63. (width, height))
  64. start1 = time.time()
  65. while True:
  66. start = time.time()
  67. in_bytes = p.stdout.read(int(width * height * 3))
  68. if not in_bytes:
  69. print(in_bytes)
  70. p.stdout.close()
  71. p.wait()
  72. break
  73. # img = (np.frombuffer(in_bytes, np.uint8)).reshape((int(height), int(width)))
  74. # bgr_img = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12)
  75. in_frame = (np.frombuffer(in_bytes, np.uint8).reshape([int(height), int(width), 3]))
  76. # print("拉流时间:", time.time() - start)
  77. # frame = cv2.resize(in_frame, (1280, 720)) # 改变图片尺寸
  78. # i += 1
  79. # print(round(time.time()-start, 5))
  80. #
  81. ai_video_file.write(in_frame)
  82. # if time.time() - start1 > 60:
  83. # ai_video_file.release()
  84. # p.stdout.close()
  85. # p.wait()
  86. # break
  87. # cv2.imshow('frame', in_frame)
  88. # cv2.waitKey(1)
  89. # time.sleep(1111)
  90. p.stdout.close()
  91. p.wait()
  92. p.kill()