40

I am planning to use ffmpeg to ensure all video files uploaded to my website are encoded as mp4 h264.

Rather than automatically processing every file I would like to minimise the processing overhead by only processing those files that are not already mp4 h264. Is there an easy way to do this either with ffmpeg or with another command line utility?

Bill Noble
  • 463
  • 1
  • 5
  • 5
  • 2
    Possible duplicate of [how to recognize video codec of a file with ffmpeg](https://stackoverflow.com/questions/2869281/how-to-recognize-video-codec-of-a-file-with-ffmpeg) – GSerg Jul 29 '19 at 09:14

4 Answers4

61

Use ffprobe

Example command

$ ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 input.mp4

Result

h264

Option descriptions

  • -v error Omit extra information except for fatal errors.

  • -select_streams v:0 Select only the first video stream. Otherwise the codec_name for all other streams in the file, such as audio, will be shown as well.

  • -show_entries stream=codec_name Only output the codec_name instead of all stream info.

  • -of default=nokey=1:noprint_wrappers=1 Select the default output format style and omit the key and wrapper info. Otherwise, without these options, it will output:

      [STREAM]
      codec_name=h264
      [/STREAM]
    

Also see

Bash script example

Only re-encode if video is not H.264:

#!/bin/bash

mkdir h264vids

for f in *.mkv
do
  audioformat=$(ffprobe -loglevel error -select_streams v:0 -show_entries stream=codec_name -of default=nw=1:nk=1 "$f")
  if [ "$audioformat" = "h264" ]; then
    ffmpeg -i "$f" -c:v copy -c:a aac -movflags +faststart h264vids/"${f%.*}.mp4"
  else
    ffmpeg -i "$f" -c:v libx264 -c:a aac -movflags +faststart h264vids/"${f%.*}.mp4"
  fi
done

This is a simple script and will ignore additional video streams if you input has more than one.

llogan
  • 121,796
  • 28
  • 232
  • 243
35

If you pass an input file to ffmpeg, without other parameters, it will give you information about the video source:

ffmpeg -i myfile.avi

Another way would be the -identify option of mplayer, which might be slightly easier to parse. There is a wrapper script midentify which gives you even better output. See this example.

pixelistik
  • 7,541
  • 3
  • 32
  • 42
  • Great that gives me the info I need. Would like not to have to parse the output though :) – Bill Noble Apr 11 '11 at 11:27
  • MediaInfo can do this and has optional XML output. http://mediainfo.sourceforge.net/en - grab the command line version. – Darren Greaves Jan 23 '12 at 10:41
  • 8
    ffmpeg package includes command `ffprobe` that supports several output formats such as xml and json. Eases parsing of ffmpeg output – mente Oct 02 '13 at 13:39
7

An alternative is to use ffprobe which is included with ffmpeg. The following will give the most terse output I can find from the ffmpeg tools:

ffprobe -hide_banner -stats -i myfile.avi
Timothy C. Quinn
  • 3,739
  • 1
  • 35
  • 47
2

As llogan said above, let me simplify it for other new leaners,video265.mp4 is your video file:

ffprobe -v error -show_entries stream=codec_name video265.mp4

Result:

[STREAM]
codec_name=hevc
[/STREAM]
Chinese Cat
  • 5,359
  • 2
  • 16
  • 17