如何使用 FFmpeg 确定文件的视频编解码器

发布于 2024-09-02 07:37:20 字数 215 浏览 4 评论 0原文

如果 AVI 文件不是 DivX 或 Xvid(例如,DX50 不可读),我在使用电视 DVD 播放器读取 AVI 文件时经常会遇到问题。我想制作一个快速脚本来确定这些文件的视频编解码器,然后再将它们刻录到 CD-ROM 或 DVD。

以下内容打印视频流的“容器”(mpeg4、mpeg2 等),但不打印编解码器:

ffmpeg -i file.avi

I often have problems reading AVI files with my TV's DVD player if they are not DivX or Xvid (e.g., DX50 is not readable). I'd like to make a fast script to determine the video codec of these files before burning them to CD-ROM or DVD.

The following prints the "container" of the video stream (mpeg4, mpeg2, etc), but not the codec:

ffmpeg -i file.avi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满地尘埃落定 2024-09-09 07:37:20

mediainfo

mediainfo --Inform="Video;%Codec%" video.mkv

在我的情况下将返回:

V_MPEG4/ISO/AVC

感谢 如何使用 mediainfo 以秒为单位或其他格式查找视频文件的持续时间?

ffprobe (ffmpeg) 简单方法

假设您的视频只有一个视频流:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 video.mkv

在我的情况下将返回:

h264

感谢 如何获取以秒为单位的视频持续时间?

ffprobe (ffmpeg) dirty way

这种方法更容易理解,但是 使答案成为可能凌乱的。

要获取编解码器信息而不播放文件,请使用 ffprobe

$ ffprobe video.mkv
[...]
Input #0, matroska,webm, from 'video.mkv':
  Metadata:
    ENCODER         : Lavf56.25.101
  Duration: 00:28:05.15, start: 0.000000, bitrate: 4353 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libx264
    Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libvorbis

要提取视频编解码器信息 - 由于 ffmpeg 将信息发送到 stderr - pipe 并 grep it:

$ ffprobe video.mkv 2>&1 >/dev/null | grep Stream.*Video
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)

为了进一步减少输出,引入 sed:

$ ffprobe video.mkv 2>&1 >/dev/null |grep Stream.*Video | sed -e 's/.*Video: //' -e 's/[, ].*//'
h264

mediainfo

mediainfo --Inform="Video;%Codec%" video.mkv

will in my case return:

V_MPEG4/ISO/AVC

Answer made possible thanks to How to find duration of a video file using mediainfo in seconds or other formats?

ffprobe (ffmpeg) easy way

Assuming your video has one video stream only:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 video.mkv

Will in my case return:

h264

Answer made possible thanks to How to get video duration in seconds?

ffprobe (ffmpeg) dirty way

This method is easier to understand but messy.

To get the codec information without playing back the file, use ffprobe.

$ ffprobe video.mkv
[...]
Input #0, matroska,webm, from 'video.mkv':
  Metadata:
    ENCODER         : Lavf56.25.101
  Duration: 00:28:05.15, start: 0.000000, bitrate: 4353 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libx264
    Stream #0:1: Audio: vorbis, 48000 Hz, stereo, fltp (default)
    Metadata:
      ENCODER         : Lavc56.26.100 libvorbis

To extract the video codec information - since ffmpeg sends information to stderr - pipe and grep it:

$ ffprobe video.mkv 2>&1 >/dev/null | grep Stream.*Video
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1280x960, SAR 1:1 DAR 4:3, 29.97 fps, 29.97 tbr, 1k tbn, 59.94 tbc (default)

To reduce the output even further, introduce sed:

$ ffprobe video.mkv 2>&1 >/dev/null |grep Stream.*Video | sed -e 's/.*Video: //' -e 's/[, ].*//'
h264
怪我鬧 2024-09-09 07:37:20

FFmpeg 也提供了编解码器。拉动 Stream #0.0: Video 行,您可以看到编解码器。 (请注意,从技术上讲,它可能具有不同的流编号,例如 0.1。)以下输出使用 MS Video-1。正如您所希望的,这与 Input #0, avi 表示的容器不同,

例如:

FFmpeg version 0.5, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --prefix=/opt/local --disable-vhook --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-avfilter-lavf --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libfaac --enable-libfaad --enable-libxvid --enable-libx264 --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/gcc-4.2 --arch=x86_64
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    1. 4. 0 /  1. 4. 0
  libswscale     1. 7. 1 /  1. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jan  8 2010 15:34:15, gcc: 4.2.1 (Apple Inc. build 5646) (dot 1)
Input #0, avi, from 'Movies/fvss_demo.avi':
  Duration: 00:02:00.30, start: 0.000000, bitrate: 719 kb/s
    Stream #0.0: Video: msvideo1, rgb555, 160x120, 10 tbr, 10 tbn, 10 tbc
    Stream #0.1: Audio: pcm_u8, 8000 Hz, mono, s16, 64 kb/s
At least one output file must be specified
manoa:~ stu$ 

FFmpeg gives the codec too. Pull the Stream #0.0: Video line and you can see the codec. (Be aware that it could technically have a different stream number, like 0.1.) The below output uses the MS Video-1. This is different, like you desire, from the container which is denoted by Input #0, avi

E.g.:

FFmpeg version 0.5, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --prefix=/opt/local --disable-vhook --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-avfilter-lavf --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libdirac --enable-libschroedinger --enable-libfaac --enable-libfaad --enable-libxvid --enable-libx264 --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/gcc-4.2 --arch=x86_64
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 0 / 52.20. 0
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    1. 4. 0 /  1. 4. 0
  libswscale     1. 7. 1 /  1. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Jan  8 2010 15:34:15, gcc: 4.2.1 (Apple Inc. build 5646) (dot 1)
Input #0, avi, from 'Movies/fvss_demo.avi':
  Duration: 00:02:00.30, start: 0.000000, bitrate: 719 kb/s
    Stream #0.0: Video: msvideo1, rgb555, 160x120, 10 tbr, 10 tbn, 10 tbc
    Stream #0.1: Audio: pcm_u8, 8000 Hz, mono, s16, 64 kb/s
At least one output file must be specified
manoa:~ stu$ 
指尖微凉心微凉 2024-09-09 07:37:20

请尝试使用 MediaInfo

它列出了每个流的编解码器,其输出足够简单,易于解析 - 如果您更喜欢类似 XPath 的查询,还有一个 XML 输出选项。

Try MediaInfo instead.

It lists the codec for each stream and its output is simple enough to parse - there's also an XML output option if you prefer XPath like queries.

仙女山的月亮 2024-09-09 07:37:20

ffmpeg 有它。在Mac上我是这样做的:

首先像这样下载ffmpeg:

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

然后在命令行上运行它:

ffmpeg -filter:v idet \
    -frames:v 100 \
    -an \
    -f rawvideo -y /dev/null \
    -i ~/Downloads/yourfile.mp4

然后在输出中检查类似的内容:

持续时间:00:00:05.48,开始:0.000000,比特率:952 kb / s
流 #0:0(eng):视频:h264(主)(avc1 / 0x31637661)、yuv420p(tv、bt709)、750x1334、619 kb/s、29.97 fps、29.97 tbr、30k tbn , 59.94 待定(默认)

ffmpeg has it. On mac i did it this way :

first download ffmpeg like this:

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

and then run this on the command line:

ffmpeg -filter:v idet \
    -frames:v 100 \
    -an \
    -f rawvideo -y /dev/null \
    -i ~/Downloads/yourfile.mp4

then check for something like this in the output:

Duration: 00:00:05.48, start: 0.000000, bitrate: 952 kb/s
Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 750x1334, 619 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)

丘比特射中我 2024-09-09 07:37:20

使用当前版本的 ffmpeg,运行 ffmpeg -i即可生成:

  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2023-05-03T15:06:15.000000Z
    encoder         : HandBrake 1.2.2 2019022300
  Duration: 00:28:45.93, start: 0.000000, bitrate: 381 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 243 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : SoundHandler

因此您可以看到此特定文件包含以 AAC-LC 编码的音频和视频在 H.264 中。没有进行编码,因此操作是立即的。

ffmpeg 版本 4.2

With the current version of ffmpeg it is enough to run ffmpeg -i <my-video-file.mp4> to produce:

  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2023-05-03T15:06:15.000000Z
    encoder         : HandBrake 1.2.2 2019022300
  Duration: 00:28:45.93, start: 0.000000, bitrate: 381 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 243 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 129 kb/s (default)
    Metadata:
      creation_time   : 2023-05-03T15:06:15.000000Z
      handler_name    : SoundHandler

So you can see this particular file contains audio encoded in AAC-LC and video in H.264. No encoding is done, so the operation is immediate.

ffmpeg version 4.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文