Skip to content

VideoCapture (CAP_FFMPEG) returns the incorrect fourcc codec due to codec_tag representing the underlying stream type #22876

@cudawarped

Description

@cudawarped

System Information

OpenCV version: 4.6.0
Operating System / Platform: Windows 11
Compiler & compiler version: Visual Studio 2022

Detailed description

VideoCapture with the FFmpeg back end returns the incorrect codec fourcc when the codec_tag represents the underlying stream type.

The existing logic is to return the codec_tag if it exists and igored the codec_id

if(codec_tag || codec_id == AV_CODEC_ID_NONE)

This is fine if the codec_tag is valid but, if it is invalid and the codec_id is valid this logic fails.

For example this MPEG-TS video has a valid
codec_id== AV_CODEC_ID_HEVC (173)
and invalid
codec_tag ==36
the MPEG-TS stream type.

Additonaly because the codec_tag is

/**
 * Additional information about the codec (corresponds to the AVI FOURCC).
 */
uint32_t         codec_tag;

the fourcc returned for the three test video's below is mp4v

highgui/video/big_buck_bunny.mp4
highgui/video/sample_322x242_15frames.yuv420p.mpeg2video.mp4
highgui/video/sample_322x242_15frames.yuv420p.mjpeg.mp4

when they use MPEG-4, MPEG-2 and MJPG respectively.

I can think of easy fixes:

  1. Only use the codec_tag if the codec_id is unavaliable. This may pass all the test cases but break existing user code.
  2. Add an additional property, i.e. CAP_PROP_CODEC_ID_FOURCC or similar, which a user can fall back on if the fourcc returned from CAP_PROP_FOURCC is garbage. This is clunky but it is guaranteed not to break any existing user code.

Steps to reproduce

Video from https://4kmedia.org/lg-new-york-hdr-uhd-4k-demo/

VideoCapture cap("LG New York HDR UHD 4K Demo.ts",CAP_FFMPEG);
const double fourcc = cap.get(CAP_PROP_FOURCC);

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions