GerryMulligan
GerryMulligan

Reputation: 751

How to determine video codec of a file with FFmpeg

I often have problems reading AVI files with my TV's DVD player if they are not DivX or Xvid (e.g., DX50 is not readable). I'd like to make a fast script to determine the video codec of these files before burning them to CD-ROM or DVD.

The following prints the "container" of the video stream (mpeg4, mpeg2, etc), but not the codec:

ffmpeg -i file.avi

Upvotes: 75

Views: 120500

Answers (5)

Velda
Velda

Reputation: 617

With the current version of ffmpeg it is enough to run ffmpeg -i <my-video-file.mp4> to produce:

  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2023-05-03T15:06:15.000000Z
    encoder         : HandBrake 1.2.2 2019022300
  Duration: 00:28:45.93, start: 0.000000, bitrate: 381 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 243 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : SoundHandler

So you can see this particular file contains audio encoded in AAC-LC and video in H.264. No encoding is done, so the operation is immediate.

ffmpeg version 4.2

Upvotes: 1

qubodup
qubodup

Reputation: 9633

mediainfo

mediainfo --Inform="Video;%Codec%" video.mkv

will in my case return:

V_MPEG4/ISO/AVC

Answer made possible thanks to How to find duration of a video file using mediainfo in seconds or other formats?

ffprobe (ffmpeg) easy way

Assuming your video has one video stream only:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 video.mkv

Will in my case return:

h264

Answer made possible thanks to How to get video duration in seconds?

ffprobe (ffmpeg) dirty way

This method is easier to understand but messy.

To get the codec information without playing back the file, use ffprobe.

$ ffprobe video.mkv
[...]
Input #0, matroska,webm, from 'video.mkv':
  Metadata:
    ENCODER         : Lavf56.25.101
  Duration: 00:28:05.15, start: 0.000000, bitrate: 4353 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libx264
    Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libvorbis

To extract the video codec information - since ffmpeg sends information to stderr - pipe and grep it:

$ ffprobe video.mkv 2>&1 >/dev/null | grep Stream.*Video
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)

To reduce the output even further, introduce sed:

$ ffprobe video.mkv 2>&1 >/dev/null |grep Stream.*Video | sed -e 's/.*Video: //' -e 's/[, ].*//'
h264

Upvotes: 132

j2emanue
j2emanue

Reputation: 62549

ffmpeg has it. On mac i did it this way :

first download ffmpeg like this:

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

and then run this on the command line:

ffmpeg -filter:v idet \
    -frames:v 100 \
    -an \
    -f rawvideo -y /dev/null \
    -i ~/Downloads/yourfile.mp4

then check for something like this in the output:

Duration: 00:00:05.48, start: 0.000000, bitrate: 952 kb/s Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 750x1334, 619 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)

Upvotes: 1

Darren Greaves
Darren Greaves

Reputation: 3344

Try MediaInfo instead.

It lists the codec for each stream and its output is simple enough to parse - there's also an XML output option if you prefer XPath like queries.

Upvotes: 2

Stu Thompson
Stu Thompson

Reputation: 38908

FFmpeg gives the codec too. Pull the Stream #0.0: Video line and you can see the codec. (Be aware that it could technically have a different stream number, like 0.1.) The below output uses the MS Video-1. This is different, like you desire, from the container which is denoted by Input #0, avi

E.g.:

FFmpeg version 0.5, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --prefix=/opt/local --disable-vhook --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-avfilter-lavf --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libfaac --enable-libfaad --enable-libxvid --enable-libx264 --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/gcc-4.2 --arch=x86_64
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    1. 4. 0 /  1. 4. 0
  libswscale     1. 7. 1 /  1. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jan  8 2010 15:34:15, gcc: 4.2.1 (Apple Inc. build 5646) (dot 1)
Input #0, avi, from 'Movies/fvss_demo.avi':
  Duration: 00:02:00.30, start: 0.000000, bitrate: 719 kb/s
    Stream #0.0: Video: msvideo1, rgb555, 160x120, 10 tbr, 10 tbn, 10 tbc
    Stream #0.1: Audio: pcm_u8, 8000 Hz, mono, s16, 64 kb/s
At least one output file must be specified
manoa:~ stu$ 

Upvotes: 8

Related Questions