Monday, 26 April 2010

RTP streaming with ffmpeg

Since I often receive private emails asking details about RTP streaming with ffmpeg, I decided to write down some notes about it.
So, first of all, yes, ffmpeg can stream audio and video over RTP. And, as far as I know, there are no major issues with this feature... You just need to know how to do it.

Let's see... The simplest command line you can use to generate an RTP session composed by an audio stream and a video stream is:

ffmpeg -re -i input.mpg -vcodec copy -an -f rtp rtp:// -vn -acodec copy -f rtp rtp:// -newaudio
Analysing this command line:
  • "-re" is needed to stream the audio and the video at the correct rate (instead of streaming as fast as possible)
  • "input.mpg" is the input file, to be streamed
  • this example just streams the audio and the video tracks without re-encoding them... Hence, "-vcodec copy" and "-acodec copy"
  • we need one output for the video, and one for the audio. Hence, the first output has "-an" (no audio), the second output has "-vn" (no video), and there is a "-newaudio" at the end (add the audio track to the second output)
  • the output format has to be RTP. Hence, "-f rtp"
  • the output protocol, has to be RPT, hence, the output file names are "rtp://:"

If you want to re-encode the audio or the video, you can change "-vcodec copy" and "-acodec copy" with whatever you prefer (for example, "-vcodec mpeg4", or similar).

After you start the ffmpeg program, it will print something like

Stream #0.1 -> #1.0
o=- 0 0 IN IP4
s=No Name
t=0 0
a=tool:libavformat 52.61.0
m=video 20000 RTP/AVP 32
c=IN IP4
m=audio 30000 RTP/AVP 14
c=IN IP4

You need to copy the SDP description (starting with "v=0") in a .sdp file, that you will use to play the stream with vlc, ffplay, mplayer, or your favorite video player.

That's it!!!

More details will probably come in the future...