| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import cv2
- import subprocess as sp
- rtspUrl = 'rtsp://localhost:8554/mystream'
- # 视频来源 地址需要替换自己的可识别文件地址
- filePath = '/Users/gemer/Desktop/'
- testPath = '/Users/gemer/Desktop/test-video/'
- camera = cv2.VideoCapture(
- filePath+"test.mp4") # 从文件读取视频
- names = ['output000.mkv', 'output001.mkv', 'output001.mkv', 'output001.mkv']
- # 视频属性
- size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
- int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
- sizeStr = str(size[0]) + 'x' + str(size[1])
- fps = camera.get(cv2.CAP_PROP_FPS) # 30p/self
- # fps = 24
- fps = int(fps)
- hz = int(1000.0 / fps)
- print('size:' + sizeStr + ' fps:' + str(fps) + ' hz:' + str(hz))
- # 主视频文件输出
- # fourcc = cv2.VideoWriter_fourcc(*'XVID')
- fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4')
- out = cv2.VideoWriter(filePath + 'res_mv.avi', fourcc, fps, size)
- # caps = [cv2.VideoCapture(testPath+i) for i in names]
- # print('cap', caps)
- # for x in caps:
- # print(x)
- # fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4')
- # fps = int(fps)
- # hz = int(1000.0 / fps)
- # 直播管道输出
- # ffmpeg推送rtmp 重点 : 通过管道 共享数据的方式
- command = ['ffmpeg',
- '-y',
- '-hide_banner',
- '-loglevel', 'warning',
- # '-hwaccel', 'videotoolbox',
- '-f', 'rawvideo',
- '-vcodec', 'rawvideo',
- '-pix_fmt', 'bgr24',
- '-s', sizeStr,
- '-r', str(fps),
- '-i', '-',
- '-b:v', '3000K',
- # '-c:v', 'h264_videotoolbox',
- '-c:v', 'libx264',
- '-pix_fmt', 'yuv420p',
- '-preset', 'ultrafast',
- '-f', 'rtsp',
- rtspUrl]
- # 管道特性配置
- # pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
- pipe = sp.Popen(command, stdin=sp.PIPE) # ,shell=False
- # pipe.stdin.write(frame.tostring())
- while True:
- ret, frame = camera.read() # 逐帧采集视频流
- # if not ret:
- # break
- # 图片输出
- # 结果帧处理 存入文件 / 推流 / ffmpeg 再处理
- # print('frame', frame)
- pipe.stdin.write(frame.tobytes()) # 存入管道用于直播
- out.write(frame) # 同时 存入视频文件 记录直播帧数据
- pass
- camera.release()
- # Release everything if job is finished
- out.release()
- out.release()
- print("Over!")
|