From 2f7945b36019ecdf15c8194525bfa103a3fb9312 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Thu, 28 Jan 2021 14:18:56 +0000
Subject: [PATCH] prober: Add "-t mp3" to decode mp3 streams

---
 prober/decoder.go | 9 +++++++--
 prober/prober.go  | 3 ++-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/prober/decoder.go b/prober/decoder.go
index 7c452662..d8849378 100644
--- a/prober/decoder.go
+++ b/prober/decoder.go
@@ -21,8 +21,13 @@ type decoder struct {
 	cmd *exec.Cmd
 }
 
-func newDecoder(r io.Reader) (*decoder, error) {
-	cmd := exec.Command("sox", "-", "-r", analysisSampleRate, "-c", analysisChannels, "-b", "64", "-e", "float", "--endian", "little", "-t", "raw", "-")
+func newDecoder(r io.Reader, ext string) (*decoder, error) {
+	var args []string
+	if ext == ".mp3" {
+		args = append(args, "-t", "mp3")
+	}
+	args = append(args, "-", "-r", analysisSampleRate, "-c", analysisChannels, "-b", "64", "-e", "float", "--endian", "little", "-t", "raw", "-")
+	cmd := exec.Command("sox", args...)
 	cmd.Stdin = r
 	cmd.Stderr = os.Stderr
 	stdout, err := cmd.StdoutPipe()
diff --git a/prober/prober.go b/prober/prober.go
index 6a854413..b80c7bdb 100644
--- a/prober/prober.go
+++ b/prober/prober.go
@@ -5,6 +5,7 @@ import (
 	"log"
 	"net/http"
 	"net/url"
+	"path/filepath"
 	"time"
 
 	"github.com/prometheus/client_golang/prometheus"
@@ -119,7 +120,7 @@ func (p *Prober) stream(deadline time.Time) {
 
 	// Instrument the raw bytes transfered, before decoding happens.
 	r := newInstrumentedReader(resp.Body, bytesReceived.WithLabelValues(p.streamName))
-	dec, err := newDecoder(r)
+	dec, err := newDecoder(r, filepath.Ext(p.streamURL))
 	if err != nil {
 		log.Printf("decoder error: %v", err)
 		connections.WithLabelValues(p.streamName, "decoder_error").Inc()
-- 
GitLab