Generating Video in Python with OpenCV

These Python scripts demonstrates algorithmic approaches to creating video content for the Pausch Bridge. These are parameterized but are not interactive. The scripts use the Python bindings for the OpenCV library available via pip as opencv-contrib-python. This package includes the ffmpeg video codecs used to generate the output.

Installation Requirements

The code requires a working installation of Python 3 with OpenCV and its dependency NumPy. For suggestions on setting up your system please see Python 3 Installation.

Video Output

The default output format is the somewhat obscure ‘lossless PNG in AVI wrapper’ for clean pixel block edges without artifacts. This can be converted to a more web-friendly format using the command line ffmpeg tool. For example:

ffmpeg -i color_bars.avi -codec:v libx264 -pix_fmt yuv420p -vf scale=456:16 color_bars.mp4

pb_color_bars.py

Generate a slowly chasing sequence of standard color bars. This generates a video file using the lossless ‘PNG in AVI’ codec to avoid edge aliasing.

Script code: pb_color_bars.py

Sample output file: color_bars.avi

Sample preview:

pb_color_cycle.py

Generate a smoothly changing solid color field. This generates an MPEG-4 video since there is no concern about edge aliasing.

Script code: pb_color_cycle.py

Sample output file: color_cycle.mp4

Sample preview:

pb_random.py

Generate a slowly changing random-dot color noise pattern. This generates an MPEG-4 video even though the result is distinctly blurred by the encoding.

Script code: pb_random.py

Sample output file: random.mp4

Sample preview:

pb_slate_bars.py

Generate a ‘slate’ sequence using a color-bar code based on the resistor color code. The clip fades up from black to a static color code image, holds it, fades back to black, and holds briefly on black. This can be useful as a prefix for marking another sequence.

../_images/slate-12345.png

Sample color-code ‘slate’ image encoding the value 12345.

Script code: pb_slate_bars.py

Sample output file: slate_bars.avi

Sample preview: