| Version 1 (modified by , 12 years ago) ( diff ) |
|---|
Encoding for streaming sites
Such as justin.tv, twitch.tv, and ustream.tv.
Note: Do not blindly run any of these commands. You must customize your -maxrate, -bufsize, and -g as shown in this guide.
-maxrate
Anytime you are encoding video with bandwidth as a limiting factor you should be using VBV (Video Buffer Verifier) with the -maxrate and -bufsize options:
- 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
-maxratevalue.
- If you have a sane upload rate, or do not know what to choose then a
-maxratevalue of 3000k-4000k will probably be fine if your upload rate can handle it.
- Note that the claimed data rate pay your ISP for may not actually be what you get. You can use a site like speedtest.net to test.
-bufsize
-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.
-g
- 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.
Streaming your desktop
Without scaling the output
If you want the output video frame size to be the same as the input:
$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1280x720 \ -i :0.0+0,0 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 3000k \ -vf "format=yuv420p" -g 60 -acodec libmp3lame -b:a 96k -ar 44100 \ -f flv rtmp://live.justin.tv/app/<stream key>
Scaling the output
If you want the output video frame size to be smaller than the input then you can use the scale video filter:
$ ffmpeg -f alsa -ac 2 -i hw:0,0 -f x11grab -framerate 30 -video_size 1680x1050 \ -i :0.0+0,0 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 3000k \ -vf "scale=1280:-1,format=yuv420p" -g 60 -acodec libmp3lame -b:a 96k -ar 44100 \ -f flv rtmp://live.justin.tv/app/<stream key>
The -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.
With webcam overlay
This will place your webcam overlay in the top right:
$ ffmpeg -f x11grab -video_size 1680x1050 -framerate 30 -i :0.0 \ -f v4l2 -video_size 320x240 -framerate 30 -i /dev/video0 \ -f alsa -ac 2 -i hw:0,0 -filter_complex \ "[0:v]scale=1024:-1,setpts=PTS-STARTPTS[bg]; \ [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \ [bg][fg]overlay=W-w-10:10,format=yuv420p[out]" -map "[out]" -map 2:a -vcodec libx264 -preset veryfast \ -maxrate 3000k -bufsize 4000k -acodec libmp3lame -ar 44100 -b:a 128k \ -f flv rtmp://live.justin.tv/app/<stream key>
- You can see additional details your webcam with something like:
ffmpeg -f v4l2 -list_formats all -i /dev/video0or withv4l2-ctl --list-formats-ext. See the documentation on the video4linux2 (v4l2) input device for more info.
- 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_sizeand remove thescale=120:-1,).
With webcam overlay and logo
This will place your webcam overlay in the top right, and a logo in the bottom left:
$ ffmpeg -f x11grab -video_size 1680x1050 -framerate 30 -i :0.0 \ -f v4l2 -video_size 320x240 -framerate 30 -i /dev/video0 \ -f alsa -ac 2 -i hw:0,0 -i logo.png -filter_complex \ "[0:v]scale=1024:-1,setpts=PTS-STARTPTS[bg]; \ [1:v]scale=120:-1,setpts=PTS-STARTPTS[fg]; \ [bg][fg]overlay=W-w-10:10[bg2]; \ [bg2][3:v]overlay=W-w-10:H-h-10,format=yuv420p[out]" -map "[out]" -map 2:a -vcodec libx264 -preset veryfast \ -maxrate 3000k -bufsize 4000k -acodec libmp3lame -ar 44100 -b:a 128k \ -f flv rtmp://live.justin.tv/app/<stream key>
Streaming a file
$ ffmpeg -re -i input.mkv -vcodec libx264 -preset veryfast -maxrate 3000k \ -bufsize 6000k -pix_fmt yuv420p -g 50 -acodec libmp3lame -b:a 128k -ac 2 \ -ar 44100 -f flv rtmp://live.justin.tv/app/<stream key>
Encoding a file for streaming
If your computer is too slow to encode the file on-the-fly like the example above then you can re-encode it first:
$ ffmpeg -i input.mkv -vcodec libx264 -preset medium -maxrate 3000k -bufsize 6000k \ -vf "scale=1280:-1,format=yuv420p" -g 50 -acodec libmp3lame -b:a 128k -ac 2 -ar 44100 file.flv
Then stream copy it to the streaming service:
$ ffmpeg -re -i file.flv -codec copy -f flv rtmp://live.justin.tv/app/<stream key>
Notes
- You can use
xwininfo | grep geometryto select the target window and get placement coordinates. For example, an output of-geometry 800x600+284+175would result in using-video_size 800x600 -i :0.0+284,175.
- The pulse input device (requires
--enable-libpulse) can be an alternative to the ALSA input device, as in:-f pulse -i default.
- Windows users can use the Windows DirectShow (dshow) input device. Also see: How to grab the desktop (screen) with FFmpeg.
Getting help
Always use a recent ffmpeg. See the compilation guides for more information.
If you need additional help then send a message to the ffmpeg-user mailing list (see MailingListEtiquette first) or hang out in the #ffmpeg IRC channel on Freenode.


