Changes between Version 11 and Version 12 of EncodingForStreamingSites


Ignore:
Timestamp:
Apr 20, 2020, 10:01:18 PM (3 months ago)
Author:
llogan
Comment:

no need to promote speed test sites. prompted by SEO attempt from previous edit. other minor changes. prefer -b:v for streaming.

Legend:

Unmodified
Added
Removed
Modified
  • EncodingForStreamingSites

    v11 v12  
    1 = Encoding for streaming sites =
     1{{{
     2#!html
     3<h1 style="font-size: 22px;">
     4Encoding for streaming sites
     5</h1>
     6}}}
    27
    3 … such as twitch.tv, ustream.tv, !YouTube Live, and other RTMP(E) streaming providers. Since FFmpeg development is very active it is recommend that you follow a [[CompilationGuide|compilation guide]] to take advantage of bug fixes and new features.
     8[[PageOutline]]
    49
    5 This guide assumes that you will be using the `libx264` encoder, which currently offers the best possible quality at fast encoding speeds. Other encoders may be more efficient, but slower, e.g. `libx265`. Also, H.264 video provides better compatibility with client devices. For more info, please read the [[Encode/H.264|H.264 Encoding Guide]].
     10Such as twitch.tv, !YouTube Live, Facebook, and other RTMP streaming providers. Since FFmpeg development is very active it is recommend to use a current version. See [[CompilationGuide|compilation guides]] or [https://ffmpeg.org/download.html FFmpeg Download] to take advantage of bug fixes and new features.
     11
     12This guide assumes that you will be using the `libx264` encoder which offers high quality, fast encoding speeds, and high compatibility. Other encoders may be more efficient, but slower, e.g. `libx265`. For more info, please read the [[Encode/H.264|H.264 Encoding Guide]].
    613
    714{{{
    815#!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
    9 '''Note:''' You may have to tweak the commands and settings listed below, e.g. by customizing the `-crf`, `-preset`, `-maxrate`, `-bufsize`, and `-g` options. Make sure you understand what they mean, and visually inspect your output.
     16'''Note:''' You may have to tweak the commands and settings listed below, e.g. by customizing the `-b:v`/`-crf`, `-preset`, `-maxrate`, `-bufsize`, and `-g` options. Make sure you understand what they mean, and visually inspect your output.
    1017}}}
    1118
    12 == `-crf` ==
     19= Options =
    1320
    14 Consider this to be as close as you can get to a "constant quality" mode. Range is 0-51: 0 is lossless, 18 is often considered to be roughly "visually lossless", 23 is the default, and 51 is the worst. Generally you want to use the highest value that still provides an acceptable quality. If you do not know what to choose then 23-28 will probably be acceptable for streaming.
     21== `-b:v` ==
     22
     23Video bitrate. Value is in bits. Refer to the documentation of your specific streaming service for bitrate recommendations.
    1524
    1625== `-preset` ==
    1726
    18 This provides the compression to encoding speed ratio. Use the slowest preset you can: `ultrafast`,`superfast`, `veryfast`, `faster`, `fast`, `medium`, `slow`, `slower`, `veryslow`, `placebo`. Try `-preset veryfast` if you are unsure of what to choose, then watch the console output or the video to see if the encoder is keeping up with your desired output frame rate: if it is not then use a faster preset and/or reduce your `-video_size`.
     27This provides the compression to encoding speed ratio. Use the slowest preset you can: `ultrafast`,`superfast`, `veryfast`, `faster`, `fast`, `medium`, `slow`, `slower`, `veryslow`, `placebo`. Try `-preset veryfast` if you are unsure of what to choose, then watch the console output or the video to see if the encoder is keeping up with your desired output frame rate: if it is not then use a faster preset and/or reduce your width x height or frame rate.
    1928
    2029== `-maxrate` ==
     
    2635* If you have a sane upload rate, or do not know what to choose then a `-maxrate` value of up to 3000k-4000k will probably be fine if your upload rate can handle it (depending on your input complexity and `-video_size`). Refer to your streaming service for any limitations that may apply.
    2736
    28 * Note that the claimed data rate pay your ISP for may not actually be what you get. You can use a site like [http://www.speedtest.net/ speedtest.net] or [https://www.speedcheck.org/ speedcheck.org] to test.
     37* Note that the claimed data rate pay your ISP for may not actually be what you get.
    2938
    3039== `-bufsize` ==
     
    3847----
    3948
     49= Examples =
     50
    4051== Streaming your desktop ==
     52
     53Examples below use [https://ffmpeg.org/ffmpeg-devices.html#x11grab x11grab] for Linux. Windows users can use [https://ffmpeg.org/ffmpeg-devices.html#dshow dshow] or [https://ffmpeg.org/ffmpeg-devices.html#gdigrab gdigrab]. macOS can use [https://ffmpeg.org/ffmpeg-devices.html#avfoundation avfoundation]. See [[Capture/Desktop|FFmpeg Wiki: Capture Desktop]] for additional examples.
    4154
    4255=== Without scaling the output ===
     
    4659{{{
    4760$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1280x720 \
    48 -i :0.0+0,0 -c:v libx264 -preset veryfast -maxrate 1984k -bufsize 3968k \
     61-i :0.0+0,0 -c:v libx264 -preset veryfast -b:v 1984k -maxrate 1984k -bufsize 3968k \
    4962-vf "format=yuv420p" -g 60 -c:a aac -b:a 128k -ar 44100 \
    5063-f flv rtmp://live.twitch.tv/app/<stream key>
     
    5366=== Scaling the output ===
    5467
    55 If you want the output video frame size to be smaller than the input then you can use the [http://ffmpeg.org/ffmpeg-filters.html#scale scale video filter]:
     68If you want the output video frame size to be smaller than the input then you can use the [https://ffmpeg.org/ffmpeg-filters.html#scale scale video filter]:
    5669
    5770{{{
    5871$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1680x1050 \
    59 -i :0.0+0,0 -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 3000k \
     72-i :0.0+0,0 -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 3000k \
    6073-vf "scale=1280:-1,format=yuv420p" -g 60 -c:a aac -b:a 128k -ar 44100 \
    6174-f flv rtmp://live.twitch.tv/app/<stream key>
     
    6477The `-1` in the scale filter example will automatically calculate the correct value to preserve the height. In this case the output will have a frame size of 1280x800.
    6578
    66 === With webcam overlay ===
     79=== With webcam overlay/picture-in-picture (PiP) ===
    6780
    6881This will place your webcam overlay in the top right:
     
    7487"[0:v]scale=1024:-1,setpts=PTS-STARTPTS[bg]; \
    7588 [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \
    76  [bg][fg]overlay=W-w-10:10,format=yuv420p[out]"
    77 -map "[out]" -map 2:a -c:v libx264 -preset veryfast \
    78 -maxrate 3000k -bufsize 4000k -c:a aac -b:a 160k -ar 44100 \
     89 [bg][fg]overlay=W-w-10:10,format=yuv420p[v]"
     90-map "[v]" -map 2:a -c:v libx264 -preset veryfast \
     91-b:v 3000k -maxrate 3000k -bufsize 4000k -c:a aac -b:a 160k -ar 44100 \
    7992-f flv rtmp://live.twitch.tv/app/<stream key>
    8093}}}
    8194
    82 * You can see additional details your webcam with something like: `ffmpeg -f v4l2 -list_formats all -i /dev/video0` or with `v4l2-ctl --list-formats-ext`. See the documentation on the [http://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2 video4linux2 (v4l2) input device] for more info.
     95* You can see additional details your webcam with something like: `ffmpeg -f v4l2 -list_formats all -i /dev/video0` or with `v4l2-ctl --list-formats-ext`. See the documentation on the [https://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2 video4linux2 (v4l2) input device] for more info.
    8396
    84 * Your webcam may natively support whatever frame size you want to overlay onto the main video, so scaling the webcam video as shown in this example can be omitted (just set the appropriate v4l2 `-video_size` and remove the `scale=120:-1,`).
     97* Your webcam may already support whatever frame size you want to overlay onto the main video, so scaling the webcam video as shown in this example can be omitted (just set the appropriate v4l2 `-video_size` and remove the `scale=120:-1,`).
    8598
    8699=== With webcam overlay and logo ===
     
    95108 [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \
    96109 [bg][fg]overlay=W-w-10:10[bg2]; \
    97  [bg2][3:v]overlay=W-w-10:H-h-10,format=yuv420p[out]"
    98 -map "[out]" -map 2:a -c:v libx264 -preset veryfast \
     110 [bg2][3:v]overlay=W-w-10:H-h-10,format=yuv420p[v]"
     111-map "[v]" -map 2:a -c:v libx264 -preset veryfast \
    99112-maxrate 3000k -bufsize 4000k -c:a aac -b:a 160k -ar 44100 -b:a 128k \
    100113-f flv rtmp://live.twitch.tv/app/<stream key>
     
    106119
    107120{{{
    108 $ ffmpeg -re -i input.mkv -c:v libx264 -preset veryfast -maxrate 3000k \
     121$ ffmpeg -re -i input.mkv -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k \
    109122-bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 \
    110123-ar 44100 -f flv rtmp://live.twitch.tv/app/<stream key>
     
    116129
    117130{{{
    118 $ ffmpeg -i input.mkv -c:v libx264 -preset medium -maxrate 3000k -bufsize 6000k \
     131$ ffmpeg -i input.mkv -c:v libx264 -preset medium -b:v 3000k -maxrate 3000k -bufsize 6000k \
    119132-vf "scale=1280:-1,format=yuv420p" -g 50 -c:a aac -b:a 128k -ac 2 -ar 44100 file.flv
    120133}}}
    121134
    122 Then [http://ffmpeg.org/ffmpeg.html#Stream-copy stream copy] it to the streaming service:
     135Then [https://ffmpeg.org/ffmpeg.html#Stream-copy stream copy] it to the streaming service:
    123136{{{
    124137$ ffmpeg -re -i file.flv -c copy -f flv rtmp://live.twitch.tv/app/<stream key>
     
    130143
    131144{{{
    132 $ ffmpeg -i input -map 0 -c:v libx264 -c:a aac -maxrate 1000k -bufsize 2000k -g 50 -f tee \
     145$ ffmpeg -i input -map 0 -c:v libx264 -c:a aac -b:v 1000k -maxrate 1000k -bufsize 2000k -g 50 -f tee \
    133146"[f=flv:onfail=ignore]rtmp://facebook|[f=flv:onfail=ignore]rtmp://youtube|local_file.mkv"
    134147}}}
     
    136149Some encoders may need different options depending on the output format; the auto-detection of this can not work with the tee muxer, so they need to be explicitly specified. The main example is the '''global_header''' flag.
    137150{{{
    138 $ ffmpeg -i input -map 0 -flags +global_header -c:v libx264 -c:a aac -maxrate 1000k -bufsize 2000k -g 50 -f tee \
     151$ ffmpeg -i input -map 0 -flags +global_header -c:v libx264 -c:a aac -b:v 1000k -maxrate 1000k -bufsize 2000k -g 50 -f tee \
    139152"[f=flv:onfail=ignore]rtmp://facebook|[f=flv:onfail=ignore]rtmp://youtube|local_file.mkv"
    140153}}}
     
    142155----
    143156
    144 == Notes ==
     157= Notes =
    145158
    146159* Linux users can use `xwininfo | grep geometry` to select the target window and get placement coordinates. For example, an output of  `-geometry 800x600+284+175` would result in using `-video_size 800x600 -i :0.0+284,175`. You can also use it to automatically enter the input screen size: `-video_size $(xwininfo -root | awk '/-geo/{print $2}')`.
    147160
    148 * The [http://ffmpeg.org/ffmpeg-devices.html#pulse-1 pulse input device] (requires `--enable-libpulse`) can be an alternative to the [http://ffmpeg.org/ffmpeg-devices.html#alsa-1 ALSA input device], as in: `-f pulse -i default`.
     161* The [https://ffmpeg.org/ffmpeg-devices.html#pulse-1 pulse input device] (requires `--enable-libpulse`) can be an alternative to the [https://ffmpeg.org/ffmpeg-devices.html#alsa-1 ALSA input device], as in: `-f pulse -i default`.
    149162
    150 * Windows users can use the [http://ffmpeg.org/ffmpeg-devices.html#dshow Windows DirectShow (dshow) input device]. Also see: [[How to grab the desktop (screen) with FFmpeg#Windows|How to grab the desktop (screen) with FFmpeg]].
     163* Windows users can use [https://ffmpeg.org/ffmpeg-devices.html#dshow dshow] or [https://ffmpeg.org/ffmpeg-devices.html#gdigrab gdigrab]. macOS can use [https://ffmpeg.org/ffmpeg-devices.html#avfoundation avfoundation]. See [[Capture/Desktop|FFmpeg Wiki: Capture Desktop]] for examples.
    151164
    152 == Getting help ==
     165= Help =
    153166
    154167Always use a recent `ffmpeg`. See the [[CompilationGuide|compilation guides]] for more information.
    155168
    156 If you need additional help then send a message to the [http://ffmpeg.org/contact.html ffmpeg-user mailing list] (see MailingListEtiquette first) or hang out in the ''#ffmpeg'' IRC channel on Freenode.
    157 
    158 {{{#!comment
    159 * Needs some Windows specific dshow examples.
    160 * Show recommended buffer sizes, and maxrate limitations for specific providers.
    161 }}}
     169If you need additional help then send a message to the [https://ffmpeg.org/contact.html ffmpeg-user mailing list] (see [https://ffmpeg.org/mailing-list-faq.html Mailing List FAQ] first) or hang out in the ''#ffmpeg'' IRC channel on Freenode.