本文主要介绍 FFmpeg(Fast Forward MPEG) 的相关知识及其常见用法。
引用官网的介绍:
FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.
引用官方的简介:
A complete, cross-platform solution to record, convert and stream audio and video.
简单说,FFmpeg 提供了一个跨平台的解决方案,其包含了视音频的记录(采集)、编解码、格式转换和流化功能。
使用 FFmpeg 进行视音频的转换非常容易,例如使用如下命令将视频由mp4格式转换为avi格式:
ffmpeg -i input.mp4 output.avi
当前 FFmpeg 包含以下几种工具:
当前 FFmpeg 包含以下几种库:
1. 将本地视频文件转为直播流。示例命令如下:
ffmpeg -re -i feast-2880x1440.mp4 -codec copy -f flv rtmp://192.168.110.168:1935/live/zb
上述命令将本地视频文件 feast-2880x1440.mp4 转换为直播流 rtmp://192.168.110.168:1935/live/zb,对于该命令的选项解释如下。
-re (input):Read input at native frame rate. Mainly used to simulate a grab device, or live input stream (e.g. when reading from a file). Should not be used with actual grab devices or live input streams (where it can cause packet loss). By default ffmpeg attempts to read the input(s) as fast as possible. This option will slow down the reading of the input(s) to the native frame rate of the input(s). It is useful for real-time output (e.g. live streaming).
-i url (input):input file url
-codec[:stream_specifier] codec (input/output,per-stream):Select an encoder (when used before an output file) or a decoder (when used before an input file) for one or more streams. codec is the name of a decoder/encoder or a special value copy (output only) to indicate that the stream is not to be re-encoded.
本示例用到了 -codec copy 的用法,即 stream copy 的概念。关于 stream copy,解释如下:
Stream copy is a mode selected by supplying the copy parameter to the -codec option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:
_______ ______________ ________
| | | | | |
| input | demuxer | encoded data | muxer | output |
| file | ---------> | packets | -------> | file |
|_______| |______________| |________|
Since there is no decoding or encoding, it is very fast and there is no quality loss. However, it might not work in some cases because of many factors. Applying filters is obviously also impossible, since filters work on uncompressed data.
-f fmt (input/output):Force input or output file format. The format is normally auto detected for input files and guessed from the file extension for output files, so this option is not needed in most cases.