Mac terminal FFmpeg manual, automatic compilation

background

End mobile audio and video learning development, FFmpeg can be said that the framework will learn, FFmpeg development in linux platform, but it also can be compiled to run other operating system environments, including Windows, Mac OS X and so on. FFmpeg is a record that can be used, converting the digital audio, video, and can open it into a computer program stream, which includes the current leading audio / video encoding library libavcodec.

FFmpeg has very powerful features, including video capture function, video format conversion, video capture, add watermarks to the video. It also supports RTP way video stream to support RTSP streaming media server, support for live applications.

Applicable scene

This article describes the Mac terminal automatically, manually compile the FFmpeg library for the Mac terminal, and ultimately generate binary files (ex: ffmpeg, ffplay ...)

Mounting

It can be installed by the following three ways ffmpeg

  • Terminal installation (by means of homebrew ): This mode ffmpeg will remain automatically updated.
  • Download ffmpeg static library : the need to manually compile, we only need to run binaries can be downloaded in a statically generated drawback is the need to manually perform the update. Furthermore, they may not contain all encoders or filters required.
  • Manually compile: download the source code, and then use the mark required (can specify whether the desired function) is running ./configure, the final use makeor make install. However, you must manually set the configuration options, and you need to install their own third-party libraries.

how to choose

  • If you just want to use the command line ffmepg recommended to use the first way or the second way.
  • If you need to use Mac OS project ffmpeg, ffmpeg and modify some of the source code to accommodate the project, the use of a third way.

Specific steps

1. Use the terminal mounting FFmpeg

1.1. With Homebrewthe installationFFmpeg

  • installationHomebrew

HomebrewIs a command line package installer. The vast majority of well-known software packages or plug-in can use it to install. If you have not installed, you can directly use the following command to install

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  • installationFFmpeg

    • Generic version
    brew install ffmpeg
    复制代码
    • The latest version, and provides minimal configuration (and library dependencies) option.
    brew install ffmpeg --HEAD
    复制代码
  • Extended installation

You can also customize the installation formula, as follows, according to their own needs expand, may fail because of some need to rely on third-party libraries

brew install ffmpeg --with-fdk-aac --with-tools --with-ffplay --with-freetype --with-libass --with-libvorbis --with-opus --with-libvpx --with-x265
复制代码

1.2. Installing a third-party library ffmpeg

v After version 2.0, Homebrewis no longer available option to its core formula. Want to use other libraries (including non-free library) to build a user needs to use ffmpeg ffmpeg from third-party repositories. These libraries are not caused by Homebrewmaintenance.

E.g:

brew tap varenc/ffmpeg
brew install varenc/ffmpeg/ffmpeg
复制代码

1.3 Update

Update requires generic version and the latest version of the installation steps according to the above is to use

  • Generic version
brew update && brew upgrade ffmpeg
复制代码
  • The latest version of
brew upgrade --fetch-HEAD ffmpeg
复制代码

Note: If you already use the brew install ffmpeginstalled ffmpeg, can be used brew uninstall ffmpegto uninstall

2. manually compile

2.2 compiler environment -. Xcode

Manually compile Xcode must be installed on a Mac, and then install a command-line tool

  • Interface installation: Preferences > Downloads > Components
  • Command line installation: xcode-select --install

2.1. Dependent libraries

2.1.1 Automatic dependent libraries
$ brew install automake fdk-aac git lame libass libtool libvorbis libvpx \
opus sdl shtool texi2html theora wget x264 x265 xvid nasm
复制代码
2.1.2 Manual installation dependent libraries

Compile FFmpeg is dependent on Pkg-config, but it depends on GLib, and GLibthey rely on gettext, so you must first install and compile the relevant dependent.

  • Pkg-config & GLib & gettext
    • gettext : Edit the file in the library stpncpy.c, and #ifndef weak_aliasadd before #undef stpncpy.
    LIBFFI_CFLAGS=-I/usr/include/ffi LIBFFI_LIBS=-lffi ./configure;make && sudo make install
    复制代码
    • the GLib : Linux platform most commonly used C language library, it has good portability and practicality.
    • Config-Pkg No penalty for : maintaining a path to save each code base database. Of course, this "database" is very simple, in fact, a special directory, the directory in a series to ".pc" suffix to the file.
    GLIB_CFLAGS="-I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include" GLIB_LIBS="-lglib-2.0 -lgio-2.0" ./configure --with-pc-path="/usr/X11/lib/pkgconfig:/usr/X11/share/pkgconfig:/usr/lib/pkgconfig:/usr/local/lib/pkgconfig"
    复制代码

Mac OS X Lion has its own stpncpyfunction with its gettextconflict repeated.

  • NASM

Nasm is required x264 assembler. The latest version can be nasm.us get on.

  • Additional libraries
    • x264: --enable-gpl --enable-libx264
    • fdk-aac: --enable-libfdk-aac
    • libvpx: --enable-libvpx
    • libvorbis: --enable-libvorbis
    • libopus;
    • LAME:--enable-libmp3lame
    • libass:--enable-libass

2.3. Freetype

macOS already installed freetype (older versions may need to select the X11 during installation), but not in a typical location

In the freetype ./configureadd the following instruction file

CFLAGS=`freetype-config --cflags`
LDFLAGS=`freetype-config --libs` PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/opt/X11/lib/pkgconfig
复制代码

2.4. Compile

After finished above all dependent operation, you can download the source code for FFmpeg. By Generic compilation guide to view detailed compilation step.

Running ./configure --help, you can see the available options.

  • download
$ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
$ cd ffmpeg
复制代码
  • Compile
    • A full compilation
    
    $ ./configure  --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass \
    --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame \
    --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libopus --enable-libxvid \
    --samples=fate-suite/
    make
    复制代码
    • The most simple compiler
    
    $ ./configure  && make
    复制代码
  • installation
sudo make install
复制代码

At this point, FFmpeg installed successfully.

Note: The above process if the error, need to address further, because ffmpeg versions and their dependent libraries versions escalating, it may be some unforeseen problem, otherwise solve the prompts.

3. Detailed manually compile

3.1. The reason

ffmpeg library can have a variety of sources, such as official automatically compiled library or online personal or third-party software that comes with the compiled libraries. These libraries but has the following disadvantages

  • The old version of the binary package is outdated, it contains serious errors or missing required features that only fixed in the new version.
  • You can not customize the compiler, such as access to platform-specific library-specific optimization or link binary packages are not supported.
  • You can not change the source code, for example, need to change the source to allow ffmpeg implement customized requirements.

3.2. Step

  • Configuration script (edit the script file ./configure)
  • Compile (make)
  • Installation (make install)

1.> Configuration: allows you to create the necessary files needed to build steps, and through the configuration script source packages are usually offered complete. During configuration, you can define the installation prefix and components enabled.

2> compiled: The compiler typically comprises After running make the configuration step is complete. At this stage, it will generate the required libraries and binaries.

3> installation: the configuration specified in the step of mounting the path binaries and libraries. Note that because you can use the build path compiled binary files, and therefore does not need to perform this step.

./configure
make
make install
复制代码

Note: compiling the source files in a directory, and libraries are installed in / usr / local in. The third step may require superuser privileges (and therefore may need to be replaced by sudo make install), because ordinary users can not modify / usr / local.

3.3 installation path

Step configuration generally allows a user to specify a so-called installation prefix, and generally designated by the configuration options PREFIX = configure --prefix, wherein PREFIX typically default / usr / local. Public prefix specify the installation directory for all components.

Installation usually involves the following directories:

  • PREFIX / bin: generating a binary file containing (eg ffmpeg, ffplay, ffprobe etc. in the case of FFmpeg)
  • PREFIX / include: generating the library comprising a header (eg libavutil / avstring.h, libavcodec / avcodec.h, libavformat / avformat.h etc. in case of FFmpeg)
  • PREFIX / lib: contains the library generated (eg libavutil, libavcodec, libavformat etc. in the case of FFmpeg)
  • PREFIX / share: regardless of the various components comprising the system; particular document file and the sample

Generally recommend using the default path, but if you like to use the number / opt / PROJECT / prefix such, the project will be installed in a dedicated directory, delete from the system, simply delete the / opt / PREFIX path. But this type of installation will need to edit all environment variables to point to a custom path.

3.4. Environment Variables

Several variables defined in the environment can affect your installation package. In particular, according to your installation prefix, you may need to update some of these variables to ensure that the system can find the tool installed components. Command can envdisplay a list of environment variables.

The following is a list of the affected variables:

  • PATH: path to simplify the definition of variables, find the path to the binary system. For example, if installed in / usr / local / in the package, you should update PATH, to include / usr / local / bin. This is done by command export PATH = / usr / local / bin: to complete the $ PATH.
  • LD_LIBRARY_PATH: path to find the library system. For example, if installed in / usr / local / in the package, you should update LD_LIBRARY_PATH, to include / usr / local / lib. This command can export LD_LIBRARY_PATH = / usr / local / lib: to complete the $ LD_LIBRARY_PATH. Sometimes this variable is not recommended, but the use ldconfig.
  • CFLAGS: comprising flag C compiler used, typically includes a preprocessing instructions, such as -IPREFIX / include or compilation flags. CFLAGS build custom systems typically source packet is prefixed by a compiler flags source packets. Or, build system allows you to specify a number of configuration options -extra-cflags.
  • LDFLAGS: These instructions are used by the linker, typically including a link instructions, such as finding the required library -LPREFIX / lib custom installation path. Custom LDFLAGS typically constructed from the source packet system source packet as a prefix flag linker. Or, build system allows you to specify configure many options -extra-ldflags.
  • PKG_CONFIG_PATH: pkg-config path used for detecting many pkg-config file compilation system used to detect specific custom libraries used CFLAGS / LDFLAGS.

If you install the package in a non-standard path, you need to update the database environment so that the system can detect tools package components. When you configure a script to run the package depends on other installed libraries / header / tools, you must do so.

A brief description of the use of

  • Common Library
Library name use
ffmpeg A command-line tool to convert video file formats, and also supports real-time encoding TV cards
ffserver A real-time broadcast streaming multimedia HTTP server that supports time shifting
ffplay A simple player based on SDL and the FFmpeg library
libavcodec It contains all the FFmpeg audio / video codec library
libavformat And muxer libraries contain demuxers
libavutil It includes tools library
libpostproc For video library to do before processing
libavutil It includes tools library
libswscale libswscale
  • The main parameters
parameter name significance
-i Set the input file name.
-f Set the output format.
-Y If the output file is overwritten when the file already exists.
-fs The end of the conversion exceeds the specified file size.
-ss Start conversion from the specified time.
-t -Ss time from start conversion (e.g. -ss 00: 00: 01.00 -t 00: 00: 10.00 ie, from 00: 00: 01.00 begin to 00: 00: 11.00).
-title Set the title.
-timestamp Set a time stamp.
-vsync Frame changes in the audio and video synchronization.
Video parameter name significance
b:v Set video traffic, the default is 200Kbit / sec. (Please quote the unit under the Notes)
r Set the frame rate, default is 25.
s Width and height of the screen is provided.
aspect Setting the ratio of the screen.
vn It does not handle video, when used for voice only do processing.
vcodec( -c:v ) Video video codec is provided, then the same input file using the codec is not provided.
Sound parameter name significance
b:a Setting per Channel (the most recent version of SVN is the sum of all Channel) traffic. (Please quote the unit under the Notes)
With Set the sample rate.
ac Set the number of Channel sound.
acodec ( -c:a ) Set sound codec, the same video is not set, the input file using the same codec.
an 不处理声音,于仅针对视频做处理时使用。
vol 设置音量大小,256为标准音量。(要设置成两倍音量时则输入512,依此类推。)
  • ffprobe

一个编码后的文件是否正确就用以下命令,如果格式错误的话会输出一堆Error。

$ ffprobe -show_frames /xx.h265

$ ffprobe -print_format json -show_format -show_streams -i /xx.h265

复制代码
  • ffmpeg

查看文件基本信息

$ ffmpeg -i /xx.h265

复制代码
  • ffplay

ffplay逐帧播放视频与显示视频帧序号

目前使用这个功能检查部分画面不正确视频的原因。macOS下使用ffplay,按下s键可单帧播放视频,配合一个显示文字的视频滤镜即可显示当前画面的帧序号,命令示例如下所示。

$ ffplay -vf "drawtext=fontfile=/Library/Fonts/Arial.ttf:text=%{n}:box=1:x=(w-tw)/2:y=h-(2*lh)" xx.mp4
复制代码

参考文档

转载于:https://juejin.im/post/5cefe31a6fb9a07ea4206298

Guess you like

Origin blog.csdn.net/weixin_34056162/article/details/91444249
Recommended