本文主要介绍 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.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:

 _______              ______________            ________
|       |            |              |          |        |
| 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.

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秒),因此,通常对于直播流的截图操作所需时间大于对于本地文件的截图操作。

FFmpeg简介及常见用法相关推荐

  1. 【Linux】关于ffmpeg的一些常见用法

    一.FFmpeg简介 FFmpeg是一款非常快速的视频和音频转换器, 是开源项目 FFmpeg (Fast Forward moving pictures expert group) 的命令行程序. ...

  2. 【c++】标准模板库STL入门简介与常见用法

    一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...

  3. MySQL导入导出 —— mysqldump 简介及常见用法

    一. 导出 导出用户需要有导出对象的权限,例如导出表要有select权限.导出视图要有show view权限.导出触发器要有trigger权限.需要锁表时要有lock tables权限等. 如果dum ...

  4. FFmpeg简介(一)

    FFmpeg简介 ffmpeg是一个非常快的视频/音频转换器,其也可以现场抓取音频/视频源,并在任意采样率.尺寸之间调整视频,以及提供多种高品质的滤镜系统. FFmpeg涉及的基本概念 分辨率: 分辨 ...

  5. linux服务器中学习使用FFmpeg命令overlay滤镜用法

    linux服务器中学习使用FFmpeg命令overlay滤镜用法 1.overlay技术简介 2.命令行用法 2.1 视频中叠加图标 2.1.1 直接叠加图标 2.1.2 延时叠加图标 2.2 视频中 ...

  6. upperbound找不到_关于lower_bound( )和upper_bound( )的常见用法

    头文件:#include lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begi ...

  7. visibility的常见用法(小白专用)

    1. visibility(可见性) visibility可见性,用于显示隐藏元素,但是保留原来的位置. inherit 继承 visible 可见的(默认的) hidden 隐藏元素后,占用原来的位 ...

  8. dnf包管理器常见用法

    dnf包管理器常见用法 DNF包管理器简介 从指定软件仓安装指定软件 更新软件包到最新的稳定发行版 dnf distro-sync 回滚某个特定软件的版本dnf downgrade DNF包管理器简介 ...

  9. stringstream的常见用法

    stringstream的常见用法 前言 今天笔试实习,做到一道字符串的题,感觉自己对字符串掌握的不够,今天记录记录stringstream的学习 一.简介 头文件是 #include<sstr ...

  10. 1 FFmpeg从入门到精通-FFmpeg简介

    1 FFmpeg从入门到精通-FFmpeg简介 2 FFmpeg从入门到精通-FFmpeg工具使用基础 3 FFmpeg从入门到精通-FFmpeg转封装 4 FFmpeg从入门到精通-FFmpeg转码 ...

最新文章

  1. 56岁潘石屹下定决心学Python,60多岁程序语言之父们还在敲代码,你还敢懈怠吗?...
  2. 6个变态的C语言Hello World程序
  3. html5在线考试开发,基于HTML5的无纸化在线考试系统.docx
  4. 【洛谷 P2051】 [AHOI2009]中国象棋(DP)
  5. linux修改ip广播地址,Linux设置查看静态IP之ifconfig命令
  6. jquery插件开发通用框架
  7. 如何隐藏win32 console application的console窗口
  8. TypeScript接口
  9. springboot开始
  10. crypto在web的使用
  11. git学习笔记(三)
  12. 关于启动Activity之间的及普通按钮的点击事件
  13. 汇编语言王爽实验十三
  14. 快乐之道:游戏设计的黄金法则
  15. android绘制矢量图地图,Android 高级 UI 进阶之路 (七) SVG 基础使用 + 绘制中国地图...
  16. DX11版引擎即将发布 陈飞舟谈背后故事
  17. 焦点关注|创造中国奇迹:北京大兴国际机场的助力者
  18. Proxmox VE 配置桌面虚拟化
  19. Ubuntu、ROS、PX4常见问题及其解决办法
  20. 定义一个复数类Complex,使得下面的代码能够工作:

热门文章

  1. 别忽视分布式系统这六大“暗流”
  2. apolloxlua include函数
  3. Python 中缀表达式转换后缀表达式
  4. 阿里云推出企业级智能协同办公方案 云桌面、云AP、云客服一应俱全
  5. 图书馆管理系统(C语言实现)
  6. 2017《JAVA》预备作业 计科1501班 王奕开
  7. Mac Pro 安装 Sublime Text 3,个性化设置,主题 和 插件 收藏
  8. 二 Linux 简单配置
  9. multipathd dead but pid file exists
  10. (十七)用JAVA编写MP3解码器——解码Layer1