poc: introduce AudioDownloader

This commit is contained in:
Rob Watson 2021-09-05 11:49:59 +02:00
parent b1ce4bc2b0
commit 3955a64d9f
2 changed files with 94 additions and 32 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
*.m4a /backend/cache/

View File

@ -3,8 +3,12 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
"io"
"log" "log"
"os"
"os/exec" "os/exec"
"strconv"
"github.com/kkdai/youtube/v2" "github.com/kkdai/youtube/v2"
) )
@ -12,49 +16,107 @@ import (
const ( const (
ItagM4AAudio = 140 ItagM4AAudio = 140
SizeOfInt16 = 2 SizeOfInt16 = 2
DefaultAudioCodec = "pcm_s16le"
DefaultFormat = "s16le"
DefaultSampleRate = 44100
) )
// TODO: process as stream to avoid loading full buffer in memory type byteToIntConverter struct {
callback func(p []uint16)
buf []uint16
}
func main() { func (w byteToIntConverter) Write(p []byte) (int, error) {
var client youtube.Client if len(p)%2 != 0 {
panic("need to handle odd number of bytes")
videoID := "s_oJYdRlrv0"
video, err := client.GetVideo(videoID)
if err != nil {
log.Fatal(err)
} }
format := video.Formats.FindByItag(ItagM4AAudio) // grow the internal buffer if necessary:
stream, _, err := client.GetStream(video, format) exp := len(p) / SizeOfInt16
if err != nil { if len(w.buf) < exp {
log.Fatal(err) w.buf = append(w.buf, make([]uint16, exp)...)
}
for i := 0; i < len(p); i += SizeOfInt16 {
w.buf[i/2] = binary.LittleEndian.Uint16(p[i : i+SizeOfInt16])
} }
// TODO: force 44.1khz w.callback(w.buf)
cmd := exec.Command("ffmpeg", "-i", "-", "-f", "s16le", "-acodec", "pcm_s16le", "-")
cmd.Stdin = stream
var out bytes.Buffer return len(p), nil
cmd.Stdout = &out }
type AudioDownloader struct {
videoID string
ytClient youtube.Client
}
func NewAudioDownloader(videoID string) *AudioDownloader {
return &AudioDownloader{videoID: videoID}
}
func (d *AudioDownloader) ReadInts(fn func(p []uint16)) error {
cachePath := fmt.Sprintf("cache/%s.raw", d.videoID)
var reader io.Reader
fetch := true
if _, err := os.Stat(cachePath); err == nil {
if fptr, err := os.Open(cachePath); err == nil {
defer fptr.Close()
reader = fptr
fetch = false
} else {
log.Printf("warning: error opening cache file: %v", err)
}
}
if fetch {
log.Println("fetching audio stream from youtube...")
video, err := d.ytClient.GetVideo(d.videoID)
if err != nil {
return fmt.Errorf("error fetching video: %v", err)
}
format := video.Formats.FindByItag(ItagM4AAudio)
stream, _, err := d.ytClient.GetStream(video, format)
if err != nil {
return fmt.Errorf("error fetching stream: %v", err)
}
cacheFile, err := os.Create(cachePath)
if err != nil {
return fmt.Errorf("error creating cache file: %v", err)
}
reader = io.TeeReader(stream, cacheFile)
}
var errOut bytes.Buffer var errOut bytes.Buffer
cmd := exec.Command("ffmpeg", "-i", "-", "-f", DefaultFormat, "-ar", strconv.Itoa(DefaultSampleRate), "-acodec", DefaultAudioCodec, "-")
cmd.Stdin = reader
cmd.Stdout = byteToIntConverter{callback: fn}
cmd.Stderr = &errOut cmd.Stderr = &errOut
if err = cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
log.Fatalf("err = %v, stdErr = %s", err, errOut.String()) log.Println(errOut.String())
return fmt.Errorf("error calling ffmpeg: %v", err)
} }
log.Printf("byteLen = %d", out.Len()) return nil
}
// TODO: reflect.Slice to avoid copying?
data := make([]uint16, 0, out.Len()/SizeOfInt16) func main() {
for i := 0; i < out.Len(); i += SizeOfInt16 { videoID := "s_oJYdRlrv0"
v := binary.LittleEndian.Uint16(out.Bytes()[i : i+SizeOfInt16]) downloader := NewAudioDownloader(videoID)
data = append(data, v) err := downloader.ReadInts(func(p []uint16) {
} // log.Printf("ints = %+v", p[0:16])
})
log.Printf("intLen = %d", len(data))
log.Printf("ints = %+v", data[0:8192]) if err != nil {
log.Fatalf("error calling downloader: %v", err)
}
log.Println("done")
} }