├── .gitignore ├── 01-write-in-front.md ├── 02-download-and-install.md ├── 03-execute.md ├── 04-media-file-structure.md ├── 05-start-converting.md ├── 06-do-in-passing-while-converting.md ├── 07-subtitles.md ├── 08-differente-encoders-special-options.md ├── 10-write-in-end.md ├── LICENSE ├── README.md ├── etc ├── feedback │ ├── README.md │ └── repo-panel.png └── fontconfig-windows │ ├── README.md │ ├── advanced-system-props.png │ ├── c-users-username.png │ ├── environ-var.png │ ├── fonts.conf │ ├── new-var.png │ └── system.png ├── image ├── by-sa.png ├── cd-ls.png ├── files-in-explorer.png ├── successful-completed.png ├── video-with-subtitles.png └── windows-ff-prompt.png └── index.md /.gitignore: -------------------------------------------------------------------------------- 1 | .directory 2 | .~* 3 | *~ 4 | -------------------------------------------------------------------------------- /01-write-in-front.md: -------------------------------------------------------------------------------- 1 | # 写在前面 2 | 3 | 4 | ## 丑话说在前头 5 | 6 | 我知道这样可能使你的心情不好,但是为了防止我受到不明不白的诋毁,我还是得写下这一节。 7 | 8 | 如果你心中怀着以下想法之一,并且觉得这样是十分正确的,请点击右上角的 X : 9 | 10 | - 我 XXXX 用的挺好的,管它做了什么事,能用就行。 11 | - 这么多人用的东西竟然没有中文,看来它根本不重视中国市场。 12 | - 好麻烦啊,我只想要现成的。 13 | - 上个世纪八国联军…… 14 | - 你是中国人不?是就支持国产,抵制外国货! 15 | - 怎么这么多链接打不开? 16 | - 啊,f墙?麻不麻烦啊,你直接把内容复制过来呗,我懒。 17 | 18 | 没符合上面中的任何一条?太好了!你应该是个拥有逻辑思维的人,我相信大部分人都是。 19 | 20 | 符合吗?那抱歉了,我建议你现在就点右上角的 X 。当然你要是愿意看下去我也管不着,只是最好别发一些激动的评论,否则我会尽量让你 **圆润离开** 。 21 | 22 | 说完了上面不适合的,我在来说说适合的人群: 23 | 24 | - 想学会怎么转视频格式、编码的人。 25 | - 想在视频网站上做出优秀视频的人。 26 | - 视频资源收集控。 27 | - 想了解关于数字媒体的知识,但以前从未接触过的人。 28 | - 知道国产毒瘤们的恶劣事迹,但苦于找不到替代品的人。 29 | - 不希望自己的东西被别人——或者别人编写的程序——掌控的人。 30 | 31 | 其实不符合也没关系, **只要你有兴趣,我当然欢迎你继续阅读下去!** 32 | 33 | 34 | ## FFmpeg 是什么,我为什么要用它? 35 | 36 | [FFmpeg][] 是一个多媒体编解码器库,并提供了命令行前端。许多软件都使用了 FFmpeg 来进行音频和视频的编解码,特别是像 [MPlayer][] 这样的多媒体播放器。 37 | FFmpeg 还是一个[自由软件][],因为自由,所以其他人可以非常方便的在他们的软件中使用 FFmpeg 的东西。如果没有 FFmpeg ,许多播放器都要自己重新写解码器。有了 FFmpeg ,那些游戏开发者只要在他们的程序中包含 FFmpeg 的视频解码模块就可以播放视频了,而将更多的精力放在游戏本身内容上面。 38 | 39 | 这份自由是来之不易的,然而它也是脆弱的。 FFmpeg 作为一个遵循 [GNU 宽通用公共许可证][](简称 LGPL)的软件,任何人都可以获取它的代码并放入自己的程序中,但是有一个限制,那就是这样的程序必须也采用 LGPL ,也意味着必须开放源代码。 40 | 但是一些软件并没有这么做,这当然是无法肯定的,拿了人家的代码,自然就得听人家的规矩。于是就有一个[耻辱柱][],记下志愿者们发现的不遵守协议的软件。 41 | 42 | 让我们来看一下这个耻辱柱吧。 43 | 里面出现了 Baofeng Storm ,也就是大家所熟知的**暴风影音**。 44 | 还有 QQPlayer ,也就是 **QQ 影音**。 45 | 当然也少不了广为人知的 Format Factory ,也就是**格式工厂**。 46 | 47 | 再看看[这篇文章](https://www.byvoid.com/zhs/blog/qq-player-ffmpeg-gpl),他分析的比我透彻的多。 48 | 看到这些,你还能淡定地用着它们吗? 49 | 播放器的话,有 [SMPlayer][] 和 [VLC][] 这两款开源免费的播放器。哦对了,播放音乐的话,也少不了 [Audacious][] 。 50 | 51 | 但是转码软件就没这么简单了,它的选择似乎要少得多。 52 | 事实上,不论是像格式工厂这样违反 LGPL 的转码软件,还是像 [MeGUI][] 这样遵守 LGPL 的软件,市面上能见到的大多数转码软件都是拿着 FFmpeg 的核心,套了一层自己的皮而已。 53 | 那为何不用最纯正的 FFmpeg 呢! 54 | 55 | 不但有着最大的定制性,还有着最轻量的前端。 56 | [命令行](03-execute.md#this-black-window)的操作界面,也能够写入脚本中,高效的完成批量任务。 57 | 我想你已经迫不及待了,那么就开始阅读[第 2 章](02-download-and-install.md)吧! 58 | 59 | [FFmpeg]: https://zh.wikipedia.org/wiki/FFmpeg 60 | [MPlayer]: https://zh.wikipedia.org/wiki/MPlayer 61 | [自由软件]: https://zh.wikipedia.org/wiki/%E8%87%AA%E7%94%B1%E8%BD%AF%E4%BB%B6 62 | [GNU 宽通用公共许可证]: https://zh.wikipedia.org/wiki/GNU%E5%AE%BD%E9%80%9A%E7%94%A8%E5%85%AC%E5%85%B1%E8%AE%B8%E5%8F%AF%E8%AF%81 63 | [耻辱柱]: https://github.com/FFmpeg/web/blob/master/src/shame 64 | [SMPlayer]: http://smplayer.sourceforge.net/zh/info 65 | [VLC]: http://www.videolan.org/vlc/ 66 | [Audacious]: http://audacious-media-player.org/ 67 | [MeGUI]: http://sourceforge.net/projects/megui/ 68 | -------------------------------------------------------------------------------- /02-download-and-install.md: -------------------------------------------------------------------------------- 1 | # 下载与安装 2 | 3 | 4 | ## GNU/Linux 5 | 6 | 得益于 GNU/Linux 上强大的包管理系统,在 GNU/Linux 中安装 FFmpeg 非常简单。但由于各种发行版采用的包管理器不同,安装时需要执行的命令也不同,以下列举三种最常见的包管理器安装 FFmpeg时所需的命令,全部需要在 root 账户或者 `sudo` 后执行: 7 | 8 | - Debian, Ubuntu, Linux Mint 9 | 10 | apt-get install libav-tools 11 | 12 | **Debian 及其衍生版用户注意:** 因为一些历史原因, Debian 及其衍生版上是没有一个叫做 `ffmpeg` 的软件包的,取而代之的是 `libav-tools` ,需要执行的命令也是 `avconv` 而不是 `ffmpeg` 。以后将不会重复说明,请将后面的 `ffmpeg` 命令自行脑补成 `avconv` 。 13 | 14 | - Fedora, CentOS, RHEL 15 | 16 | yum install ffmpeg 17 | 18 | Fedora 22 及更高版本请用 `dnf` 代替 `yum` 。 19 | 20 | - Arch Linux, Manjaro 21 | 22 | pacman -S ffmpeg 23 | 24 | 实际上,在你看到这篇文章之前,你的电脑中很可能已经安装好了 FFmpeg ,在这种情况下,包管理器会提示已经安装。 25 | 26 | 27 | ## Mac OS X® 28 | 29 | *由于作者缺少对此操作系统的了解,所写的信息可能不准确,本节内容或许需要了解它的人对其进行完善和修改。* 30 | 31 | 在 Mac OS X® 上安装 FFmpeg 有两种方式:第三方包管理器以及手动安装。 32 | 33 | 34 | ### 第三方包管理器 35 | 36 | 如果你的电脑中已经安装了 [Homebrew](http://brew.sh/) ,可以使用以下命令安装: 37 | 38 | brew install ffmpeg 39 | 40 | 如果没有安装 Homebrew ,可以执行以下命令来安装 Homebrew ,再执行上面的命令安装 FFmpeg : 41 | 42 | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 43 | 44 | 45 | ### 手动安装 46 | 47 | 访问 ,你会看到 FFmpeg 标签下有一左一右两个绿色的下载按钮,左边版本更新,右边则更稳定,凭你的喜好,点哪个都可以。 48 | 49 | 然后解压这个压缩文件,你能够看到 ffmpeg 这一个文件,接下来打开终端, `cd` 到文件存在的目录,执行下列命令: 50 | 51 | sudo cp ffmpeg /usr/local/bin/ 52 | sudo chmod 755 /usr/local/bin/ffmpeg 53 | 54 | 此时在终端中执行 `ffmpeg` ,如果出来的内容不是 `bash: ffmpeg: command not found` ,就说明安装成功了! 55 | 56 | 57 | ## Microsoft® Windows® 58 | 59 | 首先你需要知道 [你的电脑是 32 位还是 64 位](https://support.microsoft.com/zh-cn/kb/827218)。 60 | 61 | 然后访问 ,你可以看到许多灰色的按钮。如果你的电脑是 32 位,点最上面一行左边的按钮 "Download FFmpeg git-xxxxxxx 32-bit Static" ,如果是 64 位,点右边的 "Download FFmpeg git-xxxxxxx 64-bit Static" 。 62 | 63 | 解压。 64 | 65 | 双击 `ff-prompt.bat` ,如果出现了如下的界面,并且最后一行可以进行输入,则表示没有问题。 66 | ![ff-prompt](image/windows-ff-prompt.png) 67 | 68 | 它是一个绿色软件,你可以直接在这个窗口中执行接下来会讲到的命令。但为了方便,我推荐你进行“手动安装”。 69 | 双击解压出来的 bin 目录,可以看到 3 个 .exe 文件,将它们选中,复制到 `C:\\Windows\system32\` 中,此操作可能需要管理员权限。 70 | 然后在[命令提示符](https://zh.wikipedia.org/wiki/%E5%91%BD%E4%BB%A4%E6%8F%90%E7%A4%BA%E5%AD%97%E5%85%83)中输入 `ffmpeg` ,如果出来的不是 `"ffmpeg" 不是一个文件或内部命令`,则说明安装成功! 71 | 72 | 73 | ## 手机、平板、树莓派…… 74 | 75 | 虽然理论上 FFmpeg 可以在移动平台运行,但是目前移动端上十分缺乏此类应用。封闭的 iOS 就不提了,在 Android 上有一个[以 FFmpeg 为核心的转码应用](https://play.google.com/store/apps/details?id=com.silentlexx.ffmpeggui),但是功能严重不全,操作方式也大不相同。 76 | 77 | 所以转码工作最好还是在电脑上进行。 78 | 79 | [树莓派](https://zh.wikipedia.org/wiki/%E6%A0%91%E8%8E%93%E6%B4%BE)是一个特殊的存在,它并不是一台如我们想象中一样的 x86 电脑,但是它可以当作一台性能和能耗较低的电脑来使用。它预装的操作系统是 Raspbian , Debian 的一个衍生版,在它上面的安装请参考上面关于 GNU/Linux 的说明。 80 | 81 | ------------------------ 82 | 83 | 安装好了,一切就绪?那么开始[第 3 章](03-execute.md)的阅读吧! 84 | -------------------------------------------------------------------------------- /03-execute.md: -------------------------------------------------------------------------------- 1 | # 运行 2 | 3 | 4 | ## 是这个黑黑的窗口吗? 5 | 6 | 如果你是一名 Windows® 用户,在第一次运行 ff-promt.bat 的时候,也许会被吓一跳,因为此时出现在屏幕前的,是一个黑底白字——就像在黑客电影中所见到的那样的窗口。 7 | ![ff-prompt](image/windows-ff-prompt.png) 8 | 9 | > 什么情况?我的电脑坏了? 10 | 11 | 你此时心中可能会这样想。但是实际上我想告诉你,你的电脑并没有坏,这个窗口是接下来你要经常面对的东西了。 12 | 别害怕,这并不是什么只有在电影中才能看见的高级玩意儿,在十几至二十几年前,大部分的电脑都是这样子。 13 | 14 | > 可是现在都什么时代了,为什么还要使用这种老土的东西? 15 | 16 | 事实上,即使在今天,许多人也在使用它来完成大部分的任务,许多事情还只能靠它来完成。 17 | 图形界面的发明给是计算机界的一大进步,带来了许多好处,但同时的,图形化的东西也有许多缺点。比如,找一个功能需要很多次点击,程序的开发也要消耗许多精力在“如何设置一个好的图形界面”上,图形界面还促使了鼠标手的产生。 18 | 19 | 我们不得不承认,许多任务,用命令行来完成更高效。 FFmpeg 很好的诠释了这一点。 20 | 命令行没什么好怕的,我天天用呢,如果哪天它突然消失了,做事的效率不知道要低多少。 21 | 22 | 也许我在这节内容上耗费太多时间了,本文讲的是 FFmpeg 的用法而不是命令行的优势,再多讲就跑题了。如果你还不服,或者感兴趣,[这篇文章](http://os.51cto.com/art/201007/215287.htm)也许挺适合你。 23 | 24 | 25 | ## 查看帮助 26 | 27 | 首先,打开终端。 28 | 29 | - GNU/Linux 30 | 31 | 我想我不用说了,你们都会。 32 | 33 | - Mac OS X® 34 | 35 | 点 Dock 中的 Dashboard ,找到 Terminal ,点它。 36 | 37 | - Microsoft® Windows® 38 | 39 | 按 Win + R ,输入 `cmd` ,回车。 40 | 41 | 然后,执行以下命令: 42 | 43 | ffmpeg -version 44 | 45 | 你可以看到像这样的一些输出: 46 | 47 | ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers 48 | built with gcc 5.2.0 (GCC) 49 | configuration: --prefix=/usr --disable-debug --disable-static --disable-strippin 50 | g --enable-avisynth --enable-avresample --enable-fontconfig --enable-gnutls --en 51 | able-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype 52 | --enable-libfribidi --enable-libgsm --enable-libmodplug --enable-libmp3lame --e 53 | nable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable 54 | -libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-li 55 | bspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis -- 56 | -libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvi 57 | d --enable-shared --enable-version3 --enable-x11grab 58 | libavutil 54. 31.100 / 54. 31.100 59 | libavcodec 56. 60.100 / 56. 60.100 60 | libavformat 56. 40.101 / 56. 40.101 61 | libavdevice 56. 4.100 / 56. 4.100 62 | libavfilter 5. 40.101 / 5. 40.101 63 | libavresample 2. 1. 0 / 2. 1. 0 64 | libswscale 3. 1.101 / 3. 1.101 65 | libswresample 1. 2.101 / 1. 2.101 66 | libpostproc 53. 3.100 / 53. 3.100 67 | 68 | `version` 的意思就是“版本”,通过这条命令我们就可以看见 FFmpeg 的版本以及其他的一些信息。 69 | 70 | 从第一行能够看出,我在写这篇文章时,所使用的 FFmpeg 版本是 2.8 ,多数情况下你得到的版本都会比我的新,一般情况下新版本都会保持对旧版本的兼容性,所以版本较新通常不会产生问题。 71 | 但如果你使用的版本比我的旧,即使可能大部分操作都不会出现与本教程不同的地方,但最好还是升到最新版本,也就是按照上一章的方式再下载最新版。 72 | 73 | 我们现在知道了 FFmpeg 的版本,接下来需要知道它的使用方法—— FFmpeg 内置了十分详尽的帮助说明,我们可以通过这样一条命令来查看它: 74 | 75 | ffmpeg -help 76 | 77 | 如果你拥有一定的 Unix 使用经验,对在命令后加 `-help` 来获取帮助的方式应该不陌生。 78 | 79 | 现在我们获得了这样的输出: 80 | 有点长,但别惊慌,我们并不需要理解它全部的内容,现在只要瞟一眼就好。 81 | 82 | ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers 83 | built with gcc 5.2.0 (GCC) 84 | configuration: --prefix=/usr --disable-debug --disable-static --disable-stripp 85 | ing --enable-avisynth --enable-avresample --enable-fontconfig --enable-gnutls -- 86 | enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreety 87 | pe --enable-libfribidi --enable-libgsm --enable-libmodplug --enable-libmp3lame - 88 | -enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enab 89 | le-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable- 90 | libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis 91 | --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libx 92 | vid --enable-shared --enable-version3 --enable-x11grab 93 | libavutil 54. 31.100 / 54. 31.100 94 | libavcodec 56. 60.100 / 56. 60.100 95 | libavformat 56. 40.101 / 56. 40.101 96 | libavdevice 56. 4.100 / 56. 4.100 97 | libavfilter 5. 40.101 / 5. 40.101 98 | libavresample 2. 1. 0 / 2. 1. 0 99 | libswscale 3. 1.101 / 3. 1.101 100 | libswresample 1. 2.101 / 1. 2.101 101 | libpostproc 53. 3.100 / 53. 3.100 102 | Hyper fast Audio and Video encoder 103 | usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfi 104 | le}... 105 | 106 | Getting help: 107 | -h -- print basic options 108 | -h long -- print more options 109 | -h full -- print all options (including all format and codec specific option 110 | s, very long) 111 | See man ffmpeg for detailed description of the options. 112 | 113 | Print help / information / capabilities: 114 | -L show license 115 | -h topic show help 116 | -? topic show help 117 | -help topic show help 118 | --help topic show help 119 | -version show version 120 | -buildconf show build configuration 121 | -formats show available formats 122 | -devices show available devices 123 | -codecs show available codecs 124 | -decoders show available decoders 125 | -encoders show available encoders 126 | -bsfs show available bit stream filters 127 | -protocols show available protocols 128 | -filters show available filters 129 | -pix_fmts show available pixel formats 130 | -layouts show standard channel layouts 131 | -sample_fmts show available audio sample formats 132 | -colors show available color names 133 | -sources device list sources of the input device 134 | -sinks device list sinks of the output device 135 | -hwaccels show available HW acceleration methods 136 | 137 | Global options (affect whole program instead of just one file: 138 | -loglevel loglevel set logging level 139 | -v loglevel set logging level 140 | -report generate a report 141 | -max_alloc bytes set maximum size of a single allocated block 142 | -y overwrite output files 143 | -n never overwrite output files 144 | -ignore_unknown Ignore unknown stream types 145 | -stats print progress report during encoding 146 | -max_error_rate ratio of errors (0.0: no errors, 1.0: 100% error maximum error 147 | rate 148 | -bits_per_raw_sample number set the number of bits per raw sample 149 | -vol volume change audio volume (256=normal) 150 | 151 | Per-file main options: 152 | -f fmt force format 153 | -c codec codec name 154 | -codec codec codec name 155 | -pre preset preset name 156 | -map_metadata outfile[,metadata]:infile[,metadata] set metadata information of 157 | outfile from infile 158 | -t duration record or transcode "duration" seconds of audio/video 159 | -to time_stop record or transcode stop time 160 | -fs limit_size set the limit file size in bytes 161 | -ss time_off set the start time offset 162 | -sseof time_off set the start time offset relative to EOF 163 | -seek_timestamp enable/disable seeking by timestamp with -ss 164 | -timestamp time set the recording timestamp ('now' to set the current time) 165 | -metadata string=string add metadata 166 | -target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv5 167 | 0" with optional prefixes "pal-", "ntsc-" or "film-") 168 | -apad audio pad 169 | -frames number set the number of frames to output 170 | -filter filter_graph set stream filtergraph 171 | -filter_script filename read stream filtergraph description from a file 172 | -reinit_filter reinit filtergraph on input parameter changes 173 | -discard discard 174 | -disposition disposition 175 | 176 | Video options: 177 | -vframes number set the number of video frames to output 178 | -r rate set frame rate (Hz value, fraction or abbreviation) 179 | -s size set frame size (WxH or abbreviation) 180 | -aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777) 181 | -bits_per_raw_sample number set the number of bits per raw sample 182 | -vn disable video 183 | -vcodec codec force video codec ('copy' to copy stream) 184 | -timecode hh:mm:ss[:;.]ff set initial TimeCode value. 185 | -pass n select the pass number (1 to 3) 186 | -vf filter_graph set video filters 187 | -ab bitrate audio bitrate (please use -b:a) 188 | -b bitrate video bitrate (please use -b:v) 189 | -dn disable data 190 | 191 | Audio options: 192 | -aframes number set the number of audio frames to output 193 | -aq quality set audio quality (codec-specific) 194 | -ar rate set audio sampling rate (in Hz) 195 | -ac channels set number of audio channels 196 | -an disable audio 197 | -acodec codec force audio codec ('copy' to copy stream) 198 | -vol volume change audio volume (256=normal) 199 | -af filter_graph set audio filters 200 | 201 | Subtitle options: 202 | -s size set frame size (WxH or abbreviation) 203 | -sn disable subtitle 204 | -scodec codec force subtitle codec ('copy' to copy stream) 205 | -stag fourcc/tag force subtitle tag/fourcc 206 | -fix_sub_duration fix subtitles duration 207 | -canvas_size size set canvas size (WxH or abbreviation) 208 | -spre preset set the subtitle options to the indicated preset 209 | 210 | 翻到最开始的位置,跳过上面的一大串版本信息什么的,我们能看到一行 `Hyper fast Audio and Video encoder` ,它的意思就是“超快的音频和视频编码器”,这就算是对 FFmpeg 的一句简短的描述了。 211 | 212 | 接下来的一句 `usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...` 也就代表了 FFmpeg 的主要用法,我们可以看出,一条命令以 `ffmpeg` 开头,后面是可选的选项和参数,输入文件,输出文件……看不懂没关系,用多了自然就明白了。 213 | 214 | 再下面 `Getting help: ` 这一段,就说明了其他的一些获取帮助的方法, `-h long` 可以看到更为详细的帮助, `-h full` 可以看见超级详细的帮助(相对的,也非常的长)。我们现在只是做最简单的转码,还不需要用到这些高级的选项。 215 | 216 | 再往下面的内容很多,我就不一一说明了,不要求全都看懂,能看懂一点是一点。左侧是选项的名称,右侧是英文的说明。如果你感兴趣,可以在学会基础的转码以后把每个选项都试一试,这是一个非常好的消磨时间的方法。 217 | 218 | 219 | ## “选项”与“参数” 220 | 221 | 既然这是一个以命令行为基础的程序,我们可能需要掌握一些关于命令行的常识。不过,如果你觉得你暂时不想看这些令人头疼的东西,可以先跳过这节。 **如果在之后的部分中遇到了困难,再回来看也不迟。** 222 | 223 | 在命令行上,一条一长串的命令一般由两部分组成,程序与这条命令的“参数”。比如,在这样一条命令中: 224 | 225 | ffmpeg -i a.mp4 -an -c:v vp9 b.webm 226 | 227 | `ffmpeg` 就是程序,而除此之外的后面的东西,也就是 `-i a.mp4 -an -c:v vp9 b.webm` 就是这条命令的参数。 228 | 229 | 命令的参数还可以再进行更详细的分隔,它以空格做间隔,比如以上的这些参数就被分解成了 `-i`, `a.mp4`, `-an`, `-c:v`, `vp9`, `b.webm` 这 6 段。 230 | 231 | 以横杠开头的段被称为“选项”,在这个例子中有 `-i`, `-an`, `-c:v` 三个选项。“选项”就像一个开关,在命令中写上它,程序就会知道你要让它做某样事情,否则就会按默认的来。 232 | 233 | 但是有一些选项不仅仅是一个开关,除了要打开开关以外,你还要告诉它一些额外的信息,这些额外的信息紧跟在选项后面,被称作 **选项** 的参数。比如在这里, `a.mp4` 就是 `-i` 选项的参数, `vp9` 就是 `-c:v` 选项的参数, `-an` 选项不需要参数。 234 | 235 | 一个选项是否需要参数可以在帮助中找到,比如,在 `ffmpeg -help` 中可以找到这么一行: 236 | 237 | -t duration record or transcode "duration" seconds of audio/video 238 | 239 | 这就是对 `-t` 选项的说明,前面的 `-t duration` 表示这个选项需要一个叫 "duration" 的参数。后面的 `record or transcode "duration" seconds of audio/video` 是对这个选项以及参数的解释。在这里它的意思是“录制或编码 "duration" 秒的音频或视频”,通俗地说就是 `-t` 选项是用来指定时间的,后面跟着一个参数,以秒为单位。 240 | 241 | 如果一个需要参数的选项没有获得它的参数,程序就会出问题,多数情况下,它会报错并给你指示。 242 | 243 | 最后的 `b.webm` 是一个固有的参数,它不属于任何选项,但是在 FFmpeg 中它被要求写上,因为这就是转码后输出的文件名,它一定要写在最后。看看帮助中的 `usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...` ,我们就能找到我们为什么要这样做——名叫 `outfile` (也就是输出文件)的参数要放在最后面。 244 | 245 | **别的程序不一定需要这样的参数,也不一定符合这一节的规律,这里只讲 FFmpeg 。** 246 | 247 | 248 | ## 支持哪些格式? 249 | 250 | 你可能有些踌躇,因为你以前从来没有听说过这个叫 FFmpeg 的软件。“它应该很小众吧”你也许会这样想,“那么支持的媒体格式也很有限咯?” 251 | 252 | 在第一章我就有描述, FFmpeg 是一个被广泛使用的多媒体编码器库,有多广泛呢?广泛到除了某些财大气粗的商业公司,几乎所有的需要用到媒体的软件都用到了它。之所以你没听说过它的名字,是因为它通常都藏在后端。 253 | 254 | 现在你不会怀疑 FFmpeg 的支持的格式数量了吧?几乎所有。 255 | 256 | 跟据 `ffmpeg -help` 的指示,要查看所有支持的“封装格式”,输入 `ffmpeg -formats` ,然后我们会得到一大堆输出,因为实在太长了,我只截一小段给大家看一下: 257 | 258 | File formats: 259 | D. = Demuxing supported 260 | .E = Muxing supported 261 | -- 262 | E 3g2 3GP2 (3GPP2 file format) 263 | E 3gp 3GP (3GPP file format) 264 | D 4xm 4X Technologies 265 | E a64 a64 - video for Commodore 64 266 | D aac raw ADTS AAC (Advanced Audio Coding) 267 | DE ac3 raw AC-3 268 | D act ACT Voice file format 269 | D adf Artworx Data Format 270 | D adp ADP 271 | E adts ADTS AAC (Advanced Audio Coding) 272 | DE adx CRI ADX 273 | D aea MD STUDIO audio 274 | D afc AFC 275 | DE aiff Audio IFF 276 | DE alaw PCM A-law 277 | D alias_pix Alias/Wavefront PIX image 278 | DE alsa ALSA audio output 279 | 280 | 我们能看见许多我们熟悉的格式以及更多不熟悉的格式。 281 | 282 | 接下来还有他支持的编解码器 (codec) ,按照帮助,我们要输入 `ffmpeg -codecs` ,以下是输出的一部分: 283 | 284 | Codecs: 285 | D..... = Decoding supported 286 | .E.... = Encoding supported 287 | ..V... = Video codec 288 | ..A... = Audio codec 289 | ..S... = Subtitle codec 290 | ...I.. = Intra frame-only codec 291 | ....L. = Lossy compression 292 | .....S = Lossless compression 293 | \------- 294 | D.VI.. 012v Uncompressed 4:2:2 10-bit 295 | D.V.L. 4xm 4X Movie 296 | D.VI.S 8bps QuickTime 8BPS video 297 | .EVIL. a64_multi Multicolor charset for Commodore 64 (encoders: a64multi ) 298 | .EVIL. a64_multi5 Multicolor charset for Commodore 64, extended with 5th color (colram) (encoders: a64multi5 ) 299 | D.V..S aasc Autodesk RLE 300 | D.VIL. aic Apple Intermediate Codec 301 | DEVI.S alias_pix Alias/Wavefront PIX image 302 | DEVIL. amv AMV Video 303 | D.V.L. anm Deluxe Paint Animation 304 | 305 | 这些就是你电脑中 FFmpeg 所支持的格式和编码了,够多吧? 306 | 307 | 308 | ## 中文是世界上最美的语言,但是…… 309 | 310 | 看到这里了,你的心中可能有一个非常大的疑惑,那就是——为什么这个软件只有英语?名字是英语的、帮助是英语的、提示也是英语的。 311 | 312 | 原因其实显而易见,英语是目前世界上使用人数最多的语言(汉语是 **母语** 人数最多的),而 FFmpeg 的开发者和用户遍布世界各地,它不可能做很多种不同语言的版本,那么既然大家都或多或少看得懂英语,那就采用它了。 313 | 314 | 如果你觉得你的英语水平看懂这点东西没问题,那真是太好了。如果你觉得你的英语水平还是不够,这也不应该成为你的阻碍。 315 | 316 | 看不懂怎么办? 上网翻译! 317 | 318 | 你可能一开始会进度缓慢,看懂一段话都十分困难。但我相信我的读者的脑袋都是足够聪明的,过一段时间你就会发现,这些文档看起来舒服多了。 319 | 320 | 英语是现代人必备的技能之一,至少在中国大陆,它是属于义务教育中的,如果你觉得“我只是想转个视频,为此学英语成本太高了”,那别怪我嘴下无情——你英语考试多少分?因为我认为,以小学毕业的英语水平,借助网上翻译看懂 FFmpeg 的这点东西还是不难的。 321 | 322 | 中文是世界上最美的语言,但是英文是世界上使用最广泛的语言。如果你不会英语,那这个世界上你会错过的精彩东西就太多了, FFmpeg 就是其中之一。 323 | 324 | 不要慌乱,打开翻译,鼠标选中,右键复制,右键粘帖,按下回车。 325 | 326 | -------------------- 327 | 328 | 看了这么多了,或许你需要喝口水,休息一下之后再继续阅读[第 4 章](04-media-file-structure.md)。 329 | -------------------------------------------------------------------------------- /04-media-file-structure.md: -------------------------------------------------------------------------------- 1 | # 媒体文件的结构 2 | 3 | 知道了命令行怎么使用以后,我们还得搞清一些概念,才能开始使用 FFmpeg 。在这一章,我将会为大家讲明一个媒体文件的组成,免得到后面混淆概念,产生不应该产生的问题。如果你已经对此有所了解,可以放心跳过这一章。 4 | 5 | 一个媒体文件并不像许多人想象的那样,是将媒体内容编码起来直接作为文件的。实际上,它通常是由多个不同种类的媒体流( Stream )组成,再以特定的封装格式封装起来的。 6 | 7 | 比较常见的媒体流就是视频流跟音频流了,顾名思义,视频流存储的就是视频信息,音频流存储的就是音频信息。一个视频流或者音频流的内容,就是以特定的编码格式所存储的视频或音频信息。 8 | 编码格式有许多种,有的适用于视频,有的适用于音频,大家运行 `ffmpeg -codecs` 后所看到的内容就是 FFmpeg 支持的编码格式。 **一个文件的里面的媒体流所采用的编码格式跟这个文件的后缀名并没有完全的必然联系。** 9 | 10 | 不同的编码格试的编码效率以及功能不同,要查看一个编码格式的详细信息和参数,运行 `ffmpeg -help encoder=编码格式` 。 11 | 12 | 裸露的音频流和视频流就像白饭团,拿起来容易散开,吃起来也没什么味道。所以通常情况下,我们需要一条紫菜将它们包裹起来成为寿司,这个紫菜就是封装格式。 13 | 封装格式也有很多种,大家运行 `ffmpeg -formats` 就可以看到。 **文件的后缀名通常就代表这个文件的封装格式。** 14 | 15 | 现在我们有了用白米饭和紫菜做成的寿司,不过白米饭未免显得有些单调了,所以我们也可以加一些蔬菜、肉什么的进去。比如字幕,附件什么的。这些将会在后面的章节讲到。 16 | 17 | **不同的封装格式不一定能够装下同样的东西。** 有的封装格式可以装下视频流和音频流,有的只能装下音频流,有的只能装下视频流。有的能塞进很多东西,有的只能放特定的几个媒体流。要查看一个封装格式的详细信息,运行 `ffmpeg -help muxer=封装格式` 。 18 | 19 | 总而言之,一个媒体文件就像一个寿司,由一条紫菜(封装格式)包裹,里面包含了许多米饭和蔬菜。 20 | 21 | ------------------------- 22 | 23 | 下一章将是我们的重头戏——[开始转码](05-start-converting.md)。看了这么多枯燥的理论知识,是时候发挥其实际用途了! 24 | -------------------------------------------------------------------------------- /05-start-converting.md: -------------------------------------------------------------------------------- 1 | # 开始转码 2 | 3 | 4 | ## 简单的文件输入与输出 5 | 6 | > 用 FFmpeg 来执行简单的格式转换出奇的简单!你不相信吗?来试一试就知道了! 7 | 8 | 首先你要准备一个视频文件,大部分你能见到的视频文件都可以,只要不是有 [DRM 保护](https://zh.wikipedia.org/wiki/%E6%95%B0%E5%AD%97%E7%89%88%E6%9D%83%E7%AE%A1%E7%90%86)的或者某某客户端的“专有格式”,基本上 FFmpeg 都能应付。不过作为练习,最好不要拿太大的文件,因为这样转换的时间会比较长。 9 | 10 | 我这里使用 [Tor](https://zh.wikipedia.org/wiki/Tor) 的介绍视频来作为例子,可以从[这个页面](https://blog.torproject.org/blog/releasing-tor-animation)来下载到,我所选择的是 [English HQ](https://media.torproject.org/video/2015-03-animation/HQ/Tor_Animation_en.mp4) 版本。当然,如我刚刚所说,你没有必要非得使用这个视频。 11 | 12 | 假设这个视频已经下载到了你的电脑硬盘中,并存放在 `/home/alex/Downloads/` 这个目录里面,而且 FFmpeg 已经[安装](02-download-and-install.md)到你的电脑中。那么现在[打开终端](03-execute.md#look-help),然后使用 `cd` 命令转到文件所在的位置,比如在我的情况下,运行 `cd /home/alex/Downloads/` ,它不会有任何输出。 13 | 14 | - **注意:** 如果文件路径包含空格或一些特殊字符,请使用半角引号( `'` 或 `"` )将路径名包起来,比如如果文件在 `/home/alex/path with space/` ,执行 `cd "/home/alex/path with space/"` 而 **不是** `cd /home/alex/path with space/` 。 15 | 16 | - **注意:** 如果你使用的是 Microsoft® Windows® ,并且文件不在 C 盘,需要再“执行”一下盘符,比如如果文件存放在 `D:\\Downloads\` 里面,执行以下两条命令: 17 | 18 | cd D:\\Downloads\ 19 | D: 20 | 21 | `cd` 完了之后,如果你有强迫症想要检查一下是否真的已经切换到目标目录去了,可以运行 `ls` ( GNU/Linux, Mac OS X® )或 `dir` ( Microsoft® Windows® )。只要显示出来的确实是那个目录里的文件,就说明没有问题,你可以放心继续。 22 | 23 | ![运行 cd 和 ls](image/cd-ls.png) 24 | 25 | 接下来输入 `ffmpeg` 但不要回车,我们要使用 `-i` [选项](03-execute.md#options-and-arguments)来指定输入文件。 26 | 输入文件是什么?就是我们要转换的文件呀!比如我在此处要转换的文件叫做 `Tor_Animation_en.mp4` ,我就再往命令行中打 `-i Tor_Animation_en.mp4` 。 27 | 28 | 在指定了输入文件之后,我们还要指定一个输出文件,不然 FFmpeg 把转换出来的文件保存在哪儿呢? 29 | 输出文件可以是任何名字,但它的后缀名很关键,[上一章](04-media-file-structure.md)已经讲过,后缀名通常就代表着文件的封装格式。在这个例子中,我想将视频转换为 [Matroska](https://zh.wikipedia.org/wiki/Matroska) 格式(俗称 MKV 格式),那么我输出文件的后缀名就得是 `.mkv` 。 30 | 最终,我想将文件保存为 `tor.mkv` ,就在命令行后面直接加上这个文件名。 31 | 32 | 最后,我的命令行上有着这样一条命令: 33 | 34 | ffmpeg -i Tor_Animation_en.mp4 tor.mkv 35 | 36 | - **注意:** 跟 `cd` 时的目录路径一样,如果文件名包含空格或一些特殊字符,也需要用半角引号将它们包裹起来。比如如果输入文件名叫 `input file.mp4` 且输出文件名叫 `output file.mkv` ,整条命令就得是 `ffmpeg -i "input file.mp4" "output file.mkv"` 。 37 | 38 | 按下回车吧! 39 | 40 | 虽然这一节的内容很长,不过你读过之后应该就会发现,这其实就是这么一条简单的命令。为了使大家更好理解,我才将这一节写的这么详细。 41 | 42 | 如果你使用的文件很小,而且电脑性能不是特别差,可能你还没有读完上一段话,就已经转换完成了。你的命令行窗口可能看起来像是这样: 43 | ![转码成功完成](image/successful-completed.png) 44 | 45 | 你通常不必去管里面写的是什么,只要没出现红色的 `ERROR` ,一般就是没有问题地完成了。 46 | 47 | 现在,你闪闪发亮的输出文件已经保存在原来的目录中了。在我这里,这个 `tor.mkv` 就是转换后的输出文件。 48 | ![在文件管理器中看到两个文件](image/files-in-explorer.png) 49 | 50 | 51 | ## 指定编码器 52 | 53 | FFmpeg 会自动判断输入文件的封装格式以及编码,并且根据输出文件的后缀名来判断其封装格式。同时, FFmpeg 也会根据该种封装格式的默认编码来决定输出文件的编码。 54 | 55 | 运行 `ffmpeg -help muxer=matroska` 就可以查看 Matroska 格式的默认编码了,我们可以在这条命令的输出中找到这样几行字: 56 | 57 | Muxer matroska [Matroska]: 58 | Common extensions: mkv. 59 | Mime type: video/x-matroska. 60 | Default video codec: h264. 61 | Default audio codec: vorbis. 62 | Default subtitle codec: ass. 63 | 64 | 它们的意思是: Matroska 封装格式,通常的后缀名为 `mkv` ,[MIME](https://zh.wikipedia.org/wiki/%E5%A4%9A%E7%94%A8%E9%80%94%E4%BA%92%E8%81%AF%E7%B6%B2%E9%83%B5%E4%BB%B6%E6%93%B4%E5%B1%95) 为 `video/x-matroska` ,默认视频编码为 `h264` ,默认音频编码为 `vorbis` ,默认字幕编码为 `ass` 。如果你的[英语水平没问题](03-execute.md#chinese-is-most-beautiful-but),读懂这些将轻而易举。 65 | 66 | - **注意:** FFmpeg 的版本不同,各种封装格式的默认编码器也有可能不同,请[确认](03-execute.md#look-help)你所拥有的 FFmpeg 是最新版本的。 67 | 68 | [H264](https://zh.wikipedia.org/wiki/H.264/MPEG-4_AVC) 和 [Vorbis](https://zh.wikipedia.org/wiki/Vorbis) 都是两种十分流行的编码,大部分播放器都可以播放。不过,如果我想使用 Matroska 作为封装格式,但使用 [HEVC](https://zh.wikipedia.org/wiki/%E9%AB%98%E6%95%88%E7%8E%87%E8%A7%86%E9%A2%91%E7%BC%96%E7%A0%81) 作为视频编码,该怎么办呢? 69 | 70 | 快使用“帮助大法”,执行 `ffmpeg -help` 查看帮助! 71 | 然后我们能看到这样一行: 72 | 73 | -vcodec codec force video codec ('copy' to copy stream) 74 | 75 | 它告诉我们,使用 `-vcodec` 选项,可以指定视频编码器。如果指定 `copy` 将不会重新编码,而是直接把原来的媒体流拷贝过去。 76 | 77 | 现在我们可以在原来的命令( `ffmpeg -i Tor_Animation_en.mp4 tor.mkv` )的 **输出文件名之前** ,加上 `-vcodec hevc` ,这样 FFmpeg 就会使用 HEVC 而不是 H264 进行视频的编码。 78 | 最后我们要使用的命令是这样的: 79 | 80 | ffmpeg -i Tor_Animation_en.mp4 -vcodec hevc tor.mkv 81 | 82 | 按下回车! 83 | 84 | 如果出现了 `File 'tor.mkv' already exists. Overwrite ? [y/N]` ,不要惊慌,他只是告诉你 `tor.mkv` 这个文件已经存在了,并问你是否要覆盖。如果你要覆盖原来的文件,按 `y` 再回车;如果你不想覆盖,按 `n` 回车,把文件名改一下,再重新执行命令。 85 | 86 | 这回的等待时间可能就长一些了,因为 HEVC 是一个压缩比更高的编码,需要的时间也长。 87 | 88 | 同样的,在 `ffmpeg -help` 的输出中,我们也可以找到 `-acodec` 选项,它是用来指定音频编码的。所以如果我想要以 Matroska 作为封装格式, HEVC 作为视频编码, [Opus](https://zh.wikipedia.org/wiki/Opus_codec) 作为音频编码,使用的命令会是这样的: 89 | 90 | ffmpeg -i Tor_Animation_en.mp4 -vcodec hevc -acodec opus tor.mkv 91 | 92 | 如果我想要直接拷贝媒体流到输出文件中而不重新进行编码(也就是只转换一下封装格式),在编码器的位置上写 `copy` 。比如 `ffmpeg -i Tor_Animation_en.mp4 -vcodec copy -acodec copy tor.mkv` ,因为这样不进行编码,所以通常会很快。 93 | 94 | - **提示:** `-vcodec` 有一个缩写叫做 `-c:v` , `-acodec` 有一个缩写叫做 `-c:a` 。 95 | 也就是说, `ffmpeg -i Tor_Animation_en.mp4 -vcodec hevc -acodec opus tor.mkv` 可以写为 `ffmpeg -i Tor_Animation_en.mp4 -c:v hevc -c:a opus tor.mkv` ,两者没有任何区别。 96 | 使用这样的小把戏可以省下打几个字母的时间。以后我举例也一直都会用这种简单的写法。 97 | 98 | 99 | ## 照它说的做 100 | 101 | [AAC](https://zh.wikipedia.org/wiki/%E9%80%B2%E9%9A%8E%E9%9F%B3%E8%A8%8A%E7%B7%A8%E7%A2%BC) 是一种被十分广泛使用的音频编码,也是 [MP4](https://zh.wikipedia.org/wiki/MP4) 封装格式的默认音频编码。 FFmpeg 当然也对此提供了支持。那么现在我将我的视频转换为 Matroska 封装格式, HEVC 视频编码, AAC 音频编码吧! 102 | 103 | 可是,我在运行 `ffmpeg -i ffmpeg -i Tor_Animation_en.mp4 -c:v hevc -c:a aac tor.mkv` 的时候,它出错了,还以红色字体显示: 104 | 105 | [aac @ 0x55fcc749c580] The encoder 'aac' is experimental but experimental codecs 106 | are not enabled, add '-strict -2' if you want to use it. 107 | 108 | 这是什么情况?难道 FFmpeg 不支持 AAC 吗? 109 | 当然不是,忽略程序给出的错误提示是不应该的,我们来看一看这条错误提示吧! 110 | 111 | 它说,编码器 `aac` 是实验性的,但是它没有被启用,如果你想使用他的话就加上 `-strict -2` 。 112 | 113 | 既然 FFmpeg 告诉了我们要加上 `-strict -2` ,那我们就照他说的做,在输出文件之前加上这两个选项。不用管它究竟是什么意思,加就是了。 114 | 115 | ffmpeg -i Tor_Animation_en.mp4 -c:v hevc -c:a aac -strict -2 tor.mkv 116 | 117 | 好,这样就顺利完成了。 118 | 119 | FFmpeg 在出错的时候总会尽它的一切力量来减少你的麻烦,在它能做到的时候,甚至会手把手告诉你如何来解决,而且它的解决方式也通常是最有效的。即使它不能,也会给出详细的错误信息,方便你找到问题、上网搜索或请教别人。 120 | 121 | > 人生苦短,我看错误提示。 122 | 123 | 124 | ## 学会看输出 125 | 126 | 别看 FFmpeg 只有一条命令来让用户完成操作,实际上,它告诉你的信息多的令人惊讶! 127 | 128 | 129 | ### 转换时 130 | 131 | 在执行了上一节最后给出的命令后,我的终端窗口冒出了大量的字符,但是还没等我看完,就迅速被这样的信息给填满了: 132 | 133 | frame= 28 fps=0.0 q=0.0 size= 2kB time=00:00:01.49 bitrate= 11.3kbits/s 134 | frame= 30 fps= 17 q=-0.0 size= 13kB time=00:00:01.49 bitrate= 71.1kbits/ 135 | frame= 34 fps= 15 q=-0.0 size= 20kB time=00:00:01.66 bitrate= 99.9kbits/ 136 | frame= 38 fps= 13 q=-0.0 size= 31kB time=00:00:01.83 bitrate= 138.1kbits/ 137 | frame= 42 fps= 12 q=-0.0 size= 40kB time=00:00:02.00 bitrate= 165.1kbits/ 138 | frame= 46 fps= 11 q=-0.0 size= 49kB time=00:00:02.17 bitrate= 185.4kbits/ 139 | frame= 50 fps= 10 q=-0.0 size= 57kB time=00:00:02.34 bitrate= 199.3kbits/ 140 | frame= 54 fps= 10 q=-0.0 size= 63kB time=00:00:02.51 bitrate= 204.9kbits/ 141 | frame= 58 fps=9.5 q=-0.0 size= 74kB time=00:00:02.68 bitrate= 226.2kbits/ 142 | frame= 62 fps=9.2 q=-0.0 size= 85kB time=00:00:02.68 bitrate= 260.5kbits/ 143 | frame= 65 fps=8.8 q=-0.0 size= 92kB time=00:00:02.85 bitrate= 264.9kbits/ 144 | 145 | 这些是什么玩意儿?我没有看到我的进度条,也没有百分比,难道我要在这里干等着,不知道什么时候转码才会结束吗? 146 | 147 | FFmpeg 确实不会显示进度条和百分比,不过,它会给你比进度条和百分比还要多的信息。 148 | 149 | 1. 最左边的 `frame= 65` 是转码所进行到的[帧](https://zh.wikipedia.org/wiki/%E5%B8%A7)数,显示 65 就表示现在已经转到了第 65 帧。 150 | 2. 第二个 `fps=8.8` 中的 FPS 就是 **F**rame **p**er **S**econd ,也就是现在电脑每秒所处理的帧的数量。注意这个数字跟视频的帧率并无关系。 151 | 3. ~~其实我也不知道后面那个 `q=-0.0` 是什么意思,大家不要来打我呀。~~ 152 | 4. 接下来的 `size= 92kB` 表示现在已经转换出来的视频的体积,这个数字只会越变越大啊。 153 | 5. 第五个 `time=00:00:02.85` 顾名思义就是时间了,它是已经转换出来的视频的时间。在我看来,它也是一个比百分比进度条更加精准的进度显示。 154 | 155 | 如果你认为这么多信息是“无用的”,那么只要看 `time` 这一栏就好,我想你应该知道你原来的视频有多长,那么从已经转换的时间应该能看出你已经完成了多少了吧? 156 | 157 | 158 | ### 转换前 159 | 160 | 话说回来,在我的终端被这些信息刷屏之前,出来的一大堆~~看起来很不错的~~文字是什么? 161 | 让我们用鼠标滚轮滚回去看看。 162 | 163 | ffmpeg version 2.8 Copyright (c) 2000-2015 the FFmpeg developers 164 | built with gcc 5.2.0 (GCC) 165 | configuration: --prefix=/usr --disable-debug --disable-static --disable-stripp 166 | ing --enable-avisynth --enable-avresample --enable-fontconfig --enable-gnutls -- 167 | enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreety 168 | pe --enable-libfribidi --enable-libgsm --enable-libmodplug --enable-libmp3lame - 169 | -enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enab 170 | le-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable- 171 | libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis 172 | --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libx 173 | vid --enable-shared --enable-version3 --enable-x11grab 174 | libavutil 54. 31.100 / 54. 31.100 175 | libavcodec 56. 60.100 / 56. 60.100 176 | libavformat 56. 40.101 / 56. 40.101 177 | libavdevice 56. 4.100 / 56. 4.100 178 | libavfilter 5. 40.101 / 5. 40.101 179 | libavresample 2. 1. 0 / 2. 1. 0 180 | libswscale 3. 1.101 / 3. 1.101 181 | libswresample 1. 2.101 / 1. 2.101 182 | libpostproc 53. 3.100 / 53. 3.100 183 | Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Tor_Animation_en.mp4': 184 | Metadata: 185 | major_brand : mp42 186 | minor_version : 0 187 | compatible_brands: mp42isomavc1 188 | creation_time : 2015-03-12 15:36:45 189 | encoder : HandBrake 0.9.5 2011010300 190 | Duration: 00:02:17.45, start: 0.000000, bitrate: 842 kb/s 191 | Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709) 192 | , 1920x1080 [SAR 1:1 DAR 16:9], 674 kb/s, 24 fps, 24 tbr, 90k tbn, 180k tbc (def 193 | ault) 194 | Metadata: 195 | creation_time : 2015-03-12 15:36:45 196 | encoder : JVT/AVC Coding 197 | Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, flt 198 | p, 163 kb/s (default) 199 | Metadata: 200 | creation_time : 2015-03-12 15:36:45 201 | x265 [info]: HEVC encoder version 1.7 202 | x265 [info]: build info [Linux][GCC 5.1.0][64 bit] 8bpp 203 | x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX 204 | x265 [info]: Main profile, Level-4 (Main tier) 205 | x265 [info]: Thread pool created using 4 threads 206 | x265 [info]: frame threads / pool features : 2 / wpp(17 rows) 207 | x265 [info]: Coding QT: max CU size, min CU size : 64 / 8 208 | x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra 209 | x265 [info]: ME / range / subpel / merge : hex / 57 / 2 / 2 210 | x265 [info]: Keyframe min / max / scenecut : 24 / 250 / 40 211 | x265 [info]: Lookahead / bframes / badapt : 20 / 4 / 2 212 | x265 [info]: b-pyramid / weightp / weightb / refs: 1 / 1 / 0 / 3 213 | x265 [info]: AQ: mode / str / qg-size / cu-tree : 1 / 1.0 / 64 / 1 214 | x265 [info]: Rate Control / qCompress : CRF-28.0 / 0.60 215 | x265 [info]: tools: rd=3 psy-rd=0.30 signhide tmvp strong-intra-smoothing 216 | x265 [info]: tools: deblock sao 217 | Output #0, matroska, to 'tor.mkv': 218 | Metadata: 219 | major_brand : mp42 220 | minor_version : 0 221 | compatible_brands: mp42isomavc1 222 | encoder : Lavf56.40.101 223 | Stream #0:0(und): Video: hevc (libx265), yuv420p, 1920x1080 [SAR 1:1 DAR 16: 224 | 9], q=2-31, 24 fps, 1k tbn, 24 tbc (default) 225 | Metadata: 226 | creation_time : 2015-03-12 15:36:45 227 | encoder : Lavc56.60.100 libx265 228 | Stream #0:1(eng): Audio: aac ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fl 229 | tp, 128 kb/s (default) 230 | Metadata: 231 | creation_time : 2015-03-12 15:36:45 232 | encoder : Lavc56.60.100 aac 233 | Stream mapping: 234 | Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265)) 235 | Stream #0:1 -> #0:1 (aac (native) -> aac (native)) 236 | Press [q] to stop, [?] for help 237 | frame= 28 fps=0.0 q=0.0 size= 2kB time=00:00:01.49 bitrate= 11.3kbits/s 238 | 239 | 这些数据可有点多啊,我不会把每一句话都作详细说明,只是挑一些用处比较大的讲一下。 240 | 241 | 上面的版本信息不用管,然后我们能看到一行 `Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Tor_Animation_en.mp4':` ,显然这下面就是关于第 0 个输入文件的信息了。(别忘了,在 IT 界,许多东西都喜欢以 0 而不是 1 开始计数) 242 | 243 | `Duration: 00:02:17.45, start: 0.000000, bitrate: 842 kb/s` 这一行告诉我们这个视频长度是 2 分 17 秒 45 ,从 0.000000 开始,比特率 842 kb/s 。 244 | 245 | 下面一行很长,不过我们只看前面一段 `Stream #0:0(und): Video: h264` ,它告诉我们, 0 号输入文件的 0 号媒体流,视频流, `h264` 编码。 246 | 247 | 然后是 `Stream #0:1(eng): Audio: aac` , 0 号输入文件的 1 号媒体流,音频流, `aac` 编码。 248 | 249 | 再往下 `x265 [info]` 开头的一大堆,都是 HEVC 的编码程序 `x265` 所给出的信息,看不懂就甭管了。 250 | 251 | 后面我们就能看到关于输出文件的信息,还有其编码。 252 | 253 | 接下来重要的信息来了: 254 | 255 | Stream mapping: 256 | Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265)) 257 | Stream #0:1 -> #0:1 (aac (native) -> aac (native)) 258 | 259 | 媒体流的分配,很直观,我们能看到输入文件的视频流从 `h264` 转成了 `hevc` ,音频流从 `aac` 还是转成了一样的 `aac` 。从这里你可以快速的检查一下,你是否输入了你想要执行的命令。 260 | 261 | 哦,还不能忘记最后一条很有用的信息, `Press [q] to stop, [?] for help` ,它告诉你在转码的时候按 q 来中断, ? 来查看帮助。有经验的人可能知道用 Ctrl + C 可以中断,不过那个是强制退出,按 q 是相对自然一些的中断。 262 | 263 | 按 ? 会出来什么呢?感兴趣的话就自己来探索一下吧。 264 | 265 | > 谁掌握了输出,谁就掌握了程序。 266 | 267 | ------------------ 268 | 269 | 读到这里,你应该能使用 FFmpeg 来进行简单的转码了,而这个教程的“入门”阶段也完成了。在[下一章](06-do-in-passing-while-converting.md),我们将会使 FFmpeg 变得更加有效率和实用。 270 | -------------------------------------------------------------------------------- /06-do-in-passing-while-converting.md: -------------------------------------------------------------------------------- 1 | # 转码时能顺便一起做的事情 2 | 3 | 虽然这篇教程的主要内容是视频转码,但除了将文件在各种封装格式、编码中转换以外,一些其他的调整也是很有用的。 4 | 5 | 在这一章中,我们将学到许多具有各种功能的[命令行选项](03-execute.md#options-and-arguments),这些选项都需要加在整条命令的输出文件名之前。 6 | 7 | 万变不离其宗,在前面介绍的[查看帮助](03-execute.md#look-help)一节中学到的 `ffmpeg -help` 命令仍然是非常实用的,本章也将一直围绕它的输出来进行。所以,在开始阅读第一节之前,先打开终端,执行一下 `ffmpeg -help` 吧! 8 | 9 | 10 | ## 音频 11 | 12 | 我们在 `ffmpeg -help` 的输出中能找到这样一段信息: 13 | 14 | Audio options: 15 | -aframes number set the number of audio frames to output 16 | -aq quality set audio quality (codec-specific) 17 | -ar rate set audio sampling rate (in Hz) 18 | -ac channels set number of audio channels 19 | -an disable audio 20 | -acodec codec force audio codec ('copy' to copy stream) 21 | -vol volume change audio volume (256=normal) 22 | -af filter_graph set audio filters 23 | 24 | 这些就是关于音频的选项了,由于有些东西涉及的知识太多,我在这里只介绍比较常用且简单的 `-ar`, `-an`, `-vol` 这几个选项。( `-acodec` 早就讲过了,不是吗?) 25 | 26 | 27 | ### -ar 28 | 29 | 如上面所说, `-ar` 选项后面还要跟一个叫 `rate` 的参数,不过参数叫什么名字对我们来说不重要,我们只要知道后面要有一个参数就可以了。这个参数是音频的[采样率](https://zh.wikipedia.org/wiki/%E9%87%87%E6%A0%B7%E7%8E%87),以 Hz 为单位。 30 | 31 | 通常来说,采样率越大,音质越好。电话的采样率通常是 8000 Hz ,普通的录音笔通常是 32000 Hz ,一般的 MP3 音乐是 44100 Hz ,稍高品质的音乐是 48000 Hz ,再高的采样率我们平常就不容易接触到了。 32 | 33 | 以下是一个将音频文件 `input.mp3` 转换为 [Ogg](https://zh.wikipedia.org/wiki/Ogg) 封装格式, [Vorbis](https://zh.wikipedia.org/wiki/Vorbis) 编码,并且指定音频采样率为 32000 Hz 的例子: 34 | 35 | ffmpeg -i input.mp3 -c:a libvorbis -ar 32000 output.ogg 36 | 37 | - **注意:** 在 FFmpeg 中使用 Vorbis 编码时,编码器应指定为 `libvorbis` 而不是 `vorbis` ,这是两个不同的编码器,而后者还处于不完善的状态。 38 | 39 | - **提示:** 如果你喜欢的话,数字 `32000` 可以简写为 `32k` ,因为 "k" 就相当与“千”。 40 | 41 | - **注意:** 采样率只能变小,不能变大。当然你要是指定一个比原来大的采样率也没人阻止你,不过音质不会有任何的提升就是了。原理跟你把一张分辨率很小的图片拉大一样,画质没有任何提升。 42 | 43 | 44 | ### -an 45 | 46 | 这个其实没什么好讲的,它就是在进行视频转码的时候,将音频给去除,这样就会得到一个没有声音的视频。 47 | 48 | 49 | ### -vol 50 | 51 | 这个选项有一个参数,用来指定相对与原来的文件的音量大小。不过注意,标准的音量并不是 100 ,而是 256 ,也就是说,我指定了 `-vol 256` 就是原来的音量不变(跟没指定一样)。 52 | 53 | 下面的例子是将音量减小为原来的一半: 54 | 55 | ffmpeg -i input.mp3 -c:a libvorbis -vol 128 output.ogg 56 | 57 | 58 | ## 视频 59 | 60 | 在 `ffmpeg -help` 的输出中,我们也能找到这样一段信息: 61 | 62 | Video options: 63 | -vframes number set the number of video frames to output 64 | -r rate set frame rate (Hz value, fraction or abbreviation) 65 | -s size set frame size (WxH or abbreviation) 66 | -aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777) 67 | -bits_per_raw_sample number set the number of bits per raw sample 68 | -vn disable video 69 | -vcodec codec force video codec ('copy' to copy stream) 70 | -timecode hh:mm:ss[:;.]ff set initial TimeCode value. 71 | -pass n select the pass number (1 to 3) 72 | -vf filter_graph set video filters 73 | -ab bitrate audio bitrate (please use -b:a) 74 | -b bitrate video bitrate (please use -b:v) 75 | -dn disable data 76 | 77 | 同样的,实用为先,我在这里只挑 `-r`, `-s`, `-vn` 这几个选项来讲。 78 | 79 | 80 | ### -r 81 | 82 | `-r` 选项可以指定视频的[帧率](https://zh.wikipedia.org/wiki/%E5%B8%A7%E7%8E%87),其参数的单位是 Hz ,也就是我们平常所说的“帧每秒”( FPS )。比如 `-r 24` 即代表输出视频的帧率为每秒 24 帧。 83 | 84 | 当然,视频的帧率跟音频的采样率一样,只能变小不能变大。 85 | 86 | 87 | ### -s 88 | 89 | 这个选项可以指定视频的尺寸,以像素为单位。这个选项的参数由 `宽度x高度` 的格式填写。比如如果要把视频转为 720P (也就是宽度 1280 像素,高度 720 像素),就写上 `-s 1280x720` 。没错,中间用小写字母 `x` 分隔两个数字。 90 | 91 | 下面的例子会将输出文件视频的帧率指定为 15 帧每秒,画面尺寸 1280x720 : 92 | 93 | ffmpeg -i input.mp4 -r 15 -s 1280x720 output.mkv 94 | 95 | 96 | ### -vn 97 | 98 | 与 `-an` 选项一样,在命令中加入了这个选项之后,视频内容便会被去除。也就是说,只把音频剥离出来。比如,把 `video.mp4` 这个视频中的音频提取出来,保存为 `audio.mp3` ,指定音频编码为 `mp3` ,将使用这样一个命令: 99 | 100 | ffmpeg -i video.mp4 -vn -c:a mp3 audio.mp3 101 | 102 | - **提示:** MP3 封装格式的默认音频编码已经是 `mp3` ,我在这里写 `-c:a mp3` 其实是多此一举。 103 | 104 | 105 | ## 整体 106 | 107 | 除了仅仅针对媒体流的选项以外,还有一些针对整个媒体文件的调整选项。那么我们先在 `ffmpeg -help` 的输出中找出这样一段内容: 108 | 109 | Per-file main options: 110 | -f fmt force format 111 | -c codec codec name 112 | -codec codec codec name 113 | -pre preset preset name 114 | -map_metadata outfile[,metadata]:infile[,metadata] set metadata information of 115 | outfile from infile 116 | -t duration record or transcode "duration" seconds of audio/video 117 | -to time_stop record or transcode stop time 118 | -fs limit_size set the limit file size in bytes 119 | -ss time_off set the start time offset 120 | -sseof time_off set the start time offset relative to EOF 121 | -seek_timestamp enable/disable seeking by timestamp with -ss 122 | -timestamp time set the recording timestamp ('now' to set the current time) 123 | -metadata string=string add metadata 124 | -target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv5 125 | 0" with optional prefixes "pal-", "ntsc-" or "film-") 126 | -apad audio pad 127 | -frames number set the number of frames to output 128 | -filter filter_graph set stream filtergraph 129 | -filter_script filename read stream filtergraph description from a file 130 | -reinit_filter reinit filtergraph on input parameter changes 131 | -discard discard 132 | -disposition disposition 133 | 134 | 我在这里将会挑选 `-c`, `-t`, `-ss`, `-metadata` 这几个选项来讲。 135 | 136 | 137 | ### -c 138 | 139 | 其实这个选项我们在[第 5 章第 2 节](05-start-converting.md#specify-codec)的最后已经讲过了,不过我觉得有必要再做一次详细的说明。 140 | 141 | `-c` 选项用来指定输出文件的编码,但跟 `-vcodec` 和 `-acodec` 不同的是,它指定的是全部媒体流的编码而不是单独一个视频流或音频流。在通常情况下,这是行不通的,因为视频编码和音频编码是两种不同的东西,怎么能是同一个呢? 142 | 143 | 但是,我们可以通过在 `-c` 后面紧挨着加上 `:v` 成为 `-c:v` 来表示指定视频编码,紧挨着加上 `:a` 成为 `-c:a` 来表示指定音频编码。也就跟 `-vcodec` 和 `-acodec` 一样了。不过 `-c:a`, `-c:a` 的写法比 `-vcodec`, `-acodec` 的写法要短,所以我喜欢用前者。 144 | 145 | 当然,单独的 `-c` 也是有用的,有什么用呢? 146 | 147 | 还记得在编码器的地方指定 `copy` 来让 FFmpeg 不重新进行编码吗?众所周知,大部分视频或音频编码都是[有损压缩](https://zh.wikipedia.org/wiki/%E6%9C%89%E6%8D%9F%E6%95%B0%E6%8D%AE%E5%8E%8B%E7%BC%A9),重新进行一次编码不但费时费力,还会产生无法挽回的画质/音质损失,损失一两次通常是人类很难判别的,但是次数多了差别就大了。所以,我们要尽量避免重新编码,能用 `copy` 作为“编码器”的时候就尽量使用它。 148 | 149 | 让视频不重新编码的时候使用 `-c:v copy` ,让音频不重新编码的时候使用 `-c:a copy` 。那么在两个都要不重新编码的时候,就不用把这两条选项都写上去了,只要 `-c copy` 就足够。 150 | 151 | 下面是一些例子: 152 | 153 | 1. 将视频尺寸转换为 `1280x720` ,其他不变。 154 | 因为视频的尺寸变了,所以视频不得不重新编码,但音频不用。 155 | 156 | ffmpeg -i input.mp4 -c:v h264 -c:a copy -s 1280x720 output.mp4 157 | 158 | 2. 将音频的编码转换为 `libvorbis` ,其他不变。 159 | 音频的编码肯定得变,但是视频不用。 160 | 161 | ffmpeg -i input.mp4 -c:v copy -c:a libvorbis output.mp4 162 | 163 | 3. 仅仅将 MP4 封装格式转换为 Matroska 封装格式。 164 | 只是封装格式变了,视频和音频都不需要重新编码。 165 | 166 | ffmpeg -i input.mp4 -c copy output.mkv 167 | 168 | 169 | ### -t 170 | 171 | 这个选项用来指定输出文件的持续时间,以秒为单位,比如我想截取 `full.mp4` 这个视频的前 30 秒并保存为 `segment.mp4` ,就可以使用这个命令: 172 | 173 | ffmpeg -i full.mp4 -c copy -t 30 segment.mp4 174 | 175 | - **注意:** 看到了 `-c copy` 了吗?因为我们只是进行截取,所以不需要进行重新编码。能不重新编码就尽量不要重新编码。 176 | 177 | 这个选项的参数以秒为单位,但这并不意味着 5 分钟你就得写 `300` , 1 个小时你就得写 `3600` , 1 小时 23 分钟 45 秒你就得先拿出计算器算出 1×3600+23×60+45=5025 再写上 `5025` 。 178 | 事实上,你只要写 `5:00` 就可以表示 5 分钟, `1:23:45` 就可以表示 1 小时 23 分钟 45 秒。 179 | 180 | 你也可以写小数点,比如 `10.0268` 或者 `1:23:45.678` 。 181 | 182 | 183 | ### -ss 184 | 185 | 这个选项用来指定输出文件相对于输入文件的开始时间,比如我想把 `full.mp4` 这个视频的前 30 秒剪掉并把剩下的保存为 `segment.mp4` ,就可以使用这个命令: 186 | 187 | ffmpeg -i full.mp4 -c copy -ss 30 segment.mp4 188 | 189 | `-ss` 选项也可以跟 `-t` 选项配合使用以截取媒体文件的一部分,比如如果我想截取 `full.mp4` 的 12 分 25 秒至 20 分 27 秒,保存为 `segment.mp4` ,使用这条命令。 190 | 191 | ffmpeg -i full.mp4 -c copy -ss 12:25 -t 8:02 segment.mp4 192 | 193 | - **注意:** `-t` 选项后面的参数并不是结束时间而是持续时间,所以在上面的例子中我写的不是 `20:27` 而是 20:27-12:25 的结果, `8:02` 。 194 | 195 | 196 | ### -metadata 197 | 198 | 这个选项说的很明白了,它所更改的就是输出文件的[元数据](https://zh.wikipedia.org/wiki/%E5%85%83%E6%95%B0%E6%8D%AE),比如一首歌的标题、艺术家、专辑。比如 `-metadata title="我是标题"` 就会把输出文件的标题元数据改为 `我是标题` 。 `-metadata` 选项可以多次指定以更改多个元数据。 199 | 200 | 举个例子,把 `no metadata.mp3` 的标题改成 `一首歌` ,艺术家改成 `一位艺术家` ,专辑改成 `一张专辑` ,然后保存为 `with metadata.mp3` : 201 | 202 | ffmpeg -i "no metadata.mp3" -c copy -metadata title="一首歌" -metadata artist="一位艺术家" -metadata album="一张专辑" "with metadata.mp3" 203 | 204 | - **提示:** 还记得吗?在文件名包含空格或其他特殊字符的时候,必须用半角双引号包起来。 205 | 206 | --------------------------- 207 | 208 | > 我在命令中所写的选项和参数,顺序有关系吗? 209 | 210 | 这样的问题可能发自一个对命令行不熟悉的用户。那么我就来解答一下:在 FFmpeg 中,以我们目前的知识,除了 `-i 输入文件名` 必须在仅次于 `ffmpeg` 的最前面以及输出文件名必须在最后面以外,选项的顺序没有关系。 211 | 212 | 也就是说,以下两条命令是没有任何区别的。 213 | 214 | ffmpeg -i input.mp4 -c:v vp9 -c:a opus -s 1280x720 -ss 10 -t 1:00 output.webm 215 | ffmpeg -i input.mp4 -t 1:00 -c:v vp9 -s 1280x720 -c:a opus -ss 10 output.webm 216 | 217 | 但是注意了,一个选项跟这个选项的的参数是不可分开的,也就是说,音频编码必须跟在 `-c:a` 后面,持续时间必须跟在 `-t` 后面……下面这条命令是 **错误** 且 **无法** 被正常运行的: 218 | 219 | ffmpeg -i input -c:v -ss -t 10 -ss -c:a 1280x720 vp9 1:00 opus -s output.webm 220 | 221 | 看完了这些说明,我想你就应该能够熟练地掌握命令行的规则了。而在接下来的[下一章](07-subtitles.md)中,我们将会介绍有关字幕的东西。 222 | -------------------------------------------------------------------------------- /07-subtitles.md: -------------------------------------------------------------------------------- 1 | # 字幕 2 | 3 | 在观看非母语语言的视频的时候,[字幕](https://zh.wikipedia.org/wiki/%E5%AD%97%E5%B9%95)是非常有用的东西。我们在网上能找到许多带字幕的视频,这些字幕很多时候已经跟视频融为一体了。不过有时候,我们还是能找到单独的字幕文件,这些字幕文件通常以 `.srt` 或 `.ass` 作为后缀名,且能被文本编辑器打开。 4 | 5 | 要让这些字幕在播放视频的时候显示在屏幕上,我们得让播放器除了打开视频文件以外,还得加载对应的字幕文件。许多播放器也会自动加载与视频文件名相似的字幕文件或是从网上寻找字幕,不过那是另外一回事了,每个播放器也不一样。 6 | 7 | 我在这里还是拿第 5 章中所提到的 [Tor 介绍视频](https://blog.torproject.org/blog/releasing-tor-animation)来做例子,我已经下载了 [English HQ](https://media.torproject.org/video/2015-03-animation/HQ/Tor_Animation_en.mp4) 版本的视频,现在我还要下载 [Chinese (China)](https://media.torproject.org/video/2015-03-animation/subs/Tor_animation.zh-CN.srt) 版本的字幕。当然,你没有必要用我使用的例子做试验。 8 | 9 | 把视频和字幕放在一个目录中,打开视频,然后在播放器中加载字幕文件,就可以看到字幕在播放视频的时候出现在屏幕上了。 10 | 11 | 但是这样做很麻烦,也很容易在拷贝或下载时忘记。所以,使用 FFmpeg 将字幕放到视频里吧! 12 | 13 | 14 | ## 作为字幕流 15 | 16 | 如[第 4 章](04-media-file-structure.md)中所说,媒体文件是由多个 **媒体流** 组成的,我们已经对视频流和音频流很熟悉了,但在这一节,我们将认识另一种媒体流——字幕流。 **字幕流独立于视频流** ,所以它可以随时开关,就像声音可以被静音一样。 17 | 18 | 字幕流跟其他媒体流一样,有各种编码,常见的编码有 [SubRip](https://en.wikipedia.org/wiki/SubRip) (作为独立文件时后缀名为 `.srt` )和 [ASS](https://zh.wikipedia.org/wiki/SubStation_Alpha#Advanced_SubStation_Alpha.EF.BC.88ASS.E5.AD.97.E5.B9.95.EF.BC.89) (作为独立文件时后缀名为 `.ass` )。运行 `ffmpeg -codecs` 可以查看 FFmpeg 支持的所有编码,而其中第 3 列为 `S` 的就是字幕编码。 19 | 20 | 字幕文件其实也能够算是媒体文件,只不过它不包含视频流和音频流,而是只包含了字幕流。 21 | 22 | 所以,如果我把只有视频流和音频流的媒体文件 `Tor_Animation_en.mp4` 与只有字幕流的媒体文件 `Tor_animation.zh-CN.srt` 合并到一起,就会得到一个有视频流、音频流和字幕流的媒体文件了。 23 | 24 | 在 FFmpeg 中,这样做很简单,只要把要合并的两个文件都作为输入文件就可以了,也就是在 `ffmpeg` 后面写上 `-i Tor_Animation_en.mp4 -i Tor_animation.zh-CN.srt` 。 FFmpeg 会将两个输入文件中的媒体流放都到其输出文件中去。 25 | 26 | 但是并不是所有的封装格式都支持字幕流,比如常见的 MP4 格式就不支持。要查看一个封装格式是否支持字幕流,请运行 `ffmpeg -help muxer=封装格式` ,如果输出中有 `Default subtitle codec:` 这样一行,即代表此封装格式支持字幕流。如果输出文件的封装格式不支持字幕流, FFmpeg 会忽略掉它或者报错。 27 | 28 | - **提示:** 顺带一提, Matroska 和 WebM 封装格式都是支持字幕流的。 29 | 30 | 在[第 5 章第 4 节](05-start-converting.md#learn-to-look-output)中,我们知道了可以在 FFmpeg 开始转码时查看媒体流的分布( Stream mapping ),我在执行 `ffmpeg -i Tor_Animation_en.mp4 -i Tor_animation.zh-CN.srt Tor_Animation_subtitled.mkv` 的时候,就会看到这样的输出: 31 | 32 | Stream mapping: 33 | Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264)) 34 | Stream #0:1 -> #0:1 (aac (native) -> vorbis (libvorbis)) 35 | Stream #1:0 -> #0:2 (subrip (srt) -> ass (native)) 36 | 37 | 它很直观地告诉我,在本次操作中, 0 号输入文件(也就是 `Tor_Animation_en.mp4` )中的 0 号媒体流变成了 0 号输出文件的 0 号媒体流, 0 号输入文件中的 1 号媒体流变成了 0 号输出文件的 1 号媒体流, 1 号输入文件(也就是 `Tor_animation.zh-CN.srt` )的 0 号媒体流变成了 0 号输出文件的 2 号媒体流。编码的转换也被清晰地显示了出来。 38 | 39 | 因为字幕流也是媒体流,也有各种编码,所以,我们也可以通过 `-scodec` 或 `-c:s` 选项来指定字幕流的编码。这个例子可以让 FFmpeg 复制视频流和音频流(不用重新编码,加快了速度),而将字幕流转换为 `ass` 编码: 40 | 41 | ffmpeg -i Tor_Animation_en.mp4 -i Tor_animation.zh-CN.srt -c:v copy -c:a copy -c:s ass Tor_Animation_subtitled.mkv 42 | 43 | 同样的, `-c` 选项除了会影响到视频流和音频流以外,也会影响到字幕流,也就是说,指定 `-c copy` 也会让 FFmpeg 不对字幕流进行重新编码。 44 | 45 | 你甚至可以将字幕文件作为单独的输入文件!也就是对字幕文件进行转码,比如 `ffmpeg -i Tor_animation.zh-CN.srt Tor_animation.zh-CN.ass` 就会将 SubRip 字幕转换为 ASS 字幕。(因为 ASS 封装格式的默认字幕编码就是 `ass` ,所以你在这条命令中不用写 `-c:s ass` ) 46 | 47 | 48 | ## 编入视频流 49 | 50 | 虽然字幕流是一个很方便的东西,但是由于一些差的播放器不支持字幕流,字幕流也的显示也取决于播放器所使用的字体,所以在很多时候,我们需要将字幕放到视频内容中,而不是独立于视频流的字幕流。 51 | 52 | - **注意:** 因为这样做会改写视频流,所以原本视频中被字幕覆盖的地方就无法再被显示出来了。 53 | 54 | 在开始之前我们先要介绍一下 `-vf` 选项,在 `ffmpeg -help` 中,它是这样被说明的: 55 | 56 | -vf filter_graph set video filters 57 | 58 | 这个选项被用来给视频添加滤镜,就像图像处理软件的滤镜一样,有颠倒、放大、改变颜色、模糊之类的东西。用 `ffmpeg -filters` 可以查看 FFmpeg 支持的滤镜。 59 | 60 | 我们要在视频流上面加上字幕,就得使用一个叫做 `subtitles` 的滤镜,要使用这个滤镜,在命令中写上 `-vf subtitles=字幕文件名` ,还是要注意,如果文件名包含空格或其他特殊字符,得用半角引号包起来: `-vf subtitles="字幕 文件名"` 。 61 | 62 | 那么我想将 `Tor_animation.zh-CN.srt` 中的字幕写入 `Tor_Animation_en.mp4` 的视频流中,就使用这条命令: 63 | 64 | ffmpeg -i Tor_Animation_en.mp4 -vf subtitles=Tor_animation.zh-CN.srt -c:a copy Tor_Animation_subtitled.mp4 65 | 66 | - **提示:** 因为这个操作改写了视频流,所以视频流必须得重新编码。在上面这个例子中,我没有使用 `-c:v` 选项, FFmpeg 则会使用 MP4 封装格式的默认视频编码 `h264` 。 67 | 68 | - **注意:** 如果你使用的是~~遭天谴~~的 Microsoft® Windows® , FFmpeg 会在写视频流的时候无法找到字体,而导致输出文件中没有任何字幕,要解决这个问题,请参阅[这篇文章](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/blob/master/etc/fontconfig-windows/README.md)。 69 | 70 | 接下来,你就可以享受在视频中看到字幕的便捷了。 71 | ![SMPlayer 正在播放带字幕的视频](image/video-with-subtitles.png) 72 | 73 | -------------------- 74 | 75 | 学会了如何添加字幕之后,我们再来看看[不同编码器特有的设定](08-differente-encoders-special-options.md)吧。 76 | -------------------------------------------------------------------------------- /08-differente-encoders-special-options.md: -------------------------------------------------------------------------------- 1 | # 不同编码器特有的设定 2 | 3 | FFmpeg 支持很多种媒体编码器,这些媒体编码器当然都不会是千篇一律的,有些编码器有着十分丰富的选项,这些选项可以让你调整编码时的压缩比、刷新率、色域等等。你也可以通过调整一些参数来让转码快一些,或者体积更小。那么在这一章,我们就来介绍几个常见的编码器的一些特别选项。 4 | 5 | *部分功能可能仅在 GNU/Linux 上有效。* 6 | 7 | 8 | ## H264 和 HEVC 9 | 10 | 毫无疑问, H264 是目前最流行的视频编码了,现在我们在网上见到的几乎所有视频都是使用 H264 进行视频编码的,不管它的封装格式是 MP4 、 Matroska 还是 FLV 。而它为什么会变得如此流行呢?除了较高的效率,丰富的编码器选项也是它的优点之一。 11 | 12 | HEVC 是 H264 的后继版本,又称 H265 ,它提供了一个更高的压缩比,不过目前有许多差的播放器不支持这种编码,所以请小心使用。 13 | 14 | 15 | ### 恒流量系数 (CRF) 16 | 17 | 这种方式可以让编码器尽量保持一定的画面质量,而文件的大小就不是那么重要了,这种方式为单通道的方式提供了最大的压缩比。每一帧都会得到能保证其质量等级的比特率。它的坏处是你无法确认最终会得到的具体文件大小或保证它不超过一定的尺寸。 18 | 19 | 首先你需要选择一个 CRF 值,这个值在 0 至 51 之间。 0 就代表无损, 23 是默认的, 51 就是最差的画质了。 **数字越小画质越高** ,不过通常我们选 18 至 28 之间就可以了,就当作 18 是“近乎无损”吧,如果输入文件不是无损的话,通常 18 就跟原来是几乎没区别的了。 20 | 21 | CRF 值是倍数,也就是说把 CRF 增大 6 的话,文件就会变成原来的几乎一半大小,减小 6 的话就会变成两倍。通常选择 CRF 值的方式是:选择一个最大的能提供看起来还可以的值,如果出来的效果还不错,就再增大一些试试,如果看起来画质太差了就减小看看。 22 | 23 | 接下来你要选择一个预设,预设会影响编码的速度和压缩比。慢的预设会给出一个较好的压缩比(压缩比就是画面质量相对于文件大小的比例),也就是说在文件大小一样的情况下,越慢的预设画质越好,或者说在画质一样的情况下,越慢的预设文件更小。 24 | 25 | 通常选定预设的方法是使用你能忍受的最慢的预设。目前的这些预设从快到慢排列有: `ultrafast`, `superfast`, `veryfast`, `faster`, `fast`, `medium`, `slow`, `slower`, `veryslow`, `placebo` 。默认是 `medium` , `placebo` 通常没有用所以请无视它。 26 | 27 | 决定好了 CRF 值和预设之后,你就可以使用 `-crf` 选项来指定 CRF 值, `-preset` 选项来指定预设了。下面的例子是使用 `22` 作为 CRF 值, `slow` 作为预设来进行转码: 28 | 29 | ffmpeg -i input.mp4 -c:v h264 -crf 22 -preset slow -c:a copy output.mkv 30 | 31 | 32 | ### 指定平均比特率 33 | 34 | 使用 `-b:v` 选项可以让 FFmpeg 为 H264 或 HEVC 编码的视频指定一个平均的比特率,这样就可以控制最终的文件大小了。比如要让平均比特率为 1000 KBit/s ,就写上 `-b:v 1000k` 。 35 | 36 | 不过这个选项不能与 `-crf` 选项同时使用,因为实际上指定 CRF 值就是让编码器自动评判出一个平均的码率。 37 | 38 | 39 | ## VP9 40 | 41 | [VP9](https://zh.wikipedia.org/wiki/VP9) 是 Google 为 WebM 格式所开发出的一种视频编码,专门为在线串流视频设计。全世界最大的视频网站 [YouTube](https://www.youtube.com) 默认使用的视频编码就是 VP9 。同样的,它也拥有许多选项来使编码变得更有效。 42 | 43 | 44 | ### 平均比特率 45 | 46 | 与 H264 和 HEVC 一样, VP9 默认使用的是动态比特率,同样我们也可以用 `-b:v` 选项来指定其平均的比特率,比如我想让视频的平均比特率为 1 MBit/s ,就使用这条命令: 47 | 48 | ffmpeg -i input.mp4 -c:v vp9 -b:v 1m output.webm 49 | 50 | 51 | ### 稳定画质 52 | 53 | VP9 编码也提供了一种方式让视频保证一定画质,与 H264 和 HEVC 一样使用的是 `-crf` 参数,不过与 H264 和 HEVC 不同的是, VP9 的 CRF 值范围是 0-63 。 54 | 55 | 还有一点要格外注意,要让 VP9 编码器保持稳定画质的时候,必须指定 `-b:v 0` ,不要忘了。也就是说,要让转码时使用 VP9 编码并以 `10` 作为 CRF 值,使用这条命令: 56 | 57 | ffmpeg -i input.mp4 -c:v vp9 -crf 10 -b:v 0 output.webm 58 | 59 | 60 | ## 学会看文档 61 | 62 | 实际上, H264 、 HEVC 以及 VP9 还有许多令人惊奇的功能,不过这些功能对于大多数人而言实用性并不强,所以我只挑了几个最容易理解且常用的功能写在了这一章里。同样的,除了 H264 、 HEVC 和 VP9 以外,也有许多编码器有各种各样的功能。 63 | 64 | 在目录中我就说过,本书只能使你成为 FFmpeg 用户而非专家。那些高级的功能比较难以理解,也不常用到,所以我不会介绍很多没有多大用处的功能。不过,如果有某项我没介绍的功能确实对你来说很有用呢? 65 | 66 | 叫我在教程中加上对其的介绍吗?这显然不是一个好主意,除非我认为这个功能真的对许多人来说都很有用,我是不会添加一些晦涩难懂的内容的。当然,我也不是专家,你所需要的功能也许我也不知道该怎么用。所以,这时最好的解决方法有两个: 67 | 68 | 1. 查阅官方文档 69 | 2. Google 70 | 71 | FFmpeg 的[官方文档](https://ffmpeg.org/documentation.html)是最权威不过的了,里面的信息非常的多,除了我在这个教程中所使用的 `ffmpeg` 转码工具以外,还有许多别的东西。在页面中点 "Command Line Tools Documentation"下面的 "ffmpeg" 链接就可以看到对于 `ffmpeg` 转码工具的文档。除此之外, FFmpeg 还有一个[官方 Wiki](https://trac.ffmpeg.org/wiki) ,里面都是一些网友贡献的内容,所以有些内容可能比较过时了,不过还是很有参考价值的。 72 | 73 | 以上的东西全部都是英文的,所以也[没办法](03-execute.md#chinese-is-most-beautiful-but)。要是有中文的文档,我也就不会写这个教程了。 74 | 75 | 还有一个十分高效的方法是使用 [Google](https://www.google.com) 来搜索,你可以将你的关键词写成 `ffmpeg blablabla` ,最好用英文来搜索。按下回车后也许你就能找到你想要的内容了。至于为什么是 Google 而非百度,这不是[不言自明](http://www.zhihu.com/question/20140749)的嘛。 76 | 77 | ----------------------- 78 | 79 | 读到这里,我想你就能很舒服地玩转 FFmpeg 了,而其他的转码软件再也不那么重要了。后面我还要写一些 FFmpeg 能做的许多其他事情,到那时候基本上许多其他转码软件都可以见鬼去了。不过它依然还在编写中,如果想要获知最新的更新,请关注这个计划的 [GitHub Repository](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh) 。 80 | 81 | 对了,也看一看[最后一章](10-write-in-end.md)。 82 | -------------------------------------------------------------------------------- /10-write-in-end.md: -------------------------------------------------------------------------------- 1 | # 写在最后 2 | 3 | 感谢大家阅读完《如何使用 FFmpeg 进行视频转码》的 0.2 版。这次的更新相比 v0.1 来说,增加了许多内容,修正了一些错误。也离本教程的完全完工不远了,实际上,现在的状态足以被当作完成,不过我还想写更多的内容,所以还不能把版本号写成 1.0 。 4 | 5 | 我的教程在 v0.1 发布之后,成就甚好,得到了 17 颗星,这对于我这样的无名小卒来说,真是太令人高兴了。高兴了,我就有动力继续写下去了。 6 | 7 | 这个教程的[第 7 章](07-subtitles.md)是一个特别的章节,为什么这么说呢?因为这一章的写成离不开 [Jack](https://plus.google.com/u/0/102878127136820749724/posts) 的帮助,是他帮我写成了该章的草稿,也为我提供了在 Windows® 上的~~高清~~截图。 8 | 9 | 如果大家有遇到任何困难或不理解的地方,想给我提意见或建议,我将非常感谢!跟随[这个指导](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/blob/master/etc/feedback/README.md)来反馈问题吧。 10 | 11 | 顺便宣传一下我自己的博客, [FiveYellowMice's Blog](https://fiveyellowmice.github.io),这里说实话东西不多,不过我还是希望你常来看一看。 12 | 13 | 那么还请大家多多支持、指教了!祝大家生活愉快! 14 | 15 | FiveYellowMice 16 | Sun, 11 Oct 2015 06:29:09 +0000 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-ShareAlike 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-ShareAlike 4.0 International Public 58 | License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-ShareAlike 4.0 International Public License ("Public 63 | License"). To the extent this Public License may be interpreted as a 64 | contract, You are granted the Licensed Rights in consideration of Your 65 | acceptance of these terms and conditions, and the Licensor grants You 66 | such rights in consideration of benefits the Licensor receives from 67 | making the Licensed Material available under these terms and 68 | conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. BY-SA Compatible License means a license listed at 88 | creativecommons.org/compatiblelicenses, approved by Creative 89 | Commons as essentially the equivalent of this Public License. 90 | 91 | d. Copyright and Similar Rights means copyright and/or similar rights 92 | closely related to copyright including, without limitation, 93 | performance, broadcast, sound recording, and Sui Generis Database 94 | Rights, without regard to how the rights are labeled or 95 | categorized. For purposes of this Public License, the rights 96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 97 | Rights. 98 | 99 | e. Effective Technological Measures means those measures that, in the 100 | absence of proper authority, may not be circumvented under laws 101 | fulfilling obligations under Article 11 of the WIPO Copyright 102 | Treaty adopted on December 20, 1996, and/or similar international 103 | agreements. 104 | 105 | f. Exceptions and Limitations means fair use, fair dealing, and/or 106 | any other exception or limitation to Copyright and Similar Rights 107 | that applies to Your use of the Licensed Material. 108 | 109 | g. License Elements means the license attributes listed in the name 110 | of a Creative Commons Public License. The License Elements of this 111 | Public License are Attribution and ShareAlike. 112 | 113 | h. Licensed Material means the artistic or literary work, database, 114 | or other material to which the Licensor applied this Public 115 | License. 116 | 117 | i. Licensed Rights means the rights granted to You subject to the 118 | terms and conditions of this Public License, which are limited to 119 | all Copyright and Similar Rights that apply to Your use of the 120 | Licensed Material and that the Licensor has authority to license. 121 | 122 | j. Licensor means the individual(s) or entity(ies) granting rights 123 | under this Public License. 124 | 125 | k. Share means to provide material to the public by any means or 126 | process that requires permission under the Licensed Rights, such 127 | as reproduction, public display, public performance, distribution, 128 | dissemination, communication, or importation, and to make material 129 | available to the public including in ways that members of the 130 | public may access the material from a place and at a time 131 | individually chosen by them. 132 | 133 | l. Sui Generis Database Rights means rights other than copyright 134 | resulting from Directive 96/9/EC of the European Parliament and of 135 | the Council of 11 March 1996 on the legal protection of databases, 136 | as amended and/or succeeded, as well as other essentially 137 | equivalent rights anywhere in the world. 138 | 139 | m. You means the individual or entity exercising the Licensed Rights 140 | under this Public License. Your has a corresponding meaning. 141 | 142 | 143 | Section 2 -- Scope. 144 | 145 | a. License grant. 146 | 147 | 1. Subject to the terms and conditions of this Public License, 148 | the Licensor hereby grants You a worldwide, royalty-free, 149 | non-sublicensable, non-exclusive, irrevocable license to 150 | exercise the Licensed Rights in the Licensed Material to: 151 | 152 | a. reproduce and Share the Licensed Material, in whole or 153 | in part; and 154 | 155 | b. produce, reproduce, and Share Adapted Material. 156 | 157 | 2. Exceptions and Limitations. For the avoidance of doubt, where 158 | Exceptions and Limitations apply to Your use, this Public 159 | License does not apply, and You do not need to comply with 160 | its terms and conditions. 161 | 162 | 3. Term. The term of this Public License is specified in Section 163 | 6(a). 164 | 165 | 4. Media and formats; technical modifications allowed. The 166 | Licensor authorizes You to exercise the Licensed Rights in 167 | all media and formats whether now known or hereafter created, 168 | and to make technical modifications necessary to do so. The 169 | Licensor waives and/or agrees not to assert any right or 170 | authority to forbid You from making technical modifications 171 | necessary to exercise the Licensed Rights, including 172 | technical modifications necessary to circumvent Effective 173 | Technological Measures. For purposes of this Public License, 174 | simply making modifications authorized by this Section 2(a) 175 | (4) never produces Adapted Material. 176 | 177 | 5. Downstream recipients. 178 | 179 | a. Offer from the Licensor -- Licensed Material. Every 180 | recipient of the Licensed Material automatically 181 | receives an offer from the Licensor to exercise the 182 | Licensed Rights under the terms and conditions of this 183 | Public License. 184 | 185 | b. Additional offer from the Licensor -- Adapted Material. 186 | Every recipient of Adapted Material from You 187 | automatically receives an offer from the Licensor to 188 | exercise the Licensed Rights in the Adapted Material 189 | under the conditions of the Adapter's License You apply. 190 | 191 | c. No downstream restrictions. You may not offer or impose 192 | any additional or different terms or conditions on, or 193 | apply any Effective Technological Measures to, the 194 | Licensed Material if doing so restricts exercise of the 195 | Licensed Rights by any recipient of the Licensed 196 | Material. 197 | 198 | 6. No endorsement. Nothing in this Public License constitutes or 199 | may be construed as permission to assert or imply that You 200 | are, or that Your use of the Licensed Material is, connected 201 | with, or sponsored, endorsed, or granted official status by, 202 | the Licensor or others designated to receive attribution as 203 | provided in Section 3(a)(1)(A)(i). 204 | 205 | b. Other rights. 206 | 207 | 1. Moral rights, such as the right of integrity, are not 208 | licensed under this Public License, nor are publicity, 209 | privacy, and/or other similar personality rights; however, to 210 | the extent possible, the Licensor waives and/or agrees not to 211 | assert any such rights held by the Licensor to the limited 212 | extent necessary to allow You to exercise the Licensed 213 | Rights, but not otherwise. 214 | 215 | 2. Patent and trademark rights are not licensed under this 216 | Public License. 217 | 218 | 3. To the extent possible, the Licensor waives any right to 219 | collect royalties from You for the exercise of the Licensed 220 | Rights, whether directly or through a collecting society 221 | under any voluntary or waivable statutory or compulsory 222 | licensing scheme. In all other cases the Licensor expressly 223 | reserves any right to collect such royalties. 224 | 225 | 226 | Section 3 -- License Conditions. 227 | 228 | Your exercise of the Licensed Rights is expressly made subject to the 229 | following conditions. 230 | 231 | a. Attribution. 232 | 233 | 1. If You Share the Licensed Material (including in modified 234 | form), You must: 235 | 236 | a. retain the following if it is supplied by the Licensor 237 | with the Licensed Material: 238 | 239 | i. identification of the creator(s) of the Licensed 240 | Material and any others designated to receive 241 | attribution, in any reasonable manner requested by 242 | the Licensor (including by pseudonym if 243 | designated); 244 | 245 | ii. a copyright notice; 246 | 247 | iii. a notice that refers to this Public License; 248 | 249 | iv. a notice that refers to the disclaimer of 250 | warranties; 251 | 252 | v. a URI or hyperlink to the Licensed Material to the 253 | extent reasonably practicable; 254 | 255 | b. indicate if You modified the Licensed Material and 256 | retain an indication of any previous modifications; and 257 | 258 | c. indicate the Licensed Material is licensed under this 259 | Public License, and include the text of, or the URI or 260 | hyperlink to, this Public License. 261 | 262 | 2. You may satisfy the conditions in Section 3(a)(1) in any 263 | reasonable manner based on the medium, means, and context in 264 | which You Share the Licensed Material. For example, it may be 265 | reasonable to satisfy the conditions by providing a URI or 266 | hyperlink to a resource that includes the required 267 | information. 268 | 269 | 3. If requested by the Licensor, You must remove any of the 270 | information required by Section 3(a)(1)(A) to the extent 271 | reasonably practicable. 272 | 273 | b. ShareAlike. 274 | 275 | In addition to the conditions in Section 3(a), if You Share 276 | Adapted Material You produce, the following conditions also apply. 277 | 278 | 1. The Adapter's License You apply must be a Creative Commons 279 | license with the same License Elements, this version or 280 | later, or a BY-SA Compatible License. 281 | 282 | 2. You must include the text of, or the URI or hyperlink to, the 283 | Adapter's License You apply. You may satisfy this condition 284 | in any reasonable manner based on the medium, means, and 285 | context in which You Share Adapted Material. 286 | 287 | 3. You may not offer or impose any additional or different terms 288 | or conditions on, or apply any Effective Technological 289 | Measures to, Adapted Material that restrict exercise of the 290 | rights granted under the Adapter's License You apply. 291 | 292 | 293 | Section 4 -- Sui Generis Database Rights. 294 | 295 | Where the Licensed Rights include Sui Generis Database Rights that 296 | apply to Your use of the Licensed Material: 297 | 298 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 299 | to extract, reuse, reproduce, and Share all or a substantial 300 | portion of the contents of the database; 301 | 302 | b. if You include all or a substantial portion of the database 303 | contents in a database in which You have Sui Generis Database 304 | Rights, then the database in which You have Sui Generis Database 305 | Rights (but not its individual contents) is Adapted Material, 306 | 307 | including for purposes of Section 3(b); and 308 | c. You must comply with the conditions in Section 3(a) if You Share 309 | all or a substantial portion of the contents of the database. 310 | 311 | For the avoidance of doubt, this Section 4 supplements and does not 312 | replace Your obligations under this Public License where the Licensed 313 | Rights include other Copyright and Similar Rights. 314 | 315 | 316 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 317 | 318 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 319 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 320 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 321 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 322 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 323 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 324 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 325 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 326 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 327 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 328 | 329 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 330 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 331 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 332 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 333 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 334 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 335 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 336 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 337 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 338 | 339 | c. The disclaimer of warranties and limitation of liability provided 340 | above shall be interpreted in a manner that, to the extent 341 | possible, most closely approximates an absolute disclaimer and 342 | waiver of all liability. 343 | 344 | 345 | Section 6 -- Term and Termination. 346 | 347 | a. This Public License applies for the term of the Copyright and 348 | Similar Rights licensed here. However, if You fail to comply with 349 | this Public License, then Your rights under this Public License 350 | terminate automatically. 351 | 352 | b. Where Your right to use the Licensed Material has terminated under 353 | Section 6(a), it reinstates: 354 | 355 | 1. automatically as of the date the violation is cured, provided 356 | it is cured within 30 days of Your discovery of the 357 | violation; or 358 | 359 | 2. upon express reinstatement by the Licensor. 360 | 361 | For the avoidance of doubt, this Section 6(b) does not affect any 362 | right the Licensor may have to seek remedies for Your violations 363 | of this Public License. 364 | 365 | c. For the avoidance of doubt, the Licensor may also offer the 366 | Licensed Material under separate terms or conditions or stop 367 | distributing the Licensed Material at any time; however, doing so 368 | will not terminate this Public License. 369 | 370 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 371 | License. 372 | 373 | 374 | Section 7 -- Other Terms and Conditions. 375 | 376 | a. The Licensor shall not be bound by any additional or different 377 | terms or conditions communicated by You unless expressly agreed. 378 | 379 | b. Any arrangements, understandings, or agreements regarding the 380 | Licensed Material not stated herein are separate from and 381 | independent of the terms and conditions of this Public License. 382 | 383 | 384 | Section 8 -- Interpretation. 385 | 386 | a. For the avoidance of doubt, this Public License does not, and 387 | shall not be interpreted to, reduce, limit, restrict, or impose 388 | conditions on any use of the Licensed Material that could lawfully 389 | be made without permission under this Public License. 390 | 391 | b. To the extent possible, if any provision of this Public License is 392 | deemed unenforceable, it shall be automatically reformed to the 393 | minimum extent necessary to make it enforceable. If the provision 394 | cannot be reformed, it shall be severed from this Public License 395 | without affecting the enforceability of the remaining terms and 396 | conditions. 397 | 398 | c. No term or condition of this Public License will be waived and no 399 | failure to comply consented to unless expressly agreed to by the 400 | Licensor. 401 | 402 | d. Nothing in this Public License constitutes or may be interpreted 403 | as a limitation upon, or waiver of, any privileges and immunities 404 | that apply to the Licensor or You, including from the legal 405 | processes of any jurisdiction or authority. 406 | 407 | 408 | ======================================================================= 409 | 410 | Creative Commons is not a party to its public licenses. 411 | Notwithstanding, Creative Commons may elect to apply one of its public 412 | licenses to material it publishes and in those instances will be 413 | considered the "Licensor." Except for the limited purpose of indicating 414 | that material is shared under a Creative Commons public license or as 415 | otherwise permitted by the Creative Commons policies published at 416 | creativecommons.org/policies, Creative Commons does not authorize the 417 | use of the trademark "Creative Commons" or any other trademark or logo 418 | of Creative Commons without its prior written consent including, 419 | without limitation, in connection with any unauthorized modifications 420 | to any of its public licenses or any other arrangements, 421 | understandings, or agreements concerning use of licensed material. For 422 | the avoidance of doubt, this paragraph does not form part of the public 423 | licenses. 424 | 425 | Creative Commons may be contacted at creativecommons.org. 426 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 如何使用 FFmpeg 进行视频转码 2 | 3 | FFmpeg 是一个使用广泛的开源多媒体编解码器,其一大用途就是进行音频或视频的编码及格式转换。 4 | 5 | FFmpeg 并不像它表面看上去那么高端,那么难。只是中文的资料实在太少,才造成了这样的表象。所以,我决定动手开启这个计划,为中文用户提供一个更简单易学的 FFmpeg 教程。 6 | 7 | 不过本教程只能让你成为 FFmpeg 用户,不能让你成为 FFmpeg 专家,专家的那些东西我也不明白。不过就大多数情况而言,你一辈子也用不到那些专家的东西。即使偶然用到, Google 足够作为你的伙伴了。 8 | 9 | ## 开始阅读 10 | 11 | **为了方便所有人编辑,此计划已经迁移到了[新的 Wiki 页面](https://wiki.fiveyellowmice.com/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8_FFmpeg_%E8%BF%9B%E8%A1%8C%E8%A7%86%E9%A2%91%E8%BD%AC%E7%A0%81:%E9%A6%96%E9%A1%B5),在 GitHub 上的内容已废弃,请访问上述链接来阅读。** 12 | 13 | 1. ~~[在线阅读](index.md)~~ 14 | ~~这样可以直接用浏览器进行阅读,但可能不是特别方便。~~ 15 | ~~在线阅读也能够读到最新的内容,因为我不会每做一次修改或增加一些内容就重新制作电子书。~~ 16 | 17 | 2. ~~[下载电子书](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/releases/latest)~~ 18 | ~~这样就可以随时随地阅读了,不过内容可能会[旧一些](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/compare/v0.2...master "点此查看电子书与在线阅读的区别")。~~ 19 | ~~有 HTML ( ZIP 压缩 ), ePUB, PDF, AZW3 这 4 种格式可供下载。~~ 20 | 21 | ## 支持 22 | 23 | 如果你有意见或建议,请按照[这些步骤](etc/feedback/README.md)来提交问题。 24 | 25 | 如果你觉得我值得鼓励,点一下右上方的 Star 吧! 26 | 27 | ---------------------- 28 | 29 | ![CC-BY-SA](image/by-sa.png) 30 | 本作品采用知识共享 署名-相同方式共享 4.0 国际 许可协议进行许可,你可以在署名并采用相同条款的情况下自由复制、传播、再演绎本作品而无需通知作者。访问 查看该许可协议。 31 | -------------------------------------------------------------------------------- /etc/feedback/README.md: -------------------------------------------------------------------------------- 1 | # 反馈问题的步骤 2 | 3 | 你在阅读本教程的时候发现了问题吗?那欢迎将这个问题反馈给我! 4 | 5 | 这个问题可以是语法错误、错别字,也可以是不清晰的表述,也可以是内容的错误、缺失,希望我加上一些内容。无论何种问题,都能代表你对我这个计划的支持。 6 | 7 | 在这里,我将覆盖一些基本的反馈问题的步骤。 8 | 9 | ## 这个问题真的应该反馈给我吗? 10 | 11 | 在反馈问题之前,请你想一下,这个问题真的应该反馈给我吗? 12 | 13 | 虽然说所有类型的反馈都能代表对我的支持,不过不合理的反馈有可能会给我带来困扰。 14 | 15 | 为了避免复杂的说明,我举几个例子吧。 16 | 17 | 1. 假如你发现了一个错别字或者语法错误,但不确定这个字是否是真的错了,那就查一下字典吧。中华文字博大精深,有些看似错字其实是对的,有些看似正确实际上是错的。 18 | 19 | 2. 如果你发现教程中的一些步骤无法被执行,请先上网搜索,这是否是一些其他的原因导致的问题。如果是的话,那就不怪我了。 20 | 21 | 3. 如果你想知道更高深的内容,那或许反馈问题让我加上不是一个好主意,关于这点,可以看看第 8 章中的[学会看文档](08-differente-encoders-special-options.md#learn-to-look-document)一节。 22 | 23 | 4. 如果你身处中国大陆,并发现这个教程中有一些外部超链接无法打开,请不要立即将其认为是我的失误,请[检查你的互联网连接](https://fiveyellowmice.github.io/onscripter-tutorial/cant-open.html)。在解决连接问题后,再看看是否打得开。 24 | 25 | 请只有你在确认这个问题是适合反馈给我的时候,再将问题反馈给我,谢谢大家的理解。 26 | 27 | ## 何处反馈? 28 | 29 | 我这个计划的全部内容都托管在了 [GitHub](https://github.com) 上,这是目前世界上最大的开源代码托管站(虽然我的教程应该算不上代码),你也许不知道这个网站,这很正常,因为其名声基本只在程序员之间…… 30 | 31 | GitHub 提供了一套非常良好的 Issues 系统,用来追踪 bug 之类的东西,那对我而言就是让大家来反馈教程中问题的地方。这套系统非常好用,因为它就是被设计成用来反馈问题的。 32 | 33 | 不过你有可能不知道这个东西,或者比较懒,于是就打算在我的帖子(不论是贴吧还是 G+ )或者博客的评论中反馈问题。嘛,这样可以是可以的,我也不好因此对你不满,但是评论系统不是设计用来管理问题的,所以用它来反馈不是一个非常好的事情。最好的反馈平台还是 GitHub 的 Issues 。 34 | 35 | ## 要登录才能反馈? 36 | 37 | 要反馈问题,首先得有一个 GitHub 帐号,这也许对于不少人来说是个不小的门槛,不过我也没有什么办法,只好劳烦没有帐号的你注册一个帐号了。 38 | 39 | 你注册了帐号我不会得到任何利润,因为 GitHub 也不是我开的。 GitHub 是一个大公司,还十分[有骨气](https://program-think.blogspot.com/2015/03/weekly-share-82.html?q=github&scope=all),所以你可以放心的注册一个帐号。 40 | 41 | 要注册,打开 GitHub 的主页: ,你能看见网页中间靠右的地方有 3 个白色的文本框和 1 个绿色的注册按钮,第一栏写一个用户名,第二栏写你的邮箱地址,第三栏写一个密码,再按按钮就可以了。接下来只要再做一些平常注册一个帐号所需要做的事情(比如验证邮箱),你就拥有一个 GitHub 帐号了。 42 | 43 | ## 已经有人反馈了吗? 44 | 45 | 打开我这个计划的 [GitHub Repository][repo] ,然后点网页右侧的 Issues ,就能进入相应的 Issues 页面了。 46 | ![右侧面板中的 Issues 按钮](repo-panel.png) 47 | 48 | 如果是手机,往下翻,翻到一个上面写着 Issues 的方框,点 View all issues 。 49 | 50 | 现在里面找找有没有与你相似的问题,如果有,点进去看一下吧。一个 Issue 就像一个帖子,你也可以回复这个 Issue ,来进行讨论。或者点右边的 Subcribe 按钮,以后你就会收到这个 Issue 以后的评论和动向了。 51 | 52 | ## 怎么起笔? 53 | 54 | 在 Issues 页面,点右上方绿色的 New issue 按钮,手机的话,点左上角的铅笔图标,就可以开始发新 Issue 了。 55 | 56 | 一个 Issue 就像一个帖子一样,所以你也没必要注意使用什么格式,只要在标题中简明扼要的概括一下问题,内容中详细说明一下就可以了。 57 | 58 | 比贴吧和 G+ 高级的是, Issue 的内容支持 [Markdown 语法](http://wowubuntu.com/markdown/),你可以用一个星号包围在文字的两侧来表示斜体,两个星号包围表示粗体。 59 | 60 | *我会变成斜体* 61 | **我会变成粗体** 62 | 63 | 还可以放链接和图片: 64 | 65 | [链接显示的文字](链接) 66 | ![图片的描述](图片的链接) 67 | 68 | 在电脑上,你可以把图片拖到浏览器窗口中来插图片,在手机上的话……就得靠 [Imgur](https://imgur.com) 这样的图片托管网站了。 69 | 70 | ## 然后呢? 71 | 72 | 然后我就会收到一条提醒:有人来给我反馈问题了。那么,我将感激不尽。 73 | 74 | [repo]: https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh 75 | -------------------------------------------------------------------------------- /etc/feedback/repo-panel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/feedback/repo-panel.png -------------------------------------------------------------------------------- /etc/fontconfig-windows/README.md: -------------------------------------------------------------------------------- 1 | # 在 Microsoft® Windows® 下使用 FFmpeg 将字幕编入视频流时的额外说明 2 | 3 | ## 废话 4 | 5 | 要将字幕从文本变成可以显示出来的文字,[字体](https://zh.wikipedia.org/wiki/%E5%AD%97%E4%BD%93)是必须的,播放器在显示独立字幕文件或字幕流的时候需要在电脑中找到合适的字体,才能将字幕渲染到屏幕上, FFmpeg 在将字幕编入视频流的时候也是如此。 6 | 7 | FFmpeg 在找字体的时候,会使用一个叫做 [Fontconfig](https://zh.wikipedia.org/wiki/Fontconfig) 的程序,这个程序是为[类 Unix](https://zh.wikipedia.org/wiki/%E7%B1%BBUnix%E7%B3%BB%E7%BB%9F) 系统设计的,所以在 GNU/Linux 以及 Mac OS X 上,它工作地很好。 8 | 9 | 但是, Microsoft® Windows® 没有实现大部分的 [POSIX](https://zh.wikipedia.org/wiki/POSIX) 接口,所以 Fontconfig 无法在它上面正常的工作,进而 FFmpeg 无法找到字体,将字幕编入视频流也是无从谈起。 10 | 11 | - **安利:** 虽然这篇文章将指导你解决 Fontconfig 在 Microsoft® Windows® 上工作不正常的问题,但是为了你的精神、财富和智慧,[切换到 GNU/Linux](https://program-think.blogspot.com/2013/10/linux-newbie-guide.html) 才是最佳的选择。 12 | 13 | ## fonts.conf 14 | 15 | `fonts.conf` 是 Fontconfig 用来存储其配置的文件,在 GNU/Linux 下它已经存在与系统中了,而在 Microsoft® Windows® 下没有。 16 | 17 | 因为没有,所以我们就得自己写一个,好在已经有现成的了。将[这个文件](fonts.conf)的内容复制并保存为文本文件,命名为 `fonts.conf` 。将这个文件放到 `C:\\Users\用户名\` 里,比如如果你的用户名叫 Alex ,就放到 `C:\\Users\Alex\` 里面。 18 | 19 | - **Windows XP :** 文件应该放到 `C:\\Documents and Settings\用户名\` 里面。 20 | 21 | ![把 font.conf 放到 C:\\Users\用户名\ 里](c-users-username.png) 22 | 23 | 接下来,在“计算机”( Windows 7 )或“这台电脑”( Windows 8 或更高版本)上右键,选择“属性”。 24 | 在出来的窗口中的左边,点击“高级系统设置”(图中红框部分)。 25 | 26 | - **Windows XP :** 在“我的电脑”上右键后将直接打开“系统属性”窗口。 27 | 28 | ![计算机属性](system.png) 29 | 30 | 在出来的“系统属性”窗口中,选择“高级”选项卡,再在其中点击“环境变量”按钮。 31 | 32 | ![点击环境变量](advanced-system-props.png) 33 | 34 | 在“环境变量”窗口中的上半部分(“ XX 的用户变量”),点“新建”按钮。 35 | 36 | ![环境变量](environ-var.png) 37 | 38 | 在“变量名”中,写 `FONTCONFIG_FILE` ,在“值”中写上 `fonts.conf` 文件的路径,也就是 `C:\\Users\用户名\fonts.conf` 。 39 | 40 | - **Windows XP :** 路径应该是 `C:\\Documents and Settings\用户名\fonts.conf` 。 41 | 42 | ![添加变量](new-var.png) 43 | 44 | 点“确定”,再点“确定”,再点“确定”。 45 | 这样就完成了。 46 | 47 | ## 后果 48 | 49 | 以后再执行 FFmpeg 的时候,会停顿一段时间,这段时间是 Fontconfig 在加载字体。时间长短因字体数量而异。 50 | 51 | 这段停顿的时间确实很令人不爽,但是没办法,谁叫 Windows 不支持 POSIX 接口呢。 52 | -------------------------------------------------------------------------------- /etc/fontconfig-windows/advanced-system-props.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/fontconfig-windows/advanced-system-props.png -------------------------------------------------------------------------------- /etc/fontconfig-windows/c-users-username.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/fontconfig-windows/c-users-username.png -------------------------------------------------------------------------------- /etc/fontconfig-windows/environ-var.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/fontconfig-windows/environ-var.png -------------------------------------------------------------------------------- /etc/fontconfig-windows/fonts.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | C:\WINDOWS\Fonts 5 | 6 | 7 | mono 8 | monospace 9 | 10 | 11 | 12 | sans-serif 13 | serif 14 | monospace 15 | sans-serif 16 | 17 | 18 | 19 | Times 20 | Times New Roman 21 | serif 22 | 23 | 24 | Helvetica 25 | Arial 26 | sans 27 | 28 | 29 | Courier 30 | Courier New 31 | monospace 32 | 33 | 34 | serif 35 | Times New Roman 36 | 37 | 38 | sans 39 | Arial 40 | 41 | 42 | monospace 43 | Andale Mono 44 | 45 | 46 | 47 | Courier New 48 | 49 | 50 | monospace 51 | 52 | 53 | 54 | 55 | Courier 56 | 57 | 58 | monospace 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /etc/fontconfig-windows/new-var.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/fontconfig-windows/new-var.png -------------------------------------------------------------------------------- /etc/fontconfig-windows/system.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/etc/fontconfig-windows/system.png -------------------------------------------------------------------------------- /image/by-sa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/by-sa.png -------------------------------------------------------------------------------- /image/cd-ls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/cd-ls.png -------------------------------------------------------------------------------- /image/files-in-explorer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/files-in-explorer.png -------------------------------------------------------------------------------- /image/successful-completed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/successful-completed.png -------------------------------------------------------------------------------- /image/video-with-subtitles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/video-with-subtitles.png -------------------------------------------------------------------------------- /image/windows-ff-prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh/980d83a7e900d30709e250a84d7df399b55b02c6/image/windows-ff-prompt.png -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | # 如何使用 FFmpeg 进行视频转码 2 | 3 | ![CC-BY-SA](image/by-sa.png) 4 | 本作品采用知识共享 署名-相同方式共享 4.0 国际 许可协议进行许可,你可以在署名并采用相同条款的情况下自由复制、传播、再演绎本作品而无需通知作者。访问 查看该许可协议。 5 | 6 | ## 欢迎 7 | 8 | FFmpeg 是一个使用广泛的开源多媒体编解码器,其一大用途就是进行音频或视频的编码及格式转换。 9 | 10 | FFmpeg 并不像它表面看上去那么高端,那么难。只是中文的资料实在太少,才造成了这样的表象。所以,我决定动手开启这个计划,为中文用户提供一个更简单易学的 FFmpeg 教程。 11 | 12 | 不过本教程只能让你成为 FFmpeg 用户,不能让你成为 FFmpeg 专家,专家的那些东西我也不明白。不过就大多数情况而言,你一辈子也用不到那些专家的东西。即使偶然用到, Google 足够作为你的伙伴了。 13 | 14 | 那么没有问题的话就[从第一章开始阅读](01-write-in-front.md)吧! 15 | 16 | ## 目录 17 | 18 | 没有链接的章节是在此版本中还未写完的部分,如果想要查看最新的开发版本,请访问这个计划的 [GitHub Repository](https://github.com/FiveYellowMice/how-to-convert-videos-with-ffmpeg-zh) 。 19 | 20 | 1. [写在前面](01-write-in-front.md) 21 | 1. [丑话说在前头](01-write-in-front.md#unpleasant-words-in-front) 22 | 2. [FFmpeg 是什么,我为什么要用它?](01-write-in-front.md#what-is-ffmpeg-why-use-it) 23 | 2. [下载与安装](02-download-and-install.md) 24 | 1. [GNU/Linux](02-download-and-install.md#gnu-linux) 25 | 2. [Mac OS X®](02-download-and-install.md#mac-os-x) 26 | 1. [第三方包管理器](02-download-and-install.md#third-party-package-manager) 27 | 2. [手动安装](02-download-and-install.md#manual-install) 28 | 3. [Microsoft® Windows®](02-download-and-install.md#microsoft-windows) 29 | 4. [手机、平板、树莓派……](02-download-and-install.md#phone-tablet-rasppi) 30 | 3. [运行](03-execute.md) 31 | 1. [是这个黑黑的窗口吗?](03-execute.md#this-black-window) 32 | 2. [查看帮助](03-execute.md#look-help) 33 | 3. [“选项”与“参数”](03-execute.md#options-and-arguments) 34 | 4. [支持哪些格式?](03-execute.md#what-formats-supported) 35 | 5. [中文是世界上最美的语言,但是……](03-execute.md#chinese-is-most-beautiful-but) 36 | 4. [媒体文件的结构](04-media-file-structure.md) 37 | 5. [开始转码](05-start-converting.md) 38 | 1. [简单的文件输入与输出](05-start-converting.md#simple-io) 39 | 2. [指定编码器](05-start-converting.md#specify-codec) 40 | 3. [照它说的做](05-start-converting.md#do-what-it-says) 41 | 4. [学会看输出](05-start-converting.md#learn-to-look-output) 42 | 1. [转换时](05-start-converting.md#during-converting) 43 | 2. [转换前](05-start-converting.md#before-converting) 44 | 6. [转码时能顺便一起做的事情](06-do-in-passing-while-converting.md) 45 | 1. [音频](06-do-in-passing-while-converting.md#audio) 46 | 1. [-ar](06-do-in-passing-while-converting.md#ar) 47 | 2. [-an](06-do-in-passing-while-converting.md#an) 48 | 3. [-vol](06-do-in-passing-while-converting.md#vol) 49 | 2. [视频](06-do-in-passing-while-converting.md#video) 50 | 1. [-r](06-do-in-passing-while-converting.md#r) 51 | 2. [-s](06-do-in-passing-while-converting.md#s) 52 | 3. [-vn](06-do-in-passing-while-converting.md#vn) 53 | 3. [整体](06-do-in-passing-while-converting.md#general) 54 | 1. [-c](06-do-in-passing-while-converting.md#c) 55 | 2. [-t](06-do-in-passing-while-converting.md#t) 56 | 3. [-ss](06-do-in-passing-while-converting.md#ss) 57 | 4. [-metadata](06-do-in-passing-while-converting.md#metadata) 58 | 7. [字幕](07-subtitles.md) 59 | 1. [作为字幕流](07-subtitles.md#as-subtitle-stream) 60 | 2. [编入视频流](07-subtitles.md#encode-to-video) 61 | 8. [不同编码器特有的设定](08-differente-encoders-special-options.md) 62 | 1. [H264 和 HEVC](08-differente-encoders-special-options.md#h264-and-hevc) 63 | 1. [衡流量系数 (CRF)](08-differente-encoders-special-options.md#constant-rate-factor) 64 | 2. [指定平均比特率](08-differente-encoders-special-options.md#specify-average-bitrate) 65 | 2. [VP9](08-differente-encoders-special-options.md#vp9) 66 | 1. [平均比特率](08-differente-encoders-special-options.md#average-bitrate) 67 | 2. [稳定画质](08-differente-encoders-special-options.md#constant-quality) 68 | 3. [学会看文档](08-differente-encoders-special-options.md#learn-to-look-document) 69 | 9. 不仅能转码 70 | 10. [写在最后](10-write-in-end.md) 71 | --------------------------------------------------------------------------------