赞
踩
本文主要介绍 FFmpeg(Fast Forward MPEG)的相关知识及其常见用法。
说明:本文也会介绍 FFmpeg 包含的其他工具,如 ffprobe。
引用官网的介绍:
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 包含以下几种库:
本章介绍在 Linux 平台下的 ffmpeg 命令的常见用法。
说明:
示例命令如下:
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.
说明:当前ffmpeg版本支持的“codec”的值,可通过下列命令查询:
ffmpeg -codecs
本示例用到了 -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.
示例命令如下:
ffmpeg -i video_AFP.mp4 -c:v copy -bsf:v h264_mp4toannexb -an video_AFP.h264
上面的命令从本地视频文件 video_AFP.mp4 中提取H264数据,并导出至转换文件 video_AFP.h264 中。
下面详细介绍上面的命令中涉及的选项。
-c[:stream_specifier] codec (input/output,per-stream): 等同于前文介绍的选项“-codec[:stream_specifier] codec (input/output,per-stream)”。
-bsf[:stream_specifier] bitstream_filters (output,per-stream): Set bitstream filters for matching streams. bitstream_filters is a comma-separated list of bitstream filters. Use the "-bsfs" option to get the list of bitstream filters.
-an (input/output):
示例命令如下:
ffmpeg -i rtsp://10.11.111.161:8554/live -codec copy -y capture.mp4
上面的命令捕获 rtsp 协议直播流的内容,并将捕获到的内容存储到本地文件“capture.mp4”中。如果想要将捕获到的内容进行转发,则使用直播流URL替换本地文件名称即可。
-y (global): Overwrite output files without asking.
示例命令如下:
ffmpeg -i rtsp://10.11.111.162:8554/live -vcodec copy -an -y capture_without_audio.mp4
上面的命令捕获 rtsp 协议直播流的内容,然后去除捕获内容中的音频流数据,只将捕获内容中的视频流内容存储到本地文件“capture_without_audio.mp4”中。
-an (input/output):
示例命令如下:
ffmpeg -i "rtsp://192.168.237.162:8554/live" -frames:v 1 -y snapshot.png
上面的命令用于截取接收到的 rtsp 协议直播流的第一帧内容,并将截取的图片内容保存至 snapshot.png 图片文件。其中,“-frames:v 1”指定了待截取的帧数总数为1。
-frames[:stream_specifier] framecount (output,per-stream)
说明:使用 ffmpeg 命令进行截图操作时,输入的 url 可以是本地文件,也可以是直播流内容,不过如果视频类型是直播流,那么由于 ffmpeg 获取直播流信息需要花费时间(根据直播流协议等信息的不同,所需花费的时间不同,一般约为1-4秒),因此,通常对于直播流的截图操作所需时间大于对于本地文件的截图操作。
示例命令如下:
ffmpeg -ss 00:11:00 -to 00:24:25 -i livestream.mp4 -f mp4 -codec copy -q:v 1 output.mp4
上面的命令用于截取视频文件中的指定时间间隔内容,得到的视频文件为 output.mp4。下面对上述命令中的选项进行说明。
-ss position (input/output):
position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.
-to position (input/output):
-to and -t are mutually exclusive and -t has priority.
-q:v,等同于下列选项:
-q[:stream_specifier] q (output,per-stream)
-qscale[:stream_specifier] q (output,per-stream)
“-q:v”选项可以简单理解为设置输出视频的质量,其中 q 取值范围是[1, 35],取值为 1 的时候,对应着最佳的视频质量。
示例命令如下:
ffprobe rtsp://192.168.237.161:8554/live
上面命名的执行结果如下:
通过上面的图片内容可知,ffprobe 可以查看视频直播流的编码格式、分辨了、帧率、扫描方式等信息。
将上述示例中的直播流URL改为视频文件名,如 xxx.mp4,即可查看对应视频文件的相关信息了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。