Fixing MPD HTTP Output Stream ends prematurely
The Problem
The Music Player Demon (MPD) is a handy piece of software. Together with Cantata as a client, it has served my music-listening needs very well for years. Sadly, I recently ran into a bit of trouble. MPD supports multiple outputs, so instead of just outputting audio to a local pulseaudio server (or pipewire impersonating a pulseaudio server) it can simultaneously output audio as an http stream. As a homeoffice worker, I use this to listen to my music library on my work machine while I am in calls.
However, recently I’ve been running into an issue where the stream would sometimes end when the track is switched.
> mpv http://localhost:8000
● Audio --aid=1 'Somewhere' (vorbis 2ch 44100 Hz 128 kbps)
File tags:
Artist: Within Temptation
Album: The Silent Force
Album_Artist: Within Temptation
Composer: Sharon Den Adel
Date: 2004
Genre: Hard Rock & Metal
Title: Somewhere
Track: 11
AO: [pipewire] 44100Hz stereo 2ch floatp
[ffmpeg] http: Stream ends prematurely at 239048, should be 18446744073709551615
>
I was unable to find many other people having this issue on the internet (prompting me to write this post) and the references I found offered no solution. There was the classic topic marked as SOLVED without the solution posted and a Github discussion mentioning that nobody except radio stations should use the http output anyway, offering Snapcast as an alternative. Looking at Snapcast, I quickly disregarded this solution as it would require a more involved setup and a specific snapcast client instead of using clients like mpv, VLC or just a plain webbrowser.
At the time, I had been using the following configuration in my mpd.conf without problems for years:
audio_output {
type "httpd"
name "MPD"
encoder "vorbis" # optional, vorbis or lame
port "8000"
bind_to_address "0.0.0.0" # optional, IPv4 or IPv6
max_clients "1" # optional 0=no limit
}
The first promising solution came from the Archlinux forums, suggesting to add the always_on "yes" option. Subjectively, this seemed to improve but not solve the problem. Other sources that were not specific to MPD but streams cutting off in general suggested an encoding issue so I changed the encoder "vorbis" to lame. This brought no improvement at all.
As you can see in the comment in the configuration, it mentions to use “vorbis” or lame”, so at first I disregarded this path for a while. While scrolling through the MPD documentation, I noticed that there are many other encoding plugins available though…
The Solution
Changing the encoder to opus with its required configuration finally fixed the problem.
audio_output {
type "httpd"
name "MPD"
encoder "opus"
port "8000"
bind_to_address "0.0.0.0"
format "44100:16:1"
max_clients "1"
always_on "yes"
opustags "yes"
}