ffmpeg snippets[source]

xml
<glacius:metadata>
    <title>ffmpeg snippets</title>
    <description>Scripts for doing stuff with ffmpeg</description>
    <category>Programming</category>
    <category>ffmpeg</category>
    <category>Bash</category>
    <category>Code snippets</category>
</glacius:metadata>
<h2>Extract single frame as image</h2>
<glacius:code lang="bash"><![CDATA[
# extract image from a video at timestamp 1:16
# use -q:v <num> for jpeg quality control
ffmpeg -i video.mp4 -ss 01:16 -frames:v 1 image.jpeg
]]></glacius:code>
<p>
    See <a href="https://superuser.com/a/324596">this Super User answer</a> for
    quality details, apparently the values of <code>-qscale</code> are between 1 and 31
    for jpegs. PNG is lossless so won't be affected by quality. The <code>v</code>
    in <code>q:v</code> refers to the video stream. From the man page:
</p>
<blockquote>
    <p>
        Use fixed quality scale (VBR). The meaning of q/qscale is codec-dependent.
        If qscale is used without a stream_specifier then it applies only to the
        video stream, this is to maintain compatibility with previous behavior and
        as specifying the same codec specific value to 2 different codecs that is
        audio and video generally is not what is intended when no stream_specifier
        is used.
    </p>
</blockquote>
<h2>Fixing Firefox's "corrupt MP4" issue</h2>
<p>
    Firefox can't handle something about yuv444 chroma sampling (it will probably
    say something in the console about being unable to decode it).
</p>
<blockquote><p>
    Decoder may not have the capability to handle the requested video format with 
    YUV444 chroma subsampling
</p></blockquote>
<p>
    Fix it by using the <code>-pix_fmt</code> option, e.g.
</p>
<glacius:code lang="bash"><![CDATA[
ffmpeg \
  -i input.mp4 \
  -pix_fmt yuv420p \
  -c:a copy \
  output.mp4
]]></glacius:code>
<h2>Fade out video and/or audio</h2>
<p>
    Add a 2-second fade-in from the beginning and a 2-second fade-out
    from the end of an 81 second video:
</p>
<glacius:code lang="bash"><![CDATA[
ffmpeg -i input.mp4 \
  -filter:a afade=t=in:d=2:st=0,afade=t=out:d=2:st=79 \
  -filter:v fade=t=in:d=2:st=0,fade=t=out:d=2:st=79 \
  output.mp4
]]></glacius:code>