How do you identify the codec of a .mp4 video file in Xcode?

I tried

cv::VideoCapture cap( argv[1] );
unsigned f = (unsigned)cap.get( cv::CAP_PROP_FOURCC );
cout << "f: " << f << endl;

but output of f was 0. The codec of the video file is H.264.


you should at least query, if your capture opened at all:

cv::VideoCapture cap(argv[1] );
if (! cap.isOpened()) ....

I was able to play the video using OpenCV. I’m wondering if the problem is related to ffmpeg, as suggested by the 1st answer in

no, it is not related. you’re not using a Videowriter, and while writing h264 has a liceense problem, reading is free.

since you say this is osx, the video backend might be AVFoundation, not ffmpeg.

do cap.get(cv::CAP_PROP_BACKEND)

I tried CAP_PROP_BACKEND and now the output of f is 1200. The next lines of code are:

char fourcc[] = { (char) f, (char)(f >> 8), 
                  (char)(f >> 16), (char)(f >> 24),
                  '\0' };
cout << "fourcc: " << fourcc << endl;

Output of fourcc is \260, but it should be avc1, so I don’t think CAP_PROP_BACKEND gives the fourcc codec.

it doesn’t. CAP_PROP_BACKEND tells you the backend.


all your troubles stem from that.

open a VideoCapture with explicit apiPreference… with CAP_FFMPEG

I tried:

  cv::VideoCapture cap; string(argv[1]), cv::CAP_FFMPEG );
  if( !cap.isOpened() ) {
      cout << "Couldn't open capture." << endl;
      return -1;

and the output was “Couldn’t open capture.”

std::cout << cv::getBuildInformation();

I tried:

cout << cv::getBuildInformation() << endl;

and the output was:

General configuration for OpenCV 3.4.14-dev =====================================
Version control: 3.4.14-17-g6f70b0524a

Extra modules:
Location (extra): /Users/myname/opencv_contrib/modules
Version control (extra): 3.4.14-7-g31c657e0

Timestamp: 2021-04-13T01:15:18Z
Host: Darwin 19.6.0 x86_64
CMake: 3.20.1
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: RELEASE

CPU/HW features:
Baseline: SSE SSE2 SSE3 SSSE3 SSE4_1
requested: DETECT
Dispatched code generation: SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_2 (2 files): + POPCNT SSE4_2
FP16 (1 files): + POPCNT SSE4_2 FP16 AVX
AVX (6 files): + POPCNT SSE4_2 AVX
AVX2 (30 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (7 files): + POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

Built as dynamic libs?: YES
C++ Compiler: /Library/Developer/CommandLineTools/usr/bin/c++ (ver
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /Library/Developer/CommandLineTools/usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,-dead_strip
Linker flags (Debug): -Wl,-dead_strip
ccache: NO
Precompiled headers: NO
Extra dependencies: /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/OpenGL.framework
3rdparty dependencies:

OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf java matlab ovis sfm viz
Applications: tests perf_tests examples apps
Documentation: NO
Non-free algorithms: NO

QT: YES (ver 5.15.2)
QT OpenGL support: YES (Qt5::OpenGL 5.15.2)
Cocoa: YES
OpenGL support: YES (/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/OpenGL.framework /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/OpenGL.framework)
VTK support: NO

Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 2.0.6-62)
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.37)
TIFF: build (ver 42 - 4.2.0)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 2.3.0)

Video I/O:
DC1394: NO
avcodec: NO
avformat: NO
avutil: NO
swscale: NO
avresample: NO
GStreamer: NO
AVFoundation: YES
libv4l/libv4l2: NO
v4l/v4l2: NO

Parallel framework: GCD

Trace: YES (with Intel ITT)

Other third-party libraries:
Intel IPP: 2020.0.0 Gold [2020.0.0]
at: /Users/myname/opencv/build/3rdparty/ippicv/ippicv_mac/icv
Intel IPP IW: sources (2020.0.0)
at: /Users/myname/opencv/build/3rdparty/ippicv/ippicv_mac/iw
Lapack: YES (/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl)
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)

OpenCL: YES (no extra features)
Include path: NO
Link libraries: -framework OpenCL

Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.16)
Libraries: /usr/lib/libpython2.7.dylib (ver 2.7.16)
numpy: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include (ver 1.8.0rc1)
install path: lib/python2.7/site-packages

Python 3:
Interpreter: /usr/local/bin/python3 (ver 3.9.4)
Libraries: /usr/local/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib (ver 3.9.4)
numpy: /usr/local/lib/python3.9/site-packages/numpy/core/include (ver 1.20.2)
install path: ~/.virtualenvs/OpenCV-3.4.14-py3/lib/python3.9/site-packages

Python (for build): /usr/bin/python2.7

ant: NO
Java wrappers: NO
Java tests: NO

Install to: /Users/myname/installation/OpenCV-3.4.14

so, AVFoundation is the only usable video backend on your box, and it’s quite limited


sorry, but you cannot query the video codec like that.

So in order to be able to use cv::CAP_PROP_FOURCC, I would need FFMPEG built, correct? And the way to do that is by adding the line
when using CMake to build OpenCV, as shown in step 2 of Install OpenCV 3.4.4 on macOS | LearnOpenCV, correct?

just use ffmpeg directly. look at documentation for ffprobe. it can show you all information on the container and on every contained audio/video/… stream.

if all you need is to find out the codecs in a video file, OpenCV is the wrong library for that.

“the fourcc” doesn’t exist for a video. it exists per stream, because a fourcc represents a choice of codec/bitstream. most videos have a video stream and an audio stream, which means you have two different types of bytestream.