Requirements
FFMPEG must be installed on your system.
On mac, you can run :
brew install ffmpeg
OpenCV-python installation
pip3 install opencv-python
About OpenCV & codecs
By default OpenCV is shipped with royalty free codecs only.
For using non free codecs, you can compile OpenCV yourself (which takes time) or you can pipe OpenCV with FFMPEG.。
Royalty free codecs

In this example we will use the VP8 codec.
NB : OpenCV uses BGR color format.
Implementation
import cv2
import os
import time
import sys
# Don't forget to check the framerate of your input file !! You can check with : ffprobe yourFile.mp4
out = cv2.VideoWriter(
"output.mkv", cv2.VideoWriter_fourcc(*'VP80'), 25.0,
(720,400))
# The input file is located in the current working directory.
cap = cv2.VideoCapture(os.getcwd()+"/"+'Big_Buck_Bunny_Trailer_400p.ogv')
if (cap.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# Do something with the frame...
ts = time.time()
cv2.putText(frame ,"Timestamp : " + str(ts), (15, 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
out.write(frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
# Cleaning
out.release()
cap.release()
cv2.destroyAllWindows()
Standard Output (stdout) piped to FFMPEG & FFPLAY

NB : OpenCV uses BGR color format.
Data from OpenCV to FFPLAY
python3 simple.py | ffplay -f rawvideo -pix_fmt bgr24 -s 720x400 -framerate 25 -i -
Data from OpenCV to FFMPEG
In this example we will use the libx264 encoder.
python3 simple.py | ffmpeg -f rawvideo -pix_fmt bgr24 -s 720x400 -framerate 25 -i - -c:v libx264 output.mp4
Implementation
import cv2
import os
import time
import sys
# Don't forget to check resolution & framerate !!
# You can check with : ffprobe yourFile.mp4
# Simple playback
# python3 simple.py | ffplay -f rawvideo -pix_fmt bgr24 -s 720x400 -framerate 25 -i -
# Encoding
# python3 simple.py | ffmpeg -f rawvideo -pix_fmt bgr24 -s 720x400 -framerate 25 -i - -c:v libx264 output.mp4
# The input file is located in the current working directory.
cap = cv2.VideoCapture(os.getcwd()+"/"+'Big_Buck_Bunny_Trailer_400p.ogv')
if (cap.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# Do something with the frame...
ts = time.time()
cv2.putText(frame ,"Timestamp : " + str(ts), (15, 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
# We send the frame to the standard output, then FFMPEG will catch it and process it
sys.stdout.buffer.write(frame.tobytes())
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
# Cleaning
cap.release()
cv2.destroyAllWindows()
Information sources
https://pypi.org/project/opencv-python/
https://www.ffmpeg.org/
https://commons.wikimedia.org/wiki/File:Big_Buck_Bunny_Trailer_400p.ogv