Skip to content

Instantly share code, notes, and snippets.

@jmpinit
Created July 20, 2023 16:31
Show Gist options
  • Save jmpinit/94bc9a0338e23b14a3f05376481706af to your computer and use it in GitHub Desktop.
Save jmpinit/94bc9a0338e23b14a3f05376481706af to your computer and use it in GitHub Desktop.
Roundabout way to get video frames from Python into the browser via FFmpeg and WebRTC.
# To test with FFmpeg only:
# python3 capture.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i - foo.avi
# To create WebRTC stream with [ffmpeg-to-webrtc](https://github.com/ashellunts/ffmpeg-to-webrtc):
# go run . -rtbufsize 100M -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i video -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -max_delay 0 -bf 0 -f h264 - < SDP.txt
import time
import cv2, sys
cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True :
ret, frame = cam.read()
if not ret:
time.sleep(0.1)
sys.stdout.buffer.write(frame.tostring())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment