背景
ffmpeg bin工具 可能无法满足产品的使用,于是需要通过传参调用ffmpeg库,即在通过更底层的方式使用它。
FFmpeg 介绍
FFmpeg是领先的多媒体框架,能够解码,编码,转码,复用,解复用,流,过滤和播放人类和机器创造的任何东西。它支持最多种类的编码格式。无论他们是由某个标准委员会,社区或公司设计的。
它也非常便于携带:FFmpeg在各种构建环境,机器体系结构和配置下编译,运行并通过Linux,Mac OS X,Microsoft Windows,BSD,Solaris等测试基础架构FATE。
它包含:
1.库:可供应用程序使用的libavcodec,libavutil,libavformat,libavfilter,libavdevice,libswscale和libswresample支持类库。
2.工具:ffmpeg,ffplay和ffprobe应用程序可供最终用户用于转码和播放。
FFmpeg Tools
ffmpeg用于在不同格式之间转换多媒体文件的命令行工具;
ffplay 基于SDL和FFmpeg库的简单媒体播放器;
ffprobe个简单的多媒体流分析器;
FFmpeg Libraries for developers:
libavutil是一个包含简化编程功能的库,包括随机数生成器,数据结构,数学例程,核心多媒体实用程序等等。
The libavutil library is a utility library to aid portable multimedia programming. It contains safe portable string functions, random number generators, data structures, additional mathematics functions, cryptography and multimedia related functionality (like enumerations for pixel and sample formats). It is not a library for code needed by both libavcodec and libavformat.
The goals for this library is to be:
Modular
It should have few interdependencies and the possibility of disabling individual parts during./configure
.Small
Both sources and objects should be small.Efficient
It should have low CPU and memory usage.Useful
It should avoid useless features that almost no one needs.
libswscale是一个执行高度优化的图像缩放和色彩空间/像素格式转换操作的库。
The libswscale library performs highly optimized image scaling and colorspace and pixel format conversion operations.
Specifically, this library performs the following conversions:
Rescaling : is the process of changing the video size. Several rescaling options and algorithms are available. This is usually a lossy process.
Pixel format conversion : is the process of converting the image format and colorspace of the image, for example from planar YUV420P to RGB24 packed. It also handles packing conversion, that is converts from packed layout (all pixels belonging to distinct planes interleaved in the same buffer), to planar layout (all samples belonging to the same plane stored in a dedicated buffer or "plane").
This is usually a lossy process in case the source and destination colorspaces differ.
libswresample是一个库,用于执行高度优化的音频重采样,重新矩阵化和采样格式转换操作。
The libswresample library performs highly optimized audio resampling, rematrixing and sample format conversion operations.
Specifically, this library performs the following conversions:
- Resampling: is the process of changing the audio rate, for example from a high sample rate of 44100Hz to 8000Hz. Audio conversion from high to low sample rate is a lossy process. Several resampling options and algorithms are available.
- Format conversion: is the process of converting the type of samples, for example from 16-bit signed samples to unsigned 8-bit or float samples. It also handles packing conversion, when passing from packed layout (all samples belonging to distinct channels interleaved in the same buffer), to planar layout (all samples belonging to the same channel stored in a dedicated buffer or "plane").
Rematrixing: is the process of changing the channel layout, for example from stereo to mono. When the input channels cannot be mapped to the output streams, the process is lossy, since it involves different gain factors and mixing.
Various other audio conversions (e.g. stretching and padding) are enabled through dedicated options.
libavcodec是一个包含音频/视频编解码器解码器和编码器的库。
The libavcodec library provides a generic encoding/decoding framework and contains multiple decoders and encoders for audio, video and subtitle streams, and several bitstream filters.
The shared architecture provides various services ranging from bit stream I/O to DSP optimizations, and makes it suitable for implementing robust and fast codecs as well as for experimentation.
libavformatis a library containing demuxers and muxers for multimedia container formats.
he libavformat library provides a generic framework for multiplexing and demultiplexing (muxing and demuxing) audio, video and subtitle streams. It encompasses multiple muxers and demuxers for multimedia container formats.
It also supports several input and output protocols to access a media resource.
libavdevice是一个包含输入和输出设备的库,用于抓取并渲染许多常见的多媒体输入/输出软件框架,包括Video4Linux,Video4Linux2,VfW和ALSA。
The libavdevice library provides a generic framework for grabbing from and rendering to many common multimedia input/output devices, and supports several input and output devices, including Video4Linux2, VfW, DShow, and ALSA.
libavfilter是一个包含媒体过滤器的库。
The libavfilter library provides a generic audio/video filtering framework containing several filters, sources and sinks.
FFmpeg中的数据结构
-
AVFormatContext 封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装 格式相关信息。
- iformat:输入视频的AVInputFormat
- nb_streams :输入视频的AVStream 个数
- streams :输入视频的AVStream []数组
- duration :输入视频的时长(以微秒为单位)
- bit_rate :输入视频的码率
-
AVInputFormat 每种封装格式(例如FLV, MKV, MP4, AVI)对应一个该结构体。
- name:封装格式名称
- long_name:封装格式的长名称
- extensions:封装格式的扩展名
- id:封装格式ID
- 一些封装格式处理的接口函数
-
AVStream 视频文件中每个视频(音频)流对应一个该结构体。
- id:序号
- codec:该流对应的AVCodecContext
- time_base:该流的时基
- r_frame_rate:该流的帧率
-
AVCodecContext编码器上下文结构体,保存了视频(音频)编解码相关信息。
- codec:编解码器的AVCodec
- width, height:图像的宽高(只针对视频)
- pix_fmt:像素格式(只针对视频)
- sample_rate:采样率(只针对音频)
- channels:声道数(只针对音频)
- sample_fmt:采样格式(只针对音频)
-
AVCodec 每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。
- name:编解码器名称
- long_name:编解码器长名称
- type:编解码器类型
- id:编解码器ID
- 一些编解码的接口函数
-
AVPacket 存储一帧压缩编码数据。
- pts:显示时间戳
- dts :解码时间戳
- data :压缩编码数据
- size :压缩编码数据大小
- stream_index :所属的AVStream
-
AVFrame存储一帧解码后像素(采样)数据。
- data:解码后的图像像素数据(音频采样数据)。
- linesize:对视频来说是图像中一行像素的大小;对音频来说是音频帧的大小。
- width, height:图像的宽高(只针对视频)。
- key_frame:是否为关键帧(只针对视频) 。
- pict_type:帧类型(只针对视频) 。例如I,P,B。
参考资料:https://blog.csdn.net/leixiaohua1020/article/details/11693997