当前位置:   article > 正文

FFmpeg简介及常见用法_ffmpeg -pix_fmt

ffmpeg -pix_fmt

本文主要介绍 FFmpeg(Fast Forward MPEG)的相关知识及其常见用法。

说明:本文也会介绍 FFmpeg 包含的其他工具,如 ffprobe。

1 概述

引用官网的介绍:

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

2 包含的工具和库

2.1 包含的工具

当前 FFmpeg 包含以下几种工具:

  • ffmpeg: a command line tool to convert multimedia files between formats.
  • ffplay: a simple media player based on SDL and the FFmpeg libraries.
  • ffprobe: a simple multimedia stream analyzer.

2.1 包含的库

当前 FFmpeg 包含以下几种库:

  • libavutil: libavutil is a library containing functions for simplifying programming, including random number generators, data structures, mathematics routines, core multimedia utilities, and much more.
  • libavcodec: libavcodec is a library containing decoders and encoders for audio/video codecs.
  • libavformat: libavformat is a library containing demuxers and muxers for multimedia container formats.
  • libavdevice: libavdevice is a library containing input and output devices for grabbing from and rendering to many common multimedia input/output software frameworks, including Video4Linux, Video4Linux2, VfW, and ALSA.
  • libavfilter: libavfilter is a library containing media filters.
  • libswscale: libswscale is a library performing highly optimized image scaling and color space/pixel format conversion operations.
  • libswresample: libswresample is a library performing highly optimized audio resampling, rematrixing and sample format conversion operations.

3 Linux平台下的常见用法

本章介绍在 Linux 平台下的 ffmpeg 命令的常见用法。

说明:

  • 对于重复出现的 ffmpeg 命令选项,只在该选项首次出现时进行介绍;
  • ffmpeg 的很多选项会区分“输入选项(input option)”和“输出选项(output option)”场景,即同一个命令选项,在作为输入选项和输出选项时,其作用有时是不同的。ffmpeg 规定,当选项位于“-i”之前,则其用为输入选项;当选项位于“输出url”之前,则其用为输出选项。

3.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.

说明:当前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:

  1. _______ ______________ ________
  2. | | | | | |
  3. | input | demuxer | encoded data | muxer | output |
  4. | file | ---------> | packets | -------> | file |
  5. |_______| |______________| |________|

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.

3.2 从视频文件中提取H264数据

示例命令如下:

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)

  • As an input option, blocks all audio streams of a file from being filtered or being automatically selected or mapped for any output. See "-discard" option to disable streams individually.
  • As an output option, disables audio recording i.e. automatic selection or mapping of any audio stream. For full manual control see the "-map" option.

3.3 捕获直播流内容并存储在本地(或转发)

示例命令如下:

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.

3.4 禁用音频

示例命令如下:

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): 

  • As an input option, blocks all audio streams of a file from being filtered or being automatically selected or mapped for any output. See "-discard" option to disable streams individually.
  • As an output option, disables audio recording i.e. automatic selection or mapping of any audio stream. For full manual control see the "-map" option.

3.5 截图

示例命令如下:

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)

  • Stop writing to the stream after framecount frames.

说明:使用 ffmpeg 命令进行截图操作时,输入的 url 可以是本地文件,也可以是直播流内容,不过如果视频类型是直播流,那么由于 ffmpeg 获取直播流信息需要花费时间(根据直播流协议等信息的不同,所需花费的时间不同,一般约为1-4秒),因此,通常对于直播流的截图操作所需时间大于对于本地文件的截图操作。

3.6 截取视频片段

示例命令如下:

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): 

  • When used as an input option (before "-i"), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position.  When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.
  • When used as an output option (before an output url), decodes but discards input until the timestamps reach position.

position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

-to position (input/output):

  • Stop writing the output or reading the input at position.  position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

-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)

  • Use fixed quality scale (VBR). The meaning of q/qscale is codec-dependent.  If qscale is used without a stream_specifier then it applies only to the video stream, this is to maintain compatibility with previous behavior and as specifying the same codecspecific value to 2 different codecs that is audio and video generally is not what is intended when no stream_specifier is used.

“-q:v”选项可以简单理解为设置输出视频的质量,其中 q 取值范围是[1, 35],取值为 1 的时候,对应着最佳的视频质量。

3.7 查看视频信息

示例命令如下:

ffprobe rtsp://192.168.237.161:8554/live

上面命名的执行结果如下:

通过上面的图片内容可知,ffprobe 可以查看视频直播流的编码格式、分辨了、帧率、扫描方式等信息。

将上述示例中的直播流URL改为视频文件名,如 xxx.mp4,即可查看对应视频文件的相关信息了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/463237
推荐阅读
相关标签
  

闽ICP备14008679号