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 command

ffmpeg -i file.avi

prints the "container" of the video stream (mpeg4, mpeg2, etc), not the codec.

Any hint?

Thanks


Solution 1:

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

Solution 2:

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$ 

Solution 3:

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.