1

I am trying to figure out if a certain user-uploaded file is a video file.

I first tried ffprobe,

# a png file

Input #0, png_pipe, from '<file>':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgba(pc), 920x2094 [SAR 4724:4724 DAR 460:1047], 25 tbr, 25 tbn, 25 tbc

# a text file

Input #0, tty, from '<file>':
  Duration: 00:00:00.24, bitrate: 40 kb/s
    Stream #0:0: Video: ansi, pal8, 640x400, 25 fps, 25 tbr, 25 tbn, 25 tbc

# a video file

Input #0, matroska,webm, from '<file>':
  Metadata:
    encoder         : libebml v1.3.5 + libmatroska v1.4.8
    creation_time   : 2017-12-12T20:18:42.000000Z
  <redacted>

but it's too hard to figure out what's what. Even image files and text files count as a video.

Should I compare the output matroska,webm, with every codec ffmpeg supports or is there a better way to do this?

2 Answers2

0

Assuming your system supports the file command, you can pass -i, --mime option to get the mime type of the file and isolate it before processing with ffmpeg:

# a video file

$ file -i movie.mp4 | cut -d ' ' -f2 | cut -d '/' -f1
--> video

(Credit for cut command).

TGrif
  • 5,725
  • 9
  • 31
  • 52
  • It has its limits I guess... Maybe _llogan_ solution is more suitable for your usage anyway. – TGrif May 31 '19 at 18:19
0

Using ffprobe:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_type -of csv=p=0 input.mkv

Outputs either video or no output at all.

The problem is that ffprobe considers images to be video, so you can additionally/alternatively use codec_name to help determine the type:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,codec_type -of default=nw=1 input.png

Outputs:

codec_name=png
codec_type=video
llogan
  • 121,796
  • 28
  • 232
  • 243
  • Instead of looking at the codec_name, it's better to look at bit_rate. If the stream is an image, the bit_rate will be 'N/A' whereas if the stream is video the bit_rate will be numerical. – Mitchell Dec 11 '20 at 16:45
  • @Mitchell A good idea, but .mkv and .webm files will likely show N/A for stream bit_rate as well. Perhaps another solution is to [count the frames](https://stackoverflow.com/a/28376817/) where 1 = image. – llogan Dec 11 '20 at 17:53
  • just tested and a static jpeg file has a bitrate and duration. – reticivis Feb 27 '22 at 04:43