<p>FFmpeg decodes and encodes video and audio streams.</p>

<h3>Contents</h3>
<ul>
<li><a href="#examples">Examples</a></li>
<li><a href="#help">Built-in help</a></li>
<li><a href="#hwaccel">Hardware acceleration</a></li>
<li><a href="#codecs">Codecs and where they are used</a>
<ul>
<li><a href="#codecs_discs">Optical disc data storage</a></li>
<li><a href="#codecs_internet">Internet</a></li>
</ul>
</li>
<li><a href="#links">External links</a></li>
</ul>

<h3 id="examples">Examples</h3>

<p>To encode a video using H.264 (which is the default for MP4),<br />
invoke <code>ffmpeg</code> with the following options:</p>
<pre><code class="language-bash">ffmpeg -i input.mp4 \
  -vf 'scale=1920:1080' -preset veryslow \
  -movflags faststart output.mp4
</code></pre>

<p>Encode a video using H.265 by specifying the codec used for video encoding:</p>
<pre><code class="language-bash">ffmpeg -i input.mp4 \
  -vf 'scale=3840:2160' -c:v libx265 \
  -movflags faststart output.mp4
</code></pre>

<p>To cut a video from time 00:00:12 to 00:00:32:</p>
<pre><code class="language-bash">ffmpeg -ss 00:00:12 -to 00:00:32 -i input.mp4 \
  -preset veryslow \
  -movflags faststart output.mp4
</code></pre>

<p>To extract an image at time 00:00:12:</p>
<pre><code class="language-bash">ffmpeg -ss 00:00:12 -i input.mp4 \
  -frames:v 1 -s 480x270 poster.jpg
</code></pre>

<p>To extract the first subtitle track:</p>
<pre><code class="language-bash">ffmpeg -i input.mp4 \
  -map 0:s:0 subtitles.srt
</code></pre>

<p>To just copy a video stream and an audio stream to another container:</p>
<pre><code class="language-bash">ffmpeg -i input.mkv \
  -c:v copy -c:a copy \
  -movflags faststart output.mp4
</code></pre>

<p>Two-pass encoding is recommended for targeting an output file size.<br />
To encode a 90-minute video to fit on a 700 MB CD-ROM:</p>
<pre><code class="language-bash">ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 1 \
  -an /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 933k -pass 2 \
  -ac 2 -c:a libfdk_aac -b:a 128k output.mp4
</code></pre>

<h3 id="help">Built-in help</h3>

<pre><code class="language-bash">ffmpeg -hide_banner -h
</code></pre>

<p>To show available codecs that were enabled during configuration:</p>
<pre><code class="language-bash">ffmpeg -codecs
</code></pre>

<p>To show options for the encoder <code>libx264</code>:</p>
<pre><code class="language-bash">ffmpeg -hide_banner -h encoder=libx264
</code></pre>

<h3 id="hwaccel">Hardware acceleration</h3>

<p>The graphics card can be used to accelerate video encoding.<br />
FFmpeg needs to be built with enabled hardware acceleration.</p>

<p>A shell script that builds DEB packages of FFmpeg with NVIDIA GPU acceleration<br />
enabled is found in the <a href="#files">files</a> section of this page.</p>

<p>The following command uses the NVIDIA encoder (NVENC) to encode a video<br />
using HEVC (H.265):</p>
<pre><code class="language-bash">ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \
  -c:v hevc_nvenc -preset hq \
  -movflags faststart output.mp4
</code></pre>
<p>Unfortunately, even with high-quality preset, the video quality of <code>hevc_nvenc</code><br />
is significantly lower compared to a software encoder (e.g. <code>libx265</code>).</p>

<p>Help about the encoder <code>hevc_nvenc</code> is shown using the following command:</p>
<pre><code class="language-bash">ffmpeg -hide_banner -h encoder=hevc_nvenc
</code></pre>

<h3 id="codecs">Codecs and where they are used</h3>

<h4 id="codecs_discs">Optical disc data storage</h4>
<div class="table">
<table class="cellborders">
<tr>
<th>Disc</th>
<th>Launch</th>
<th>Video codec</th>
<th>Max. resolution</th>
</tr>
<tr>
<td>Ultra HD Blu-ray</td>
<td>2016</td>
<td>H.265/HEVC</td>
<td>3840 &times; 2160</td>
</tr>
<tr>
<td>Blu-ray Disc</td>
<td>2006</td>
<td>H.264/MPEG-4 AVC</td>
<td>1920 &times; 1080</td>
</tr>
<tr>
<td>DVD</td>
<td>1996</td>
<td>MPEG-2</td>
<td>720 &times; 576 (PAL) / 720 &times; 480 (NTSC)</td>
</tr>
</table>
</div>

<h4 id="codecs_internet">Internet</h4>

<p>Mozilla's <a href="https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Video_codecs#recommendations_for_everyday_videos" target="_blank">
recommendations for everyday videos</a>:</p>
<ol>
<li>Container: WebM, Video codec: VP9, Audio codec: Opus
<pre><code class="language-bash">ffmpeg -i input.mp4 \
  -c:v libvpx-vp9 -c:a libopus \
  output.webm
</code></pre>
</li>
<li>Container: MP4, Video codec: AVC (H.264), Audio codec: AAC
<pre><code class="language-bash">ffmpeg -i input.mp4 \
  -c:v libx264 -c:a libfdk_aac \
  -movflags faststart output.mp4
</code></pre>
</li>
</ol>

<p><a href="https://support.google.com/youtube/answer/1722171" target="_blank">
YouTube recommended upload encoding settings</a>:</p>
<ul>
<li>Container: MP4
<pre><code class="language-bash">-movflags faststart output.mp4
</code></pre>
</li>
<li>Video codec: H.264
<pre><code class="language-bash">-c:v libx264 -profile:v high -bf 2 -pix_fmt yuv420p
</code></pre>
</li>
<li>Audio codec: AAC-LC
<pre><code class="language-bash">-c:a libfdk_aac -profile:a aac_low
</code></pre>
</li>
</ul>

<h3 id="links">External links</h3>
<ul>
<li><a href="https://ffmpeg.org/" target="_blank">
https://ffmpeg.org/</a>
</li>
<li><a href="https://trac.ffmpeg.org/wiki/Encode/H.264" target="_blank">
https://trac.ffmpeg.org/wiki/Encode/H.264</a>
</li>
<li><a href="https://trac.ffmpeg.org/wiki/Encode/AAC" target="_blank">
https://trac.ffmpeg.org/wiki/Encode/AAC</a>
</li>
<li><a href="https://trac.ffmpeg.org/wiki/HWAccelIntro" target="_blank">
https://trac.ffmpeg.org/wiki/HWAccelIntro</a>
</li>
<li><a href="https://en.wikipedia.org/wiki/HTML5_video#Browser_support" target="_blank">
https://en.wikipedia.org/wiki/HTML5_video#Browser_support</a>
</li>
</ul>