This refreshed project shows checked, headless-by-default video reading and transcoding in Python 3 and C++17. Both programs accept explicit paths, reject invalid inputs, verify that the output writer opened, preserve measured frame size and frame rate by default, and print a JSON summary.
python -m pip install -r requirements.txt
python videoRead.py --input chaplin.mp4 --max-frames 30Add --display when a desktop is available. Press q or Escape to stop the window.
python videoWrite.py --input chaplin.mp4 --output output.avi \
--codec MJPG --max-frames 30codec must contain four characters. Codec availability depends on the backend in your OpenCV build, so the program checks VideoWriter.isOpened() before writing.
cmake -S . -B build -DBUILD_TESTING=ON
cmake --build build --parallel
./build/video_read --input=chaplin.mp4 --max-frames=30
./build/video_write --input=chaplin.mp4 --output=output.avi \
--codec=MJPG --max-frames=30
ctest --test-dir build --output-on-failurepython -m pip install -r requirements-test.txt
python -m pytest tests -qThe regression suite reads an exact frame count, checks the bundled clip's 640x360 geometry, transcodes eight frames, reopens the result, and exercises invalid-codec, invalid-frame-rate, and missing-file errors. It passed with Python OpenCV 4.13 and exact OpenCV 5.0.0, plus exact native C++ OpenCV 4.13.0 and exact 5.0.0.
LearnOpenCV is maintained by BigVision.AI, a computer vision and AI consulting company. We help organizations design, build, optimize, and deploy production-ready AI solutions. Our team has deep expertise in computer vision, deep learning, multimodal AI, and edge deployment, with experience solving complex technical challenges across industries.
Have a project in mind? Talk with our expert AI solution builders.

