2021-06-03 19:07:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
|
2021-06-21 20:37:22 +02:00
|
|
|
"rsr/model"
|
|
|
|
"rsr/mp3"
|
2021-06-03 19:07:27 +02:00
|
|
|
"rsr/util"
|
|
|
|
"rsr/vorbis"
|
|
|
|
)
|
|
|
|
|
2021-06-21 20:37:22 +02:00
|
|
|
var client = new(http.Client)
|
|
|
|
|
|
|
|
const (
|
|
|
|
colRed = "\033[31m"
|
2021-06-03 19:07:27 +02:00
|
|
|
colYellow = "\033[33m"
|
2021-06-21 20:37:22 +02:00
|
|
|
colReset = "\033[m"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nTracksRecorded int // Number of recorded tracks.
|
2021-06-21 20:40:00 +02:00
|
|
|
limitTracks bool
|
|
|
|
maxTracks int
|
2021-06-03 19:07:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func usage(arg0 string, exitStatus int) {
|
|
|
|
fmt.Fprintln(os.Stderr, `Usage:
|
2021-06-21 20:37:22 +02:00
|
|
|
`+arg0+` [options...] <STREAM_URL>
|
2021-06-03 19:07:27 +02:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-dir <DIRECTORY> -- Output directory (default: ".").
|
2021-06-21 20:37:22 +02:00
|
|
|
-n <NUM> -- Stop after <NUM> tracks.
|
2021-06-03 19:07:27 +02:00
|
|
|
|
|
|
|
Output types:
|
|
|
|
* <INFO>
|
2021-06-21 20:37:22 +02:00
|
|
|
`+colYellow+`! <WARNING>`+colReset+`
|
|
|
|
`+colRed+`! <ERROR>`+colReset)
|
2021-06-03 19:07:27 +02:00
|
|
|
os.Exit(exitStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printInfo(f string, v ...interface{}) {
|
2021-06-21 20:37:22 +02:00
|
|
|
fmt.Printf("* "+f+"\n", v...)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func printWarn(f string, v ...interface{}) {
|
2021-06-21 20:37:22 +02:00
|
|
|
fmt.Fprintf(os.Stderr, colYellow+"! "+f+colReset+"\n", v...)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func printNonFatalErr(f string, v ...interface{}) {
|
2021-06-21 20:37:22 +02:00
|
|
|
fmt.Fprintf(os.Stderr, colRed+"! "+f+colReset+"\n", v...)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func printErr(f string, v ...interface{}) {
|
|
|
|
printNonFatalErr(f, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-06-21 20:37:22 +02:00
|
|
|
func record(url, dir string) {
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
printErr("HTTP request error: %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Add("Icy-MetaData", "1") // Request metadata for icecast mp3 streams.
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
printErr("HTTP error: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var extractor model.Extractor
|
|
|
|
|
|
|
|
// Set up extractor depending on content type.
|
|
|
|
contentType := resp.Header.Get("content-type")
|
|
|
|
err = nil
|
|
|
|
switch contentType {
|
2021-06-22 14:08:38 +02:00
|
|
|
case "application/ogg", "audio/ogg", "audio/vorbis", "audio/vorbis-config":
|
2021-06-21 20:37:22 +02:00
|
|
|
extractor, err = vorbis.NewExtractor()
|
2021-06-22 14:08:38 +02:00
|
|
|
case "audio/mpeg", "audio/MPA", "audio/mpa-robust":
|
2021-06-21 20:37:22 +02:00
|
|
|
extractor, err = mp3.NewExtractor(resp.Header)
|
|
|
|
default:
|
2021-06-22 14:08:38 +02:00
|
|
|
printErr(`Content type '%v' not supported, supported formats:
|
|
|
|
Ogg/Vorbis ('application/ogg', 'audio/ogg', 'audio/vorbis', 'audio/vorbis-config')
|
|
|
|
mp3 ('audio/mpeg', 'audio/MPA', 'audio/mpa-robust')`, contentType)
|
2021-06-21 20:37:22 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
printErr("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
printInfo("Stream type: '%v'", contentType)
|
|
|
|
|
|
|
|
// Make reader blocking.
|
|
|
|
r := util.NewWaitReader(resp.Body)
|
|
|
|
|
|
|
|
// The first track is always discarded, as streams usually don't start at
|
|
|
|
// the exact end of a track, meaning it is almost certainly going to be
|
|
|
|
// incomplete.
|
|
|
|
discard := true
|
|
|
|
|
|
|
|
var rawFile bytes.Buffer
|
|
|
|
var filename string
|
|
|
|
var hasFilename bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
var block bytes.Buffer
|
|
|
|
|
|
|
|
wasFirst, err := extractor.ReadBlock(r, &block)
|
|
|
|
if err != nil {
|
|
|
|
printNonFatalErr("Error reading block: %v", err)
|
|
|
|
// Reconnect, because this error is usually caused by a
|
|
|
|
// file corruption or a network error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if wasFirst &&
|
|
|
|
// We only care about the beginning of a new file when it marks an
|
|
|
|
// old file's end, which is not the case in the beginning of the
|
|
|
|
// first file.
|
|
|
|
rawFile.Len() > 0 {
|
|
|
|
if !discard {
|
|
|
|
// Save previous track.
|
|
|
|
if !hasFilename {
|
|
|
|
printNonFatalErr("Error: Could not get a track filename")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filePath := path.Join(dir, filename)
|
|
|
|
err := os.WriteFile(filePath, rawFile.Bytes(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
printNonFatalErr("Error writing file: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
printInfo("Saved track as: %v", filePath)
|
|
|
|
|
|
|
|
// Stop after the defined number of tracks (if the option was
|
|
|
|
// given).
|
|
|
|
nTracksRecorded++
|
|
|
|
if limitTracks && nTracksRecorded >= maxTracks {
|
|
|
|
printInfo("Successfully recorded %v tracks, exiting", nTracksRecorded)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// See declaration of `discard`.
|
|
|
|
discard = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset everything.
|
|
|
|
rawFile.Reset()
|
|
|
|
hasFilename = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find out the current track's filename.
|
|
|
|
if !hasFilename {
|
|
|
|
if f, ok := extractor.TryGetFilename(); ok {
|
|
|
|
if discard {
|
|
|
|
printInfo("Discarding track: %v", f)
|
|
|
|
} else {
|
|
|
|
printInfo("Recording track: %v", f)
|
|
|
|
}
|
|
|
|
filename = f
|
|
|
|
hasFilename = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append block to the current file byte buffer.
|
|
|
|
rawFile.Write(block.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:07:27 +02:00
|
|
|
func main() {
|
|
|
|
var url string
|
|
|
|
dir := "."
|
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
usage(os.Args[0], 1)
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:21:13 +02:00
|
|
|
// Parse command line arguments.
|
2021-06-03 19:07:27 +02:00
|
|
|
for i := 1; i < len(os.Args); i++ {
|
2021-06-21 20:37:22 +02:00
|
|
|
// Returns the argument after the given option. Errors if there is no
|
|
|
|
// argument.
|
|
|
|
expectArg := func(currArg string) string {
|
|
|
|
i++
|
|
|
|
if i >= len(os.Args) {
|
|
|
|
printErr("Expected argument after option '%v'", currArg)
|
|
|
|
}
|
|
|
|
return os.Args[i]
|
|
|
|
}
|
|
|
|
|
2021-06-03 19:07:27 +02:00
|
|
|
arg := os.Args[i]
|
|
|
|
if len(arg) >= 1 && arg[0] == '-' {
|
2021-06-21 20:37:22 +02:00
|
|
|
switch arg {
|
2021-06-03 19:07:27 +02:00
|
|
|
case "-dir":
|
2021-06-21 20:37:22 +02:00
|
|
|
dir = expectArg(arg)
|
|
|
|
case "-n":
|
|
|
|
nStr := expectArg(arg)
|
|
|
|
n, err := strconv.ParseInt(nStr, 10, 32)
|
|
|
|
if err != nil || n <= 0 {
|
|
|
|
printErr("'%v' is not an integer larger than zero", nStr)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
2021-06-21 20:37:22 +02:00
|
|
|
limitTracks = true
|
|
|
|
maxTracks = int(n)
|
|
|
|
case "--help", "-h":
|
2021-06-03 19:07:27 +02:00
|
|
|
usage(os.Args[0], 0)
|
|
|
|
default:
|
2021-06-21 20:37:22 +02:00
|
|
|
printErr("Unknown option: '%v'", arg)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if url == "" {
|
|
|
|
url = arg
|
|
|
|
} else {
|
2021-06-21 20:37:22 +02:00
|
|
|
printErr("Expected option, but got '%v'", arg)
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if url == "" {
|
|
|
|
printInfo("Please specify a stream URL")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
printInfo("URL: %v", url)
|
|
|
|
printInfo("Output directory: %v", dir)
|
2021-06-21 20:43:57 +02:00
|
|
|
if limitTracks {
|
|
|
|
printInfo("Stopping after %v tracks", maxTracks)
|
|
|
|
}
|
2021-06-03 19:07:27 +02:00
|
|
|
|
2021-06-21 20:37:22 +02:00
|
|
|
// Record the actual stream.
|
2021-06-03 19:07:27 +02:00
|
|
|
for {
|
2021-06-21 20:37:22 +02:00
|
|
|
record(url, dir)
|
|
|
|
printInfo("Reconnecting due to previous error")
|
2021-06-03 19:07:27 +02:00
|
|
|
}
|
|
|
|
}
|