Changes between Initial Version and Version 1 of EncodingForStreamingSites


Ignore:
Timestamp:
Feb 17, 2014, 8:19:53 AM (6 years ago)
Author:
llogan
Comment:

make initial page

Legend:

Unmodified
Added
Removed
Modified
  • EncodingForStreamingSites

    v1 v1  
     1= Encoding for streaming sites =
     2
     3Such as justin.tv, twitch.tv, and ustream.tv.
     4
     5{{{
     6#!div style="border: 1pt dotted; margin: 1em; background-color: #fffff9;"
     7'''Note:''' Do not blindly run any of these commands. You must customize your `-maxrate`, `-bufsize`, and `-g` as shown in this guide.
     8}}}
     9
     10== `-maxrate` ==
     11
     12Anytime you are encoding video with bandwidth as a limiting factor you should be using VBV (Video Buffer Verifier) with the `-maxrate` and `-bufsize` options:
     13
     14* Assuming your upload rate is 1024kbit/s (1 megabit/s), and assuming you can reliably utilize 80% of that = 820 kbit/s. Audio will consume 128 kbit/s (64k/channel for stereo) leaving ~692 kbit/s for video: this is your `-maxrate` value.
     15
     16* If you have a sane upload rate, or do not know what to choose then a `-maxrate` value of 3000k-4000k will probably be fine if your upload rate can handle it.
     17
     18* 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] to test.
     19
     20== `-bufsize` ==
     21
     22`-bufsize` sets the buffer size, and can be 1-2 seconds. If you use `-maxrate 3000k` then use a `-bufsize` of 3000k-6000k. You will have to experiment to see what looks best for your content.
     23
     24== `-g` ==
     25
     26* Use a 2 second GOP (Group of Pictures), so simply multiply your output frame rate * 2. For example, if your input is `-framerate 30`, then use `-g 60`.
     27
     28== Streaming your desktop ==
     29
     30=== Without scaling the output ===
     31
     32If you want the output video frame size to be the same as the input:
     33
     34{{{
     35$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1280x720 \
     36-i :0.0+0,0 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 3000k \
     37-vf "format=yuv420p" -g 60 -acodec libmp3lame -b:a 96k -ar 44100 \
     38-f flv rtmp://live.justin.tv/app/<stream key>
     39}}}
     40
     41=== Scaling the output ===
     42
     43If 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]:
     44
     45{{{
     46$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1680x1050 \
     47-i :0.0+0,0 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 3000k \
     48-vf "scale=1280:-1,format=yuv420p" -g 60 -acodec libmp3lame -b:a 96k -ar 44100 \
     49-f flv rtmp://live.justin.tv/app/<stream key>
     50}}}
     51
     52The `-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.
     53
     54=== With webcam overlay ===
     55
     56This will place your webcam overlay in the top right:
     57
     58{{{
     59$ ffmpeg -f x11grab -video_size 1680x1050 -framerate 30 -i :0.0 \
     60-f v4l2 -video_size 320x240 -framerate 30 -i /dev/video0 \
     61-f alsa -ac 2 -i hw:0,0 -filter_complex \
     62"[0:v]scale=1024:-1,setpts=PTS-STARTPTS[bg]; \
     63 [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \
     64 [bg][fg]overlay=W-w-10:10,format=yuv420p[out]"
     65-map "[out]" -map 2:a -vcodec libx264 -preset veryfast \
     66-maxrate 3000k -bufsize 4000k -acodec libmp3lame -ar 44100 -b:a 128k \
     67-f flv rtmp://live.justin.tv/app/<stream key>
     68}}}
     69
     70* 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.
     71
     72* 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,`).
     73
     74=== With webcam overlay and logo ===
     75
     76This will place your webcam overlay in the top right, and a logo in the bottom left:
     77
     78{{{
     79$ ffmpeg -f x11grab -video_size 1680x1050 -framerate 30 -i :0.0 \
     80-f v4l2 -video_size 320x240 -framerate 30 -i /dev/video0 \
     81-f alsa -ac 2 -i hw:0,0 -i logo.png -filter_complex \
     82"[0:v]scale=1024:-1,setpts=PTS-STARTPTS[bg]; \
     83 [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \
     84 [bg][fg]overlay=W-w-10:10[bg2]; \
     85 [bg2][3:v]overlay=W-w-10:H-h-10,format=yuv420p[out]"
     86-map "[out]" -map 2:a -vcodec libx264 -preset veryfast \
     87-maxrate 3000k -bufsize 4000k -acodec libmp3lame -ar 44100 -b:a 128k \
     88-f flv rtmp://live.justin.tv/app/<stream key>
     89}}}
     90
     91----
     92
     93== Streaming a file ==
     94
     95{{{
     96$ ffmpeg -re -i input.mkv -vcodec libx264 -preset veryfast -maxrate 3000k \
     97-bufsize 6000k -pix_fmt yuv420p -g 50 -acodec libmp3lame -b:a 128k -ac 2 \
     98-ar 44100 -f flv rtmp://live.justin.tv/app/<stream key>
     99}}}
     100
     101=== Encoding a file for streaming ===
     102
     103If your computer is too slow to encode the file on-the-fly like the example above then you can re-encode it first:
     104
     105{{{
     106$ ffmpeg -i input.mkv -vcodec libx264 -preset medium -maxrate 3000k -bufsize 6000k \
     107-vf "scale=1280:-1,format=yuv420p" -g 50 -acodec libmp3lame -b:a 128k -ac 2 -ar 44100 file.flv
     108}}}
     109
     110Then [http://ffmpeg.org/ffmpeg.html#Stream-copy stream copy] it to the streaming service:
     111{{{
     112$ ffmpeg -re -i file.flv -codec copy -f flv rtmp://live.justin.tv/app/<stream key>
     113}}}
     114
     115----
     116
     117== Notes ==
     118
     119* You 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`.
     120
     121* 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`.
     122
     123* 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]].
     124
     125== Getting help ==
     126
     127Always use a recent `ffmpeg`. See the [[CompilationGuide|compilation guides]] for more information.
     128
     129If 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.