SPYOFF VPN

2017-07-09


FFmpeg 是什么?

官网上这样描述的: A complete, cross-platform solution to record, convert and stream audio and video.

简单来说,FFmpeg 是一种完整的、跨平台的音视频处理方案。可以处理音频和视频的录制、转换和流功能

官网地址:FFmpeg

下面先来简单入门。

  • 安装

    这个地址 介绍了 Linux、OS X、Windows 还有其他平台的安装方法,具体可以按照自己电脑平台进行安装。

    因为我使用的是 Mac,所以为了方便选择通过 Homebrew 来安装。

    Homebrew 是一个命令行包管理器,类似于 Linux 发行版上的 apt-get

    • 安装 Homebrew

      如果你还没安装 Homebrew,可以通过 Terminal 快速安装。

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

      安装好 Homebrew 之后,可以通过 Terminal 键入如下命令安装 FFmpeg。

      brew install ffmpeg --with-fdk-aac --with-tools --with-freetype --with-libass --with-libvorbis --with-libvpx --with-x265 --with-webp
      

      安装好之后可以通过 brew info ffmpeg 查看安装 FFmpeg 的具体信息,下面是信息输出。

      ffmpeg: stable 3.3.2 (bottled), HEAD
      Play, record, convert, and stream audio and video
      https://ffmpeg.org/
      /usr/local/Cellar/ffmpeg/3.3.2 (273 files, 52.8MB) *
        Built from source on 2017-06-27 at 20:19:48 with: --with-fdk-aac --with-libass --with-tools --with-webp --with-x265 --with-freetype --with-libvorbis --with-libvpx
      From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/ffmpeg.rb
      ==> Dependencies
      Build: pkg-config ✔, texi2html ✔, yasm ✔
      Recommended: lame ✔, x264 ✔, xvid ✔
      Optional: chromaprint ✘, fdk-aac ✔, fontconfig ✔, freetype ✔, frei0r ✘, game-music-emu ✘, libass ✔, libbluray ✘, libbs2b ✘, libcaca ✘, libgsm ✘, libmodplug ✘, libsoxr ✘, libssh ✘, libvidstab ✘, libvorbis ✔, libvpx ✔, opencore-amr ✘, openh264 ✘, openjpeg ✘, openssl ✔, opus ✘, rtmpdump ✘, rubberband ✘, schroedinger ✘, sdl2 ✘, snappy ✘, speex ✘, tesseract ✘, theora ✘, two-lame ✘, wavpack ✘, webp ✔, x265 ✔, xz ✘, zeromq ✘, zimg ✘
      ==> Options
      --with-chromaprint
      	Enable the Chromaprint audio fingerprinting library
      --with-fdk-aac
      	Enable the Fraunhofer FDK AAC library
      --with-fontconfig
      	Build with fontconfig support
      --with-freetype
      	Build with freetype support
      --with-frei0r
      	Build with frei0r support
      --with-game-music-emu
      	Build with game-music-emu support
      --with-libass
      	Enable ASS/SSA subtitle format
      --with-libbluray
      	Build with libbluray support
      --with-libbs2b
      	Build with libbs2b support
      --with-libcaca
      	Build with libcaca support
      --with-libgsm
      	Build with libgsm support
      --with-libmodplug
      	Build with libmodplug support
      --with-libsoxr
      	Enable the soxr resample library
      --with-libssh
      	Enable SFTP protocol via libssh
      --with-libvidstab
      	Enable vid.stab support for video stabilization
      --with-libvorbis
      	Build with libvorbis support
      --with-libvpx
      	Build with libvpx support
      --with-opencore-amr
      	Enable Opencore AMR NR/WB audio format
      --with-openh264
      	Enable OpenH264 library
      --with-openjpeg
      	Enable JPEG 2000 image format
      --with-openssl
      	Enable SSL support
      --with-opus
      	Build with opus support
      --with-rtmpdump
      	Enable RTMP protocol
      --with-rubberband
      	Enable rubberband library
      --with-schroedinger
      	Enable Dirac video format
      --with-sdl2
      	Enable FFplay media player
      --with-snappy
      	Enable Snappy library
      --with-speex
      	Build with speex support
      --with-tesseract
      	Enable the tesseract OCR engine
      --with-theora
      	Build with theora support
      --with-tools
      	Enable additional FFmpeg tools
      --with-two-lame
      	Build with two-lame support
      --with-wavpack
      	Build with wavpack support
      --with-webp
      	Enable using libwebp to encode WEBP images
      --with-x265
      	Enable x265 encoder
      --with-xz
      	Enable decoding of LZMA-compressed TIFF files
      --with-zeromq
      	Enable using libzeromq to receive commands sent through a libzeromq client
      --with-zimg
      	Enable z.lib zimg library
      --without-lame
      	Disable MP3 encoder
      --without-qtkit
      	Disable deprecated QuickTime framework
      --without-securetransport
      	Disable use of SecureTransport
      --without-x264
      	Disable H.264 encoder
      --without-xvid
      	Disable Xvid MPEG-4 video encoder
      --HEAD
      	Install HEAD version
      

      可以通过 brew update && brew upgrade ffmpeg 把 FFmpeg 更新到最新版本。

      还可以通过 brew reinstall ffmpeg --with-webp 重新安装 FFmpeg,并带上 webp 处理模块。其中 --with-webp 是上面 ==> Options 列出的选项之一。

  • 玩玩

    通过 Terminal 运行 ffmpeg 查看输出:

    ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
      built with Apple LLVM version 8.1.0 (clang-802.0.42)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --disable-lzma --enable-nonfree --enable-vda
      libavutil      55. 58.100 / 55. 58.100
      libavcodec     57. 89.100 / 57. 89.100
      libavformat    57. 71.100 / 57. 71.100
      libavdevice    57.  6.100 / 57.  6.100
      libavfilter     6. 82.100 /  6. 82.100
      libavresample   3.  5.  0 /  3.  5.  0
      libswscale      4.  6.100 /  4.  6.100
      libswresample   2.  7.100 /  2.  7.100
      libpostproc    54.  5.100 / 54.  5.100
    Hyper fast Audio and Video encoder
    usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
    
    Use -h to get full help or, even better, run 'man ffmpeg'
    

    第一行输出了 FFmpeg 的版本信息,接下来第三行输出了编译选项,倒数第二行列出了 FFmpeg 命令行的使用格式, 最后一行列出了 FFmpeg 的帮助命令说明,比如 ffmpeg -h,更详细的文档命令 man ffmpeg

    我们看看 ffmpeg -h 都列出了哪些命令说明:

    ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
      built with Apple LLVM version 8.1.0 (clang-802.0.42)
      configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --disable-lzma --enable-nonfree --enable-vda
      libavutil      55. 58.100 / 55. 58.100
      libavcodec     57. 89.100 / 57. 89.100
      libavformat    57. 71.100 / 57. 71.100
      libavdevice    57.  6.100 / 57.  6.100
      libavfilter     6. 82.100 /  6. 82.100
      libavresample   3.  5.  0 /  3.  5.  0
      libswscale      4.  6.100 /  4.  6.100
      libswresample   2.  7.100 /  2.  7.100
      libpostproc    54.  5.100 / 54.  5.100
    Hyper fast Audio and Video encoder
    usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
    
    Getting help:
        -h      -- print basic options
        -h long -- print more options
        -h full -- print all options (including all format and codec specific options, very long)
        -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter
        See man ffmpeg for detailed description of the options.
    
    Print help / information / capabilities:
    -L                  show license
    -h topic            show help
    -? topic            show help
    -help topic         show help
    --help topic        show help
    -version            show version
    -buildconf          show build configuration
    -formats            show available formats
    -muxers             show available muxers
    -demuxers           show available demuxers
    -devices            show available devices
    -codecs             show available codecs
    -decoders           show available decoders
    -encoders           show available encoders
    -bsfs               show available bit stream filters
    -protocols          show available protocols
    -filters            show available filters
    -pix_fmts           show available pixel formats
    -layouts            show standard channel layouts
    -sample_fmts        show available audio sample formats
    -colors             show available color names
    -opencl_bench       run benchmark on all OpenCL devices and show results
    -sources device     list sources of the input device
    -sinks device       list sinks of the output device
    -hwaccels           show available HW acceleration methods
    
    Global options (affect whole program instead of just one file:
    -loglevel loglevel  set logging level
    -v loglevel         set logging level
    -report             generate a report
    -max_alloc bytes    set maximum size of a single allocated block
    -opencl_options     set OpenCL environment options
    -y                  overwrite output files
    -n                  never overwrite output files
    -ignore_unknown     Ignore unknown stream types
    -filter_threads     number of non-complex filter threads
    -filter_complex_threads  number of threads for -filter_complex
    -stats              print progress report during encoding
    -max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error  maximum error rate
    -bits_per_raw_sample number  set the number of bits per raw sample
    -vol volume         change audio volume (256=normal)
    
    Per-file main options:
    -f fmt              force format
    -c codec            codec name
    -codec codec        codec name
    -pre preset         preset name
    -map_metadata outfile[,metadata]:infile[,metadata]  set metadata information of outfile from infile
    -t duration         record or transcode "duration" seconds of audio/video
    -to time_stop       record or transcode stop time
    -fs limit_size      set the limit file size in bytes
    -ss time_off        set the start time offset
    -sseof time_off     set the start time offset relative to EOF
    -seek_timestamp     enable/disable seeking by timestamp with -ss
    -timestamp time     set the recording timestamp ('now' to set the current time)
    -metadata string=string  add metadata
    -program title=string:st=number...  add program with specified streams
    -target type        specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
    -apad               audio pad
    -frames number      set the number of frames to output
    -filter filter_graph  set stream filtergraph
    -filter_script filename  read stream filtergraph description from a file
    -reinit_filter      reinit filtergraph on input parameter changes
    -discard            discard
    -disposition        disposition
    
    Video options:
    -vframes number     set the number of video frames to output
    -r rate             set frame rate (Hz value, fraction or abbreviation)
    -s size             set frame size (WxH or abbreviation)
    -aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
    -bits_per_raw_sample number  set the number of bits per raw sample
    -vn                 disable video
    -vcodec codec       force video codec ('copy' to copy stream)
    -timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
    -pass n             select the pass number (1 to 3)
    -vf filter_graph    set video filters
    -ab bitrate         audio bitrate (please use -b:a)
    -b bitrate          video bitrate (please use -b:v)
    -dn                 disable data
    
    Audio options:
    -aframes number     set the number of audio frames to output
    -aq quality         set audio quality (codec-specific)
    -ar rate            set audio sampling rate (in Hz)
    -ac channels        set number of audio channels
    -an                 disable audio
    -acodec codec       force audio codec ('copy' to copy stream)
    -vol volume         change audio volume (256=normal)
    -af filter_graph    set audio filters
    
    Subtitle options:
    -s size             set frame size (WxH or abbreviation)
    -sn                 disable subtitle
    -scodec codec       force subtitle codec ('copy' to copy stream)
    -stag fourcc/tag    force subtitle tag/fourcc
    -fix_sub_duration   fix subtitles duration
    -canvas_size size   set canvas size (WxH or abbreviation)
    -spre preset        set the subtitle options to the indicated preset
    

    上面输出了一堆东东,到底是什么鬼啊?现在我们不需要关注这些选项到底是什么,我们试试最简单的用法。

    FFmpeg 可以处理本地的音视频,也可以处理网络上的音视频流。将一个网络视频下载到本地:

    ffmpeg -i https://www.zengzhihao.pw/videos/girl/index.m3u8 index.m3u8
    

    上面下载的视频是一个韩国小姐姐的直播视频~

    然后看到一堆哗啦啦的输出之后,线上小姐姐的视频已经下载到本地了。

    通过文本编辑器打开 index.m3u8,可以发现里面内容是这样的:

    #EXTM3U
    #EXT-X-VERSION:3
    #EXT-X-TARGETDURATION:11
    #EXT-X-MEDIA-SEQUENCE:1
    #EXTINF:10.427778,
    index1.ts
    #EXTINF:10.427778,
    index2.ts
    #EXTINF:10.427778,
    index3.ts
    #EXTINF:10.427778,
    index4.ts
    #EXTINF:7.925111,
    index5.ts
    #EXT-X-ENDLIST
    

    是不是感觉漏了点什么?

    没错,index0.ts 这个 ts 文件并没有被包含进 index.m3u8 里面。这是因为 FFmpeg 处理 m3u8 文件时, 默认只会包含最后的五个 ts 文件。可以通过加上 -hls_list_size 0 来改变这个默认行为。

    我们来试试下面这个命令:

    ffmpeg -i https://www.zengzhihao.pw/videos/girl/index.m3u8 -hls_list_size 0 -y index.m3u8
    

    现在再来看看 index.m3u8 文件里面的内容:

    #EXTM3U
    #EXT-X-VERSION:3
    #EXT-X-TARGETDURATION:11
    #EXT-X-MEDIA-SEQUENCE:0
    #EXTINF:10.427778,
    index0.ts
    #EXTINF:10.427778,
    index1.ts
    #EXTINF:10.427778,
    index2.ts
    #EXTINF:10.427778,
    index3.ts
    #EXTINF:10.427778,
    index4.ts
    #EXTINF:7.925111,
    index5.ts
    #EXT-X-ENDLIST
    

    OK,成功了!

    先来解释一下这个命令的含义:

    ffmpeg -i https://www.zengzhihao.pw/videos/girl/index.m3u8 -hls_list_size 0 -y index.m3u8
    
    • -i 表示输入文件,可以是本地音视频文件,也可以是网络上的音视频流。
    • -y 表示覆盖本地存在的同名文件。

    我们可以通过 ffmpeg -i index.m3u8 来查看这个视频文件的具体信息。

    [hls,applehttp @ 0x7f8359003400] Opening 'index0.ts' for reading
    Input #0, hls,applehttp, from 'index.m3u8':
      Duration: 00:01:00.06, start: 1.460200, bitrate: 0 kb/s
      Program 0
        Metadata:
          variant_bitrate : 0
        Stream #0:0: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 800x600 [SAR 1:1 DAR 4:3], 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc
        Metadata:
          variant_bitrate : 0
        Stream #0:1: Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp
        Metadata:
          variant_bitrate : 0
    

    其中 Duration: 00:01:00.06 表示视频时长,Video: h264 表示视频为 h264 编码,Audio: aac 表示音频为 aac 编码。



温馨提醒

为了获得更好的阅读体验,您应该使用 Chrome 浏览器访问本站!


联系站长

Email: hi@zengzhihao.pw



VULTR