【问题标题】:Writing an MP4 file on the Mac with OpenCV ffmpeg使用 OpenCV ffmpeg 在 Mac 上编写 MP4 文件
【发布时间】:2013-03-17 18:27:06
【问题描述】:

我在 Mac 上使用 OpenCV 和 ffmpeg 来编写视频。我已经能够使用编解码器/fourcc 代码 FMP4 成功编写 .avi 文件。但是,我想编写 .mp4 文件。当我尝试使用fourcc FMP4 编写.mp4 文件时,我收到此错误:

[mp4 @ 0x100b4ec00] Tag FMP4/0x34504d46 incompatible with output codec id '13' ( [0][0][0])

当我使用 AVC1 时出现以下错误:

[libx264 @ 0x104003000] broken ffmpeg default settings detected
[libx264 @ 0x104003000] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x104003000] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x104003000] speed presets are listed in x264 --help
[libx264 @ 0x104003000] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error

这里有人知道与 OpenCV 和 ffmpeg 一起使用的正确编解码器来写入 Mac 上的 MP4 容器吗?

如果 AVC1 是正确的编解码器,我该如何正确安装 ffmpeg + OpenCV?我做了

brew install gpac
brew install ffmpeg
brew install opencv

我用来打开 videowriter 的调用:

fourcc = cv2.cv.CV_FOURCC('A', 'V', 'C', '1')   
video_out = cv2.VideoWriter(
    filename=output_filename,
    fourcc=fourcc,
    fps=video_fps,
    frameSize=(video_width,video_height),
    isColor=1)

当我运行x264 --help 时,我得到了

% x264 --help
x264 core:125
Syntax: x264 [options] -o outfile infile

Infile can be raw (in which case resolution is required),
  or YUV4MPEG (*.y4m),
  or Avisynth if compiled with support (no).
  or libav* formats if compiled with lavf support (no) or ffms support (no).
Outfile type is selected by filename:
 .264 -> Raw bytestream
 .mkv -> Matroska
 .flv -> Flash Video
 .mp4 -> MP4 if compiled with GPAC support (no)
Output bit depth: 8 (configured at compile time)

Options:

  -h, --help                  List basic options
      --longhelp              List more options
      --fullhelp              List all options

Example usage:

      Constant quality mode:
            x264 --crf 24 -o <output> <input>

      Two-pass with a bitrate of 1000kbps:
            x264 --pass 1 --bitrate 1000 -o <output> <input>
            x264 --pass 2 --bitrate 1000 -o <output> <input>

      Lossless:
            x264 --qp 0 -o <output> <input>

      Maximum PSNR at the cost of speed and visual quality:
            x264 --preset placebo --tune psnr -o <output> <input>

      Constant bitrate at 1000kbps with a 2 second-buffer:
            x264 --vbv-bufsize 2000 --bitrate 1000 -o <output> <input>

Presets:

      --profile <string>      Force the limits of an H.264 profile
                                  Overrides all settings.
                                  - baseline,main,high,high10,high422,high444
      --preset <string>       Use a preset to select encoding settings [medium]
                                  Overridden by user settings.
                                  - ultrafast,superfast,veryfast,faster,fast
                                  - medium,slow,slower,veryslow,placebo
      --tune <string>         Tune the settings for a particular type of source
                              or situation
                                  Overridden by user settings.
                                  Multiple tunings are separated by commas.
                                  Only one psy tuning can be used at a time.
                                  - psy tunings: film,animation,grain,
                                                 stillimage,psnr,ssim
                                  - other tunings: fastdecode,zerolatency

Frame-type options:

  -I, --keyint <integer or "infinite"> Maximum GOP size [250]
      --tff                   Enable interlaced mode (top field first)
      --bff                   Enable interlaced mode (bottom field first)
      --pulldown <string>     Use soft pulldown to change frame rate
                                  - none, 22, 32, 64, double, triple, euro (requires cfr input)

Ratecontrol:

  -B, --bitrate <integer>     Set bitrate (kbit/s)
      --crf <float>           Quality-based VBR (0-51) [23.0]
      --vbv-maxrate <integer> Max local bitrate (kbit/s) [0]
      --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [0]
  -p, --pass <integer>        Enable multipass ratecontrol
                                  - 1: First pass, creates stats file
                                  - 2: Last pass, does not overwrite stats file

Input/Output:

  -o, --output <string>       Specify output file
      --sar width:height      Specify Sample Aspect Ratio
      --fps <float|rational>  Specify framerate
      --seek <integer>        First frame to encode
      --frames <integer>      Maximum number of frames to encode
      --level <string>        Specify level (as defined by Annex A)
      --quiet                 Quiet Mode

Filtering:

      --vf, --video-filter <filter0>/<filter1>/... Apply video filtering to the input file

      Filter options may be specified in <filter>:<option>=<value> format.

      Available filters:
      crop:left,top,right,bottom
      select_every:step,offset1[,...]

谢谢, -s

【问题讨论】:

  • 容器没有 FOURCC——只有编解码器和像素格式。 FMP4 是 MPEG-4(第 2 部分)视频。您使用的是什么视频编解码器? H.264 / MPEG-4 AVC 将是 AVC1 或其他(请参阅 fourcc.org/codecs.php
  • 嗨,@slhck,这是我的问题。写入 MP4 容器时应该使用什么视频编解码器。我已经更新了这个问题,以便在这一点上更加清楚。谢谢。
  • 请提供您正在使用的完整的未删减命令行,包括完整的输出。你确定你也安装了 x264 吗? (brew install x264; brew uninstall ffmpeg; brew install ffmpeg)
  • 您可能有兴趣查看my answer。它是用python编写的,但它是在Mac OS X上测试的。关键是CV_FOURCC('F', 'M', 'P', '4')
  • @Sameer 对此问题是否有任何更新?

标签: macos opencv ffmpeg fourcc


【解决方案1】:

这似乎是一个老问题,但我会为了那些偶然发现它的人的利益而回答它。除非您从源代码安装 ffmpeg,否则您无法让 ffmpeg 与 x264 一起使用。因此,来自包管理器的二进制安装不会启用 x264 包。您可以二进制安装 x264(但可能您想使用源代码来控制要与正确版本的 ffmpeg 兼容的 x264 版本)。

所以回答这个问题:你的安装需要先安装x264,然后用--enable-libx264编译ffmpeg。

现在有很多关于如何从源代码编译 ffmpeg 的指南。一个快速的谷歌搜索抛出了这个:http://www.martinlos.com/?p=41 for mac 和https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide for ubuntu。我经常在 ubuntu 上从源代码编译,并且知道这些指令可以正常工作,而 mac 指令看起来也不错。

【讨论】:

    【解决方案2】:

    要安装 ffmpeg,试试这个对我有用的:

    brew install --build-from-source ffmpeg

    【讨论】:

    【解决方案3】:

    在 Mac 上使用 Homebrew 安装 ffmpeg 及其所有依赖项更加容易和灵活:

    http://www.renevolution.com/how-to-install-ffmpeg-on-mac-os-x/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-09
      • 2015-08-11
      • 2020-07-15
      • 2011-07-22
      • 2018-03-10
      • 2020-10-17
      • 2018-05-31
      • 1970-01-01
      相关资源
      最近更新 更多