热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FFmpeg学习详细笔记(一)

FFmpeg1.FFmpeg简介1.1F

FFmpeg

1.FFmpeg简介

1.1 FFmpeg的定义

FFmpeg既是一款音视频编解码工具,同时也是一组音视频编解码开发套件,作为编解码开发套件,它为开发者提供了丰富的音视频处理的调用接口,提供了多种媒体格式的封装和解封装,包括多种音视频解码、多种协议的流媒体、多种色彩格式转换、多种采样率转换、多种码率转换等;FFmpeg框架提供了多种丰富的插件模块,包含封装与解封装的插件、编码与解码的插件等。

FF:Fast Forward,Fast Free,Fast Fourier。

mpeg:Moving Picture Experts Group(动态图像专家组)

1.2 FFmpeg历史

FFmpeg是法国天才程序员Fabrice Bellard在2000年时开发出初版,后来由Michael Niedermayer维护。

FFmpeg的源码Git库提供了多站同步的获取方式

git://source.ffmpeg.org/ffmpeg.git

http://git.videolan.org/?p=ffmpeg.git

https://github.com/FFmpeg/FFmpeg

1.3 FFmpeg的基本组成

FFmpeg框架基本组成包含:AVFormat、AVCodec、AVFilter、AVDevice、AVUtil等模块库。

  1. FFmpeg的封装模块AVFormat,实现了多媒体领域的绝大多数媒体封装格式,包括封装和解封装。如MP4,FLV,KV,TS等文件封装格式,RTMP,RTSP,MMS,HLS等网络协议封装格式。
  2. FFmpeg的编解码模块AVCodec,实现了目前多媒体领域绝大多数常用的编解码格式,及支持编码,也支持解码。支持MPEG4、AAC、MJPEG自带的媒体编解码格式之外,还支持第三方的编解码器,如H.264(AVC)编码,需x264编码器,H.265(HEVC)编码,需要使用x265编码器;MP3(mp3lame)编码,需要libmp3lame编码器。
  3. FFmpeg的滤镜模块AVFilter,提供了一个通用的音频,视频,字幕等滤镜处理框架。可以有多个输入和输出。
    参考:

[main]
[tmp]
[flip]
输入
split
overlay
crop
vflip
输出

滤镜处理将输入的视频切割成了两部分流,一部分流抛给crop滤镜与vflip滤镜处理模块进行操作,另一部分保持原样,当crop滤镜与vflip滤镜处理操作完成之后,将流合并到原有的overlay图层中,并显示在最上面一层,输出新的视频。对应的命令行如下。

./ffmpeg -i INPUT -vf "split [main][tmp]; [tmp] crop=iw:ih/2:0:0, vflip [flip]; [main][flip] overlay=0:H/2" OUTPUT

相同的Filter线性链之间用逗号分割
不同的Filter线性链之间用分号分割
4. FFmpeg的视频图像转换计算模块swscale,提供了高级别的图像转换API,例如它允许进行图像缩放和像素格式转换,常见于将图像从1080p转换成720p或者480p等的缩放,或者将图像数据从YUV420P转换成YUYV,或者YUV转RGB等图像格式转换。
5. FFmpeg的音频转换计算模块swresample,swresample模块提供了高级别的音频重采用API。它允许音频采样,音频通道布局转换和布局调整。

1.4 FFmpeg的编解码工具ffmpeg

ffmpeg是FFmpeg源代码编译后生成的一个可执行程序,其可以作为命令行工具使用

./ffmpeg -i input.mp4 output.avi

这是一条简单的ffmpeg命令,可以看到,ffmpeg通过-i参数将input.mp4作为输入源输入,然后进行转码与封装操作,输出到output.avi中,这条命令主要做了如下工作。

1.获得输入源input.mp4;
2.转码;
3.输出文件output.avi.

看似简单的两步主要工作,其实远远不止是从后缀名为MP4的文件输出成后缀名为AVI的文件,因为在ffmpeg中,MP4与AVI是两种文件封装格式,并不是后缀名就可以决定的。

换一种方式也可以做到上面的功能:

./ffmpeg -i input.mp4 -f avi output.dat

其中“-f”这个参数的工作非常重要,它指定了输出文件的容器格式,尽管输出文件是output.dat,后缀是.dat,但是它格式依然是avi。

ffmpeg主要工作流程是:
解封装(Demuxing)-》解码(Decoding)-》编码(Encoding)-》封装(Muxing)

经历6个步骤:
读取输入源-》进行音视频的解封装-》解码每一帧音视频数据-》编码每一帧音视频数据-》进行音视频的重新封装-》输出到目标

读取文件
解封装libavformat
解码libavcodec
转换参数
新编码libavcodec
封装libavformat
写入文件

1.5 FFmpeg的播放器ffplay

ffplay是FFmpeg源代码编译后生成的另一个可执行程序,与ffmpeg在FFmpeg项目中充当的角色基本相同,可以作为测试工具进行使用,ffplay提供了音视频显示和播放相关的图像信息、音频的波形信息等。
注意:如果要使用ffplay,系统需要有SDL来进行ffplay的基础支撑,ffplay旧版依赖于SDL-1.2,而新版本依赖于SDL-2.0,需要安装对应的SDL才能生成ffplay。

1.6 FFmpeg的多媒体分析器ffprobe

ffprobe是一个非常强大的多媒体分析工具,从媒体文件或媒体流中获得你想要了解的媒体信息,如音频的参数,视频的参数,媒体容器的参数信息等。例如分析某个媒体容器中的音频是什么编码格式、视频的编码格式,媒体的总时长,复合码率等信息。

./ffprobe -show_streams output.mp4
[STREAM]
index=0
codec_name=mpeg4
codec_long_name=MPEG-4 part 2
profile=Simple Profile
....
....
[/STREAM]

流相关信息是通过[STREAM][/STREAM]的方式展现出来的,当视频文件容器中包含音频流与视频流或更多路流时,会通过[STREAM][/STREAM]进行多个流的分割,分割后采用index来进行流的索引信息的分区。

1.7 FFmpeg编译

1.7.1 FFmpeg之Windows平台编译

FFmpeg在Windows平台中的编译需要使用MinGW-w64,MinGW是Minimalist GNU for Windows的缩写。详细介绍和安装方法参考http://www.mingw.org/。MinGW-64单独使用比较麻烦,可与MSYS环境配合使用,Minimal SYStem的缩写,主要完成的工作为UNIX on Windows的功能,是一个仿生UNIX环境的Windows工具集,它的详细介绍和使用方法可以参照http://www.mingw.org/wiki/MSYS

MinGW-64+MSYS环境准备好后,就正式进入编译环节了。

打开安装好的MSYS2 MinGW x64程序

  1. 进入FFmpeg源码目录,执行./configure;
  2. configure成功后执行make,在MinGW环境下编译ffmpeg是一个比较漫长的过程
  3. 执行make install,到此为止,FFmpeg在Windows上的编译已全部完成,此时我们可以尝试使用FFmpeg命令行来验证编译结果。执行"./ffmpeg.exe -h"

liuyu@LAPTOP-3K0R1C9D MINGW64 /e/Programing/ffmpeg
$ ./configure
nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
//上面提示缺少yasm汇编器,可以不安装yasm汇编器。没有则报错。
liuyu@LAPTOP-3K0R1C9D MINGW64 /e/Programing/ffmpeg
$ ./configure --disable-x86asm
$ ./ffmpeg.exe -h
ffmpeg version 4.4.git Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11.2.0 (Rev5, Built by MSYS2 project)
configuration: --disable-x86asm
libavutil 57. 7.100 / 57. 7.100
libavcodec 59. 9.101 / 59. 9.101
libavformat 59. 5.100 / 59. 5.100
libavdevice 59. 0.101 / 59. 0.101
libavfilter 8. 9.100 / 8. 9.100
libswscale 6. 1.100 / 6. 1.100
libswresample 4. 0.100 / 4. 0.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/bsf/protocol
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
-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
-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 maximum error rate ratio of decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-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)
-fpsmax rate set max 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

最近下载github上FFmpeg源码速度非常慢,经常失败,于是改去gitee上下载镜像包:https://gitee.com/mirrors/ffmpeg

1.8 FFmpeg编码支持与定制

FFmpeg本身支持一些音视频编码格式、文件封装格式与流媒体传输协议,但是支持的数量依然有限,FFmpeg所做的值是提供基础的框架,所有的编码格式、文件封装格式与流媒体协议均可以作为FFmpeg的一个模块挂载到FFmpeg框架中。可以通过FFmpeg源码的configure命令查看FFmpeg所支持的音视频编码格式、文件封装格式与流媒体传输协议。

1.8.1 FFmpeg编码器支持

通过使用编译配置命令:./configure --list-encoders参数查看

1.8.2 FFmpeg解码器支持

解码主要是在输入的时候进行解码,也可以理解为将压缩过的编码进行解压缩,关于解码的支持,可以通过./configure --list-decoders命令进行查看,支持MPEG4/H.264/H.265(HEVC)/MP3格式

1.8.3 FFmpeg的封装支持

FFmpeg的封装(Muxing)是指将压缩后的编码封装到一个容易格式中,如果要查看FFmpeg源代码中都可以支持哪些容器格式,可以通过命令./configure --list-muxers来查看

1.8.4 FFmpeg的解封装支持

FFmpeg的解封装(Demuxing)是指将读入的容器格式拆解开,讲礼貌压缩的音频流,字幕流,数据流等提取出来,查看源代码都可以支持哪些输入的容器格式:./configure --list-demuxers来查看

1.8.5 FFmpeg的通信协议支持

FFmpeg不仅仅支持本地的多媒体处理,而且还支持网络流媒体的处理,支持网络流媒体协议相对来说也很全面,可以通过命令./configure --list-protocols查看。


推荐阅读
author-avatar
zero__
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有