1

I've been looking around for a solution to this and I'm completely stuck. The icecast/shoutcast libs all seem to be Python 2.7 which is an issue as I'm using 3.6

Any ideas for where to start with broadcasting and authenticating would be very useful. I'm looking to stream mp3 files.

TIA.

Matt Cowley
  • 2,164
  • 2
  • 18
  • 29
  • Does it have to be SHOUTcast? If you could use Icecast instead, it's just an HTTP PUT request. SHOUTcast sources are non-standard. – Brad Nov 06 '17 at 04:36
  • @Brad icecast would also work, I again have no idea how to stream via a PUT request though. – Matt Cowley Dec 30 '17 at 16:54

2 Answers2

1

First, you need to encode your audio stream. You need to run this stream in real-time, and it needs to have a constant sample rate. It isn't enough to just pipe MP3 files through... they can have ID3 tags which may break the stream, and they might be at different sample rates. It's also ideal to have a constant bitrate for internet streaming (but this is not required for the stream to work).

In the end, you'll basically have one big never-ending MP3 file that is created at the same rate it will be played back.

Next, you simply need to make an HTTP PUT to the Icecast server. See this question: Is there any way to do HTTP PUT in python

Icecast uses basic auth, generally.

If you want to make this easier on yourself (albeit with less control), shell out to FFmpeg:

ffmpeg -re -i [some playlist] -f mp3 -method PUT http://example.com/some-stream

(where example.com is your Icecast server)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Using ffmpeg works well as other parts of my project also use it. Could you explain how exactly I'd going about authing and calling ffmpeg with a callback? – Matt Cowley Dec 30 '17 at 21:42
  • Using ffmpeg like so: `ffmpeg -i "[file]" -f mp3 -method PUT http://[usr]:[pswd]@[host]:8000/stream.mp3` Results in: `av_interleaved_write_frame(): Unknown error Error writing trailer of http://[usr]:[pswd]@[host]:8000/stream.mp3: Error number -10053 occurredsize= 7kB time=00:00:00.50 bitrate= 107.4kbits/s speed=43.9x video:0kB audio:6kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 4.768518% Conversion failed!` Any ideas? – Matt Cowley Dec 30 '17 at 22:09
  • @MattCowley Ah, yes, you need `-re` before the input. Speed should be 1x. :-) Also, conversion is always going to "fail" because it's the end of the stream. If you read from a perpetual source, non-issue. And, your auth in-URL should be fine. I don't write Python so I can't help you with Python callbacks, unfortunately. – Brad Dec 30 '17 at 22:23
  • Thank you! How does ffmpeg take a playlist? I'm currently while looping ffmpeg calls with individual tracks but this causes the stream to die between tracks and requires a refresh client-side to listen again. – Matt Cowley Dec 31 '17 at 11:49
  • @MattCowley Check out input concat, but maybe for your case you should have separate FFmpeg processes... one that decodes the file to PCM, and then pipe that PCM data into your encoding process as it comes in. You can also use something like VLC for the playback and FFmpeg for the output. – Brad Dec 31 '17 at 19:04
  • Is there any better way to talk to you as this comment chain is getting large? Following on: The library I'm using for other stuff in my project contains a couple of classes the handle ffmpeg to PCM, and leave me with a method `read()` that returns 20ms of byte like data (audio). Any chance you could possibly explain how I could then send that into a continual stream via ffmpeg put? – Matt Cowley Jan 02 '18 at 11:25
  • You can contact me at brad@audiopump.co if you'd like to schedule a consulting session. As to writing to FFmpeg in stream, just call that read method repeatedly. If the reading side is set with `-re`, then it's going to run in realtime, which is what you want. So, you just have to write as fast as the read goes. – Brad Jan 02 '18 at 21:03
0

Use liquidsoap to generate your audio stream(s) and output them to shoutcast and/or icecast2 servers. I currently have liquidsoap, shoutcast, icecast2 and apache2 all running on the same Ubuntu 18.04 server. liquidsoap generates the audio stream and outputs it to both shoutcast and icecast2. Listeners can use their browser to access either the shoutcast stream at port 8000 or the icecast2 stream at port 8010. It works very well 24 x 7.

You can have multiple streams and liquidsoap has many features including playlists and time-based (clock) actions. See the liquidsoap documentation for examples to create audio streams from your mp3 or other format audio files. Best of all liquidsoap is free.

Jack
  • 1
  • 1