Back to TIL
shell

Poor man's audio stream with FFmpeg

Experimented with streaming setups using FFmpeg, trying RTP and HLS configurations. HLS added some latency, but it’s a solid option for background streaming on different devices. Safari allows HLS streams to run in the background on iOS.

Using my MacBook’s mic as stream source:

$ ffmpeg -f avfoundation -i ":1" -acodec libmp3lame -ab 32k -ac 1 -f rtp rtp://0.0.0.0:12345

Open up rtp://127.0.0.1:12345 in VLC and listen to yourself talk.

Next up was streaming to a server in a data center. I installed ffmpeg and nginx. Don’t forget to also open up the specified port in the firewall.

Then it’s as simple as modifying the default nginx server (/etc/nginx/sites-enabled/default)

location /hls {
    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    }
    alias /var/www/html/hls/;
    add_header Cache-Control no-cache;
    add_header 'Access-Control-Allow-Origin' '*';
}

Opening that up in VLC as well, there was quite some latency.

Opening up the stream on iOS did allow for it to run in the background.

There are more professional ways of doing this, using mediamtx for example, but I didn’t want to sink more time into this. At least it has solid documentation.