FFmpeg介绍及常见用法

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

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.2 包含的库

当前 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 常见用法

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.

 

你可能感兴趣的:(常用软件,视音频)