4

I have a directory with many files of different types and want to find out the codec of each video file. This works well with this command:

$ ffprobe -v error -select_streams v -show_entries stream=codec_name,codec_type -of default=noprint_wrappers=1 video-file
codec_name=hevc
codec_type=video

But unfortunately I also get a result for JPG and text files:

$ ffprobe -v error -select_streams v -show_entries stream=codec_name,codec_type -of default=noprint_wrappers=1 file.jpg 
codec_name=mjpeg
codec_type=video
$ ffprobe -v error -select_streams v -show_entries stream=codec_name,codec_type -of default=noprint_wrappers=1 file.txt
codec_name=ansi
codec_type=video

Both files are clearly not video files. Well, I could filter out the file extensions or blacklist these codec names. But isn't there a proper way with ffprobe?

2 Answers 2

4

FFmpeg is primarily a processor of timed media samples. An image is a video stream consisting of one frame. Checking for pipe in format=format_name entry is your best bet for detecting images.

As far as text files go, ffmpeg converts the .txt contents to a video image, and the codec_type indicates the decoded stream type. You can check format_name again for tty.

1
  • Thanks, that helped me a lot to understand the problem. Do you know other non-video formats where ffmpeg recognizes a video stream? Jul 2, 2018 at 16:37
0

With the help of Gyan's comment I created this little function for the fish shell.

function gvc --description 'Get the video codec of a file if the file is a video' --argument file
    if test -f "$file"
        set format (ffprobe -v panic -select_streams v -show_entries format=format_name -of default=nokey=1:noprint_wrappers=1 $file)
        if test -n "$format" -a "$format" != "image2" -a "$format" != "tty"
            echo (ffprobe -v error -select_streams v -show_entries stream=codec_name -of default=nokey=1:noprint_wrappers=1 $file)
        end
    end
    return $status
end

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .