async def consume_video(track, local_video): """ Receive incoming video. The video can optionally be written to a file. """ last_size = None writer = None try: while True: frame = await track.recv() # print frame size frame_size = (frame.width, frame.height) if frame_size != last_size: print('Received frame size', frame_size) last_size = frame_size # write to file if args.write_video: if writer is None: fourcc = cv2.VideoWriter_fourcc(*'XVID') writer = cv2.VideoWriter(args.write_video, fourcc, 30, frame_size) writer.write(frame_to_bgr(frame)) # we are only interested in the latest frame if local_video.received.full(): await local_video.received.get() await local_video.received.put(frame) finally: if writer is not None: writer.release()
async def run_answer(pc, signaling): remote_track = None @pc.on('track') def on_track(track): nonlocal remote_track assert track.kind == 'video' remote_track = track # receive offer offer = await signaling.receive() await pc.setRemoteDescription(offer) # send answer await pc.setLocalDescription(await pc.createAnswer()) await signaling.send(pc.localDescription) print('Receiving video') while True: done, pending = await asyncio.wait([remote_track.recv()], timeout=5) for task in pending: task.cancel() if done: frame = list(done)[0].result() data_bgr = frame_to_bgr(frame) cv2.imwrite(OUTPUT_PATH, data_bgr) else: print('No video for 5s, stopping') break
async def recv(self): frame = await self.received.get() self.counter += 1 if (self.counter % 100) > 50: # apply image processing to frame if self.transform == 'edges': img = frame_to_bgr(frame) edges = cv2.Canny(img, 100, 200) return frame_from_gray(edges) elif self.transform == 'rotate': img = frame_to_bgr(frame) rows, cols, _ = img.shape M = cv2.getRotationMatrix2D((cols / 2, rows / 2), self.counter * 7.2, 1) rotated = cv2.warpAffine(img, M, (cols, rows)) return frame_from_bgr(rotated) elif self.transform == 'green': return VideoFrame(width=frame.width, height=frame.height) else: return frame else: # return raw frame return frame
async def recv(self): coros = [track.recv() for track in self.tracks] frames = await asyncio.gather(*coros) data_bgrs = [frame_to_bgr(frame) for frame in frames] data_bgr = numpy.hstack(data_bgrs) return frame_from_bgr(data_bgr)
def test_frame_to_bgr(self): frame = VideoFrame(width=640, height=480) image = frame_to_bgr(frame) self.assertEqual(image.shape, (480, 640, 3))