Skip to content

Simple http mp3 streaming

Aline Freitas edited this page Apr 24, 2014 · 1 revision

I wanted to listen on ncmpcpp, a cli frontend to MPD, but the tricky should work on any other music player.

If I wanted a ogg streaming from the /dev/netcat device I could do like this:

aline@luiza-mouraria ~ $ { echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat /dev/netcat ; } | nc -l -p 8080

And capture the output from, for example MPlayer:

aline@luiza-mouraria ~ $ mplayer http://localhost:8080
MPlayer 1.1-4.7.3 (C) 2000-2012 MPlayer Team
Reproduzindo http://localhost:8080
Resolving localhost for AF_INET...
Connecting to server localhost[127.0.0.1]: 8080...

Cache size set to 320 KBytes
Cache fill:  0.00% (0 bytes)   

libavformat version 54.29.104 (external)
Detectado formato de arquivo libavformat!
[ogg @ 0x7fad2ea26fa0]Estimating duration from bitrate, this may be inaccurate
[lavf] stream 0: audio (vorbis), -aid 0
==========================================================================
Abrindo decodificador de audio: [ffmpeg] FFmpeg/libavcodec audio decoders
libavcodec version 54.59.100 (external)
AUDIO: 22050 Hz, 2 ch, s16le, 104.8 kbit/14.85% (ratio: 13100->88200)
Selected audio codec: [ffvorbis] afm: ffmpeg (FFmpeg Vorbis)
==========================================================================
AO: [alsa] 48000Hz 2ch s16le (2 bytes per sample)
Vídeo: sem vídeo
Iníciando reprodução...
A:  16.8 (16.8) of 0.0 (unknown)  0.7% 47%

It plays nice! But, for some reason it didn't work on MPD. So I use ffmpeg to convert the stdin from 2050hz ogg to 44100hz mp3 and pipe it to this simple netcat http stream:

aline@luiza-mouraria ~ $ { echo -ne "HTTP/1.0 200 OK\r\n\r\n"; ffmpeg -i /dev/netcat -ar 44100 -f mp3 - ; } | nc -l -p 8080
ffmpeg version 1.0.8 Copyright (c) 2000-2013 the FFmpeg developers
  built on Apr 24 2014 14:55:38 with gcc 4.7.3 (Gentoo 4.7.3-r1 p1.4, pie-0.5.5)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --optflags='-march=native -O2 -pipe' --extra-cflags='-march=native -O2 -pipe' --extra-cxxflags='-march=native -O2 -pipe' --disable-static --enable-gpl --enable-postproc --enable-avfilter --enable-avresample --disable-stripping --disable-debug --disable-doc --disable-vaapi --disable-vdpau --disable-ffplay --disable-runtime-cpudetect --enable-libmp3lame --disable-indev=oss --disable-indev=jack --enable-x11grab --disable-outdev=oss --disable-outdev=sdl --enable-libfreetype --disable-altivec --disable-avx --disable-ssse3 --disable-vis --disable-neon --cpu=host --enable-hardcoded-tables
  libavutil      51. 73.101 / 51. 73.101
  libavcodec     54. 59.100 / 54. 59.100
  libavformat    54. 29.104 / 54. 29.104
  libavdevice    54.  2.101 / 54.  2.101
  libavfilter     3. 17.100 /  3. 17.100
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[ogg @ 0x633150] Estimating duration from bitrate, this may be inaccurate
Input #0, ogg, from '/dev/netcat':
  Duration: N/A, start: 0.000000, bitrate: 104 kb/s
    Stream #0:0: Audio: vorbis, 22050 Hz, stereo, s16, 104 kb/s
Output #0, mp3, to 'pipe:':
  Metadata:
    TSSE            : Lavf54.29.104
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16
Stream mapping:
  Stream #0:0 -> #0:0 (vorbis -> libmp3lame)
Press [q] to stop, [?] for help

Now I created a playlist file netcat.m3u with http://localhost:8080. From ncmpcpp I just browse to playlists, add netcat.m3u and... it works!

Clone this wiki locally