wiki:

StreamingGuide


Version 29 (modified by rogerdpack, 4 years ago) (diff)

probe

Streaming

FFmpeg can basically stream through one of two ways: It either streams to a some "other server", which restreams for it, or it can stream via UDP directly to some destination host, or alternatively directly to a multicast destination. Servers which can receive from FFmpeg (to restream) include ffserver (linux only, though with cygwin it might work), or Wowza Media Server, or Flash Media Server. Even VLC can pick up the stream from ffmpeg, then redistribute it, acting as a server. Nginx also has an rtmp redistribution plugin. Since FFmpeg is at times more efficient than VLC at doing the raw encoding, this can be a useful option compared to doing both transcoding and streaming in VLC.

How to stream with several different simultaneous bitrates is described here.

NB that when you are testing your streams, you may want to test them with both VLC and FFplay, as FFplay sometimes introduces its own artifacts when it is scaled (it has poor quality scaling). Don't use ffplay as your baseline for determining quality.

Also note that encoding it to the x264 "baseline" is basically for older iOS devices or the like, see here. Some people argue that just using mpeg4video codec is better than x264 baseline (where possible) since it is a simpler codec.

The FFmpeg's "-re" flag means to "Read input at native frame rate. Mainly used to simulate a grab device." i.e. if you want to play a video file, but at realtime, then use this. My guess is you typically don't want this flag when streaming from a live device.

Here's how one guy broadcast a live stream:

$ ffmpeg -y -loglevel warning -f dshow -i video="screen-capture-recorder" -vf crop=690:388:136:0 -r 30 -s 962x388 -threads 2 -vcodec libx264 -vpre baseline -vpre my_ffpreset -f flv rtmp:///live/myStream.sdp

Here is my FFmpeg preset (libx264-my_ffpreset.ffpreset):
coder=1
flags2=+wpred+dct8x8
level=31
maxrate=1200000
bufsize=200000
wpredp=0
g=60
refs=1
subq=3
trellis=0
bf=0
rc_lookahead=0

Here is what another person did:

ffmpeg -f dshow -I video="Virtual-Camera" -vcodec libx264 -tune zerolatency
-b 900k -f mpegts udp://10.1.0.102:1234

And here is what another person did:

ffmpeg -f dshow -i video="screen-capture-recorder":audio="Stereo Mix (IDT High
Definition" -vcodec  libx264 -preset ultrafast -tune zerolatency -r 10
-async 1 -acodec libmp3lame -ab 24k -ar 22050 -bsf:v h264_mp4toannexb
-maxrate 750k -bufsize 3000k   -f mpegts udp://192.168.5.215:48550

NB that they also had to adjust the rtbufsize in that example. I'm also not entirely sure which presets are "best" or what the available options are. Also note that newer version of FFmpeg may need a different syntax for specifying preset/tune.

Latency

You may be able to decrease latency by specifing that I-frames come "more frequently" (or basically always, in the case of x264's zerolatency setting), though this can increase frame size and decrease quality, see here for some more background. Basically for typical x264 streams, it inserts an I-frame every 250 frames. This means that new clients that connect to the stream may have to wait up to 250 frames before they can start receiving the stream (or start with old data). So increasing I-frame frequency (makes the stream larger, but might decrease latency). For real time captures you can also decrease latency of audio in windows dshow by using the dshow audio_buffer_size setting. You can also decrease latency by tuning any broadcast server you are using to minimize latency, and finally by tuning the client that receives the stream to not "cache" any incoming data, which, if it does, increases latency.

Sometimes audio codecs also introduce some latency of their own. You may be able to get less latency by using speex, for example, or opus, in place of libmp3lame.

You will also want to try and decrease latency at the server side, for instance wowza hints.

Also setting -probesize and -analyzeduration to low values may help your stream start up more quickly (it uses these to scan for "streams" in certain muxers, like ts, where some can appears "later", and also to estimate the duration, which, for live streams, the latter you don't need anyway).

Cpu usage/File size

In general, the more cpu you use to compress, the better the output image will be, or the smaller of a file the output will be.

Basically, the easiest way to save cpu is to decrease the input frame rate/size, or decrease the output frame rate/size.

Also you could (if capturing from live source), instruct the live source to feed a "smaller stream" (ex: webcam stream 640x480 instead of 1024x1280), or you could set a lower output "output quality" setting (q level), or specify a lower output desired bitrate (see x264EncodingGuide for a background). Or try a different output codec, or specify new parameters to your codec (for instance, a different profile or preset for libx264). Specifying $ -threads 0 instructs the encoder to use all available cpu cores, which is the default. You could also resize the input first, before transcoding it, so it's not as large. Applying a smoothing filter like hqdn3d before encoding might help it compress better, yielding smaller files.

You can also set a lower output frame rate to of course decrease cpu usage.

If you're able to live capture in a pixel format that matches your output format (ex: yuv420p output from a webcam, instead of mjpeg), that might help with cpu usage, since it avoids an extra conversion. Using 64-bit instead of 32-bit executables (for those that have that choice) can result in a slight speedup. If you're able to use -vcodec copy that, of course, uses the least cpu of all options since it just sends the frames verbatim to the output.

Sometimes you can change the "pixel formats" somehow, like using rgb16 instead of rgb24, to save time/space (or yuv420 instead of yuv444 or the like, since 420 stores less information it may compress better).

Streaming a simple RTP audio stream from FFmpeg

FFmpeg can stream a single stream using the RTP protocol. In order to avoid buffering problems on the other hand, the streaming should be done through the -re option, which means that the stream will be streamed in real-time (i.e. it slows it down to simulate a live streaming source.

For example the following command will generate a signal, and will stream it to the port 1234 on localhost:

ffmpeg -re -f lavfi -i aevalsrc="sin(400*2*PI*t)" -ar 8000 -f mulaw -f rtp rtp://127.0.0.1:1234

To play the stream with ffplay, run the command:

ffplay rtp://127.0.0.1:1234

Codecs

The most popular streaming codec is probably libx264, though if you're streaming to a device which requires a "crippled" baseline h264 implementation, some have argued that the mp4 video codec is better. You can also use mpeg2video, or really any other video codec you want, typically, as long as your receiver can decode it, and it suits your needs.

Outputting files

FFmpeg supports splitting files (using "-f segment" for the output, see segment muxer) into time based chunks, useful for HTTP live streaming style file output. See also this SO post.

Transcoding/repeating

Ffmpeg can also receive from "a source" (for instance live or udp) and then transcode and re-broadcast the stream.

One mailing list user wrote this, quote:

In my application, I have a server running vlc sucking streams from some cameras, encoding them as MPEG2 streams and sending them to ports 5000 through 5003 on my intermediary server (I'll have to let someone else explain how to set that up, as that was someone else's part of the project). I have another server running Wowza, with an instance named "live". And I've got an intermediate server that sucks in MPEG2 streams coming in on ports 5000 to 5003, transcoding them into mp4 streams with H.264 and AAC codecs, and pushing the transcoded streams on to Wowza.

The command line I use to pull the stream from port 5000, transcode it, and push it is: ffmpeg -i 'udp://localhost:5000?fifo_size=1000000&overrun_nonfatal=1' -crf 30 -preset ultrafast -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -vcodec libx264 -r 25 -b:v 500k -f flv 'rtmp://<wowza server IP>/live/cam0'

-i 'udp://localhost:5000?fifo_size=1000000&overrun_nonfatal=1' tells ffmpeg where to pull the input stream from. The parts after the ? are probably not needed most of the time, but I did need it after all.

-crf 30 sets the Content Rate Factor. That's an x264 argument that tries to keep reasonably consistent video quality. A value of 30 allows somewhat lower quality and bit rate.

-preset ultrafast as the name implies provides for the fastest possible encoding. If some tradeoff between quality and encode speed, go for the speed. This might be needed if you are going to be transcoding multiple streams on one machine.

-acodec aac sets the audio codec (internal AAC encoder)

-strict experimental allows use of some experimental codecs (the internal AAC encoder is experimental)

-ar 44100 set the audio sample rate

-ac 2 specifies two channels of audio

-b:a 96k sets the audio bit rate

-vcodec libx264 sets the video codec

-r 25 set the frame rate

-b:v 500k set the video bit rate

-f flv says to deliver the output stream in an flv wrapper

'rtmp://<wowza server IP>/live/cam0' is where the transcoded video stream gets pushed to

variable bitrate

FFmpeg doesn't (today) support varying a bitrate based on changing network conditions. It does support outputting in "different" bitrates, at the same time, however see here, which is vaguely related.

troubleshooting

If you get a "black/blank" screen from your server, try sending it yuv422p or yuv420p type input. Some servers get confused if you send them yuv444 input (which is the default for libx264).

See also

This tutorial.