Skip to main content
Version: Next

Decoding a video

The following codes will demux, decode and iterate a video file. The video could be in any valid format. The mpegCoder.MpegDecoder could recognize the video codec automatically.

decoding.py
import mpegCoderd = mpegCoder.MpegDecoder()opened = d.FFmpegSetup('test-video.mp4')if opened:  # If encoder is not loaded successfully, do not continue.    gop = True    while gop is not None:        gop = d.ExtractGOP()  # Extract current GOP.d.clear()  # Close the input video.

In each while loop, a Group of pictures (GOP) would be extracted. The GOP is a collection of video frames, and also the minimal data unit of the video compression algorithm. In mpegCoder, the GOP is arranged as a 4D np.ndarray. The shape (N, H, W, C) means frame number, height, width, and channel number respectively. Each frame has been converted to RGB (uint8) space. If the video reaches its end, the returned gop would be None.

Decoder rescaling

Users could configure MpegDecoder and scale the video frames. For example, the following codes would scale the frame to 720x486, no matter which picture size the video file is.

...
d = mpegCoder.MpegDecoder()
d.setParameter(widthDst=720, heightDst=486)
opened = d.FFmpegSetup('test-video.mp4')
...