【问题标题】:How can I know a certain file is a video file?我怎么知道某个文件是视频文件?
【发布时间】:2019-10-17 06:14:01
【问题描述】:

我正在尝试确定某个用户上传的文件是否是视频文件。

我第一次尝试了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>

但很难弄清楚是什么。甚至图像文件和文本文件也算作视频。

我应该将输出 matroska,webm, 与 ffmpeg 支持的每个编解码器进行比较,还是有更好的方法来做到这一点?

【问题讨论】:

    标签: ffmpeg libavcodec ffprobe


    【解决方案1】:

    使用ffprobe

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

    输出video 或根本不输出。

    问题在于ffprobe 将图像视为视频,因此您可以另外/替代地使用codec_name 来帮助确定类型:

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

    输出:

    codec_name=png
    codec_type=video
    

    【讨论】:

    • 与其看codec_name,不如看bit_rate。如果流是图像,则 bit_rate 将为“N/A”,而如果流是视频,则 bit_rate 将为数字。
    • @Mitchell 好主意,但 .mkv 和 .webm 文件也可能会显示流 bit_rate 的 N/A。也许另一个解决方案是 count the frames 其中 1 = 图像。
    【解决方案2】:

    假设您的系统支持file 命令,您可以通过-i, --mime 选项获取文件的mime 类型并在使用ffmpeg 处理之前将其隔离:

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

    Credit 用于cut 命令)。

    【讨论】:

    • 我猜它有其局限性......也许 llogan 解决方案更适合您的使用。
    猜你喜欢
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多