I am trying to use youtube-dl to get the audio URL of a youtube video, send a GET request towards that URL and then use the requests body io.ReadCloser and pass that to https://github.com/faiface/beep decoder.
So when I do this with different encodings I get the following error:
mp3: MPEG version 2.5 is not supported
or
mp3: only layer3 (want 1; got 2) is supported
wav: missing RIFF at the beginning >
flac: flac.parseStreamInfo: invalid FLAC signature; expected "fLaC", got "\x00\x00\x00\x18"
as you can see my knowledge of audio encoding is pretty much non existent. Since these errors dont tell me much.
But from what I have gathered is that its probably mp3 that is the desired format. And that MPEG 2.5 is some kind of proprietary format? And what I probably want is MPEG version 2 or 1? Is that possible with youtube-dl?
Also is there some other way of doing this instead (streaming audio from youtube with GO?)
package main
import (
"log"
"net/http"
"time"
"fmt"
"os/exec"
"strings"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
)
func main() {
audioUrl, err := GetAudioLink(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"mp3",
"140",
)
if err != nil {
log.Fatal(err)
}
req, err := http.Get(audioUrl)
if err != nil {
log.Fatal(err)
}
f := req.Body
streamer, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/60))
done := make(chan bool)
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
done <- true
})))
<-done
}
func GetAudioLink(url, format, youtubedlQuality string) (audioUrl string, err error) {
var cmd []byte
if cmd, err = exec.Command(
"youtube-dl",
"--extract-audio",
"--get-url",
"--audio-format",
format,
"-f",
youtubedlQuality,
url,
).Output(); err != nil {
return audioUrl, fmt.Errorf("calling youtube-dl: %w", err)
}
audioUrl = strings.TrimSpace(string(cmd))
return audioUrl, err
}
Make sure that you are using the master branch of https://github.com/faiface/beep go get github.com/faiface/beep@master
And you need a local installation of youtube-dl. I couldnt find any GO lib for this.