How can I know a certain file is a video file?

可紊 提交于 2021-01-28 12:09:26

问题


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?


回答1:


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



回答2:


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).



来源:https://stackoverflow.com/questions/56397732/how-can-i-know-a-certain-file-is-a-video-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!