Streaming to Icecast2 using ffmpeg

[linkstandalone]

It's been a while since I last updated this blog, but I recently played around a lot with ffmpeg and my personal Icecast2 server.
So today, I want to teach you how to stream basically anything to an Icecast2 server.
This post assumes that you already have an Icecast2 server running as well as ffmpeg installed. Well then, let's crack on with the tutorial.

Streaming audio files

First of all, we're gonna stream an audio file to our Icecast2 server.
This is just a simple one-liner:

ffmpeg -re -i YOURAUDIOFILE.flac -ar 48000 -ac 2 -c:a libvorbis -aq 5 -content_type 'audio/ogg' -vn -f ogg 
icecast://source:PASSWORD@YOURICECASTSERVER:8000/MOUNTPOINT.ogg

This seems confusing at first, but it's actually quite simple:
-re This option causes the input file to be read at about 1x speed and not as fast as possible by your CPU, so that ffmpeg doesn't just dump all of the data onto your Icecast2 server
-i YOURAUDIOFILE.flac This is just your input file.
-ar 48000 -ac -c:a libvorbis -aq 5 This just specifies that your output should be 48000Hz stereo audio, encoded using libvorbis with a quality level of 5.
-content_type 'audio/ogg' -f ogg This tells your Icecast2 server that the audio is encoded in the ogg vorbis format.
-vn Disables video output.
icecast://source:PASSWORD@YOURICECASTSERVER:8000/MOUNTPOINT.ogg Your Icecast2 server information. Replace PASSWORD, YOURICECASTSERVER and MOUNTPOINT with the appropriate values.

Streaming an audio device

Streaming an audio device is very similar to streaming a file.
First determine your audio device name using ffmpeg -list_devices true -f dshow -i dummy and then run the following command:

ffmpeg -f dshow -i audio="YOURDEVICENAME" -ar 48000 -ac 2 -c:a libvorbis -aq 5 -content_type 'audio/ogg' -vn 
-f ogg icecast://source:PASSWORD@YOURICECASTSERVER:8000/MOUNTPOINT.ogg

It's basically the same as the previous command, with the only difference being that your input file is now a DirectShow device. As you cannot read a device faster that realtime, the -re flag is not needed anymore