Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 54 ยท Video and Cameras

Video Recipes: Duration, Slow Motion, Timestamps, Blending and More

Practical OpenCV video tasks: read duration and FPS, extract frames, resize, add timestamps, slow motion, reverse playback, blend two videos, images to video and threaded reading.

Reading and Writing Video taught the basic loop and Processing Video Frame by Frame taught how to make it fast. Real projects then ask a set of everyday questions: How long is this video? How do I save every tenth frame? How do I change the resolution, burn the time into the picture, slow the clip down, play it backwards, fade from one clip into another, or turn a folder of pictures into a movie? This lesson is a collection of such recipes.

Almost every recipe rests on three facts about video that you will learn first: how frames, frame rate and time relate, which properties VideoCapture can report, and how VideoWriter decides the playback speed. The OpenCV code is read-only because it needs files or a camera. NumPy and plain Python models run the frame arithmetic.

Frames, FPS and time

A video is a list of frames shown at a steady rate. Three numbers are connected:

duration = frame_count / fps and time of frame n = n / fps

Python
import cv2

cap = cv2.VideoCapture("clip.mp4")
if not cap.isOpened():
    raise SystemExit("could not open the video")

fps = cap.get(cv2.CAP_PROP_FPS)
frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f"{width}x{height}, {fps:.2f} fps, {frames} frames, {frames / fps:.1f} s")
cap.release()

Cautions: FRAME_COUNT is an estimate for some containers and for streams it is 0 or negative; webcams report FPS as 0 for some drivers, so measure it yourself by timing a few dozen frames. cap.get(cv2.CAP_PROP_POS_MSEC) is the time of the current position in milliseconds, and CAP_PROP_POS_FRAMES is the index of the next frame to be read.

To jump to a position, set one of these properties, for example cap.set(cv2.CAP_PROP_POS_FRAMES, 300) or cap.set(cv2.CAP_PROP_POS_MSEC, 10_000). Seeking is exact only for some codecs; for highly compressed formats it jumps to the nearest keyframe, so test it.

Extract frames to images

Python
import os
os.makedirs("frames", exist_ok=True)

cap = cv2.VideoCapture("clip.mp4")
step = 10                                  # save every 10th frame
index = saved = 0
while True:
    ok, frame = cap.read()
    if not ok:                             # end of the file
        break
    if index % step == 0:
        cv2.imwrite(f"frames/frame_{saved:05d}.jpg", frame)
        saved += 1
    index += 1
cap.release()
print("saved", saved, "frames")

Zero-padding the file name (:05d) keeps the files in the right order when sorted. To save one frame per second instead, use step = int(round(fps)).

Images to video

VideoWriter needs a file name, a fourcc codec code, the playback FPS and the frame size as (width, height). Every frame you write must have exactly that size, in 3-channel BGR.

Python
import glob

files = sorted(glob.glob("frames/*.jpg"))
first = cv2.imread(files[0])
h, w = first.shape[:2]

fourcc = cv2.VideoWriter_fourcc(*"mp4v")           # common choices: mp4v, XVID, MJPG; DIVX also works
out = cv2.VideoWriter("movie.mp4", fourcc, 24.0, (w, h))
for name in files:
    frame = cv2.imread(name)
    if frame.shape[:2] != (h, w):
        frame = cv2.resize(frame, (w, h))          # every frame must match
    out.write(frame)
out.release()

Common reasons for an empty or unplayable output: swapping width and height in the size tuple, writing frames of a different size (they are silently dropped), a codec your OpenCV build does not have, forgetting release(), and writing grayscale frames without isColor=False.

Change the resolution

There are two ways. For a camera, ask the device (it may refuse): cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) and cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720), then read back what you really got. For a file, resize every frame with cv2.resize and write it with a writer made for the new size:

Python
new_size = (640, 360)
out = cv2.VideoWriter("small.mp4", fourcc, fps, new_size)
ok, frame = cap.read()
while ok:
    out.write(cv2.resize(frame, new_size, interpolation=cv2.INTER_AREA))
    ok, frame = cap.read()

Draw a timestamp or a date on every frame

Use datetime for a live clock, or the frame index for the position in the file:

Python
from datetime import datetime

frame_no = 0
while True:
    ok, frame = cap.read()
    if not ok:
        break
    stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")      # wall-clock time
    seconds = frame_no / fps                                  # position in the video
    label = f"{stamp}  t={seconds:6.2f}s"
    cv2.putText(frame, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 4, cv2.LINE_AA)
    cv2.putText(frame, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
    frame_no += 1

Drawing the text twice (thick dark, then thin light) keeps it readable on any background; see Drawing Shapes and Text. To draw a filled rectangle on every frame (a privacy box or a banner), call cv2.rectangle(frame, p1, p2, color, -1) inside the loop in the same way.

Slow motion and fast motion

You change the perceived speed in one of two ways:

  1. Write the same frames at a different FPS. Frames written at half the FPS play at half speed, with no extra frames, so slow motion looks jerky.
  2. Duplicate or interpolate frames. Write each frame twice for 2x slower playback at the original FPS, or blend neighboring frames (cv2.addWeighted(prev, 0.5, frame, 0.5, 0)) to create in-between frames that are smoother. For fast motion, write only every n-th frame.
Python
slow = 2                                    # 2x slower
out = cv2.VideoWriter("slow.mp4", fourcc, fps, (w, h))
prev = None
ok, frame = cap.read()
while ok:
    if prev is not None:
        out.write(cv2.addWeighted(prev, 0.5, frame, 0.5, 0))   # in-between frame
    out.write(frame)
    prev = frame
    ok, frame = cap.read()

For truly smooth slow motion from a normal-speed clip you need optical-flow-based interpolation (see Motion and Object Tracking) or a neural model.

Play a video in reverse

Seeking backward frame by frame is slow and unreliable, so read all frames into a list (watch the memory: a minute of 1080p is several gigabytes) or, better, process in chunks:

Python
frames = []
ok, frame = cap.read()
while ok:
    frames.append(frame)
    ok, frame = cap.read()

for frame in reversed(frames):
    cv2.imshow("reverse", frame)
    if cv2.waitKey(int(1000 / fps)) & 0xFF == ord("q"):
        break

For long videos, extract frames to disk (as above) and read them back in reverse file order.

Blend two videos

A cross-fade between two clips is addWeighted with weights that change over time. Both frames must have the same size, so resize one if needed:

Python
a = cv2.VideoCapture("clip_a.mp4")
b = cv2.VideoCapture("clip_b.mp4")
fade_frames = 60                                  # a 2-second fade at 30 fps
for i in range(fade_frames):
    ok1, fa = a.read()
    ok2, fb = b.read()
    if not (ok1 and ok2):
        break
    fb = cv2.resize(fb, (fa.shape[1], fa.shape[0]))
    alpha = i / (fade_frames - 1)                 # 0.0 -> 1.0
    out.write(cv2.addWeighted(fa, 1 - alpha, fb, alpha, 0))

Faster reading with a thread

Reading a frame (decoding, or waiting for a camera) takes time during which your processing code idles. A common trick is to read frames in a background thread into a queue, so decoding and processing overlap. This raises throughput for slow cameras and network streams; for a file already decoded faster than you process it, there is no gain.

Python
import threading, queue

class FrameReader:
    def __init__(self, source, maxsize=64):
        self.cap = cv2.VideoCapture(source)
        self.q = queue.Queue(maxsize=maxsize)
        self.stopped = False
        threading.Thread(target=self._run, daemon=True).start()

    def _run(self):
        while not self.stopped:
            ok, frame = self.cap.read()
            if not ok:
                self.stopped = True
                break
            self.q.put(frame)                   # blocks if the queue is full

    def read(self):
        return self.q.get() if not (self.stopped and self.q.empty()) else None

Always stop the thread and release() the capture at the end. More speed advice is in Making OpenCV Fast.

Try it: the arithmetic behind the recipes

You do not need real video to practice the calculations. This model turns frames, FPS and time into each other, plans which frames to save, and builds the frame schedule for slow and fast motion.

Python Frames, FPS and time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try it: cross-fade, reverse and a frame-size check

This second model uses tiny NumPy "frames" to show a cross-fade between two clips, the reversed order and the most common VideoWriter mistake: a frame whose size does not match the writer.

Python Blend, reverse and validate frames

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What is real: the property names, the frames / fps relationship, the VideoWriter size and fourcc rules, addWeighted cross-fades and the threading pattern are as OpenCV works. What is the stand-in: no video files can be opened in the browser, so the models use plain numbers and tiny arrays; code that reads and writes real files must be run on your machine with a codec your OpenCV build supports.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A video has 1,800 frames and plays at 30 fps. How long is it?

    duration = frame_count / fps = 1800 / 30 = 60 seconds.
  2. 2. In cv2.VideoWriter(name, fourcc, fps, size), what is the correct order of size?

    VideoWriter expects (width, height), but a NumPy frame's shape is (height, width, channels). Mixing them up produces an empty file.
  3. 3. How can you make slow motion smoother than just writing the same frames at a lower FPS?

    Blending neighbors (or using optical flow) creates new frames, so the motion is not a series of jumps.
  4. 4. What does cap.get(cv2.CAP_PROP_POS_FRAMES) return?

    POS_FRAMES is a 0-based index of the frame that the next read() will return.
  5. 5. Why read frames in a background thread?

    While the main thread processes one frame, the reader thread is already fetching the next, which helps with slow cameras and network streams.

Summary

  • duration = frames / fps and the time of frame n is n / fps; read them from CAP_PROP_FPS, CAP_PROP_FRAME_COUNT and CAP_PROP_POS_MSEC (and verify them: some sources report 0).
  • Extract frames with a counter and imwrite using zero-padded names; build a video from images with VideoWriter and (width, height) frames that all match.
  • Resize frames with INTER_AREA, stamp text with putText (draw it twice for contrast), and cross-fade clips with addWeighted and a changing alpha.
  • Slow motion needs extra (ideally interpolated) frames, reverse playback needs the frames in memory or on disk, and a reader thread can raise throughput.

Next: follow moving objects through those frames in Motion and Object Tracking.