Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Authored by Souvik Mandal, Ph.D.

Project Leader & Instructor, Computational Behavioral Sciences, LS100, FAS, Harvard University | Linkedin ID: souvik-mandal-phd


Last updated: 2026-07-03

Open in Colab Open in GitHub Codespaces

Overview

This notebook splits a video into consecutive frame-count–based chunks and saves them into a subfolder named after the source video.

Example: user/xyz/documents/videos/example.mp4 → outputs into user/xyz/documents/videos/example/ as example_chunk01.mp4, example_chunk02.mp4, …

Th workflow is as follows

  • Creates an output subdirectory with the base filename of your video.

  • Splits the video into consecutive chunks, each containing a user-defined number of frames.

  • Writes a final remainder chunk if the video length isn’t divisible by your chunk size.

  • Names chunks as: <video_stem>_chunkXX.mp4 (e.g., example_chunk01.mp4).

Why frames (not time)?
This ensures exact frame counts per chunk, which is helpful when you want deterministic splits for annotation, ML, or analysis workflows.


Requirements

  • Python 3.8+

  • OpenCV (cv2) installed with a working backend (FFmpeg/GStreamer depending on OS).

  • Sufficient disk space to write chunked files.

Tip (macOS/Linux): If your OpenCV lacks codecs, install/enable FFmpeg. On macOS, brew install ffmpeg can help; on Linux, install via your package manager.

Install OpenCV, if you need

Uncomment and run the cell below if you need to install OpenCV. If you’re in a restricted network, install locally on your machine.

1. Set Your Inputs

  • input_path: Absolute or relative path to your video file.

  • chunk_num_frame: Number of frames per chunk (positive integer).

  • codec: FourCC code for output (default mp4v; try avc1 or H264 if available on your system for smaller files).

2. Define Core Function

The function below reads frames sequentially and writes chunk files with the same FPS and resolution as the source video.

3. Run the Splitter

Run the cell below to split your video using the parameters defined earlier.

4. Verify Outputs

List the chunked files to confirm.