Refactor, add HTTP server
This commit is contained in:
parent
3955a64d9f
commit
622c7aeb00
|
@ -1 +1,2 @@
|
||||||
|
*.m4a
|
||||||
/backend/cache/
|
/backend/cache/
|
||||||
|
|
152
backend/main.go
152
backend/main.go
|
@ -1,70 +1,41 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"strings"
|
||||||
"strconv"
|
"time"
|
||||||
|
|
||||||
"github.com/kkdai/youtube/v2"
|
"github.com/kkdai/youtube/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ItagM4AAudio = 140
|
ContentTypeAudioM4A = "audio/m4a"
|
||||||
SizeOfInt16 = 2
|
ItagM4AAudio = 140
|
||||||
|
DefaultHTTPBindAddr = "0.0.0.0:8888"
|
||||||
DefaultAudioCodec = "pcm_s16le"
|
DefaultTimeout = 5 * time.Second
|
||||||
DefaultFormat = "s16le"
|
|
||||||
DefaultSampleRate = 44100
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type byteToIntConverter struct {
|
|
||||||
callback func(p []uint16)
|
|
||||||
buf []uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w byteToIntConverter) Write(p []byte) (int, error) {
|
|
||||||
if len(p)%2 != 0 {
|
|
||||||
panic("need to handle odd number of bytes")
|
|
||||||
}
|
|
||||||
|
|
||||||
// grow the internal buffer if necessary:
|
|
||||||
exp := len(p) / SizeOfInt16
|
|
||||||
if len(w.buf) < exp {
|
|
||||||
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])
|
|
||||||
}
|
|
||||||
|
|
||||||
w.callback(w.buf)
|
|
||||||
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type AudioDownloader struct {
|
type AudioDownloader struct {
|
||||||
videoID string
|
videoID string
|
||||||
ytClient youtube.Client
|
ytClient youtube.Client
|
||||||
|
reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAudioDownloader(videoID string) *AudioDownloader {
|
func NewAudioDownloader(videoID string) (*AudioDownloader, error) {
|
||||||
return &AudioDownloader{videoID: videoID}
|
var (
|
||||||
}
|
reader io.Reader
|
||||||
|
ytClient youtube.Client
|
||||||
func (d *AudioDownloader) ReadInts(fn func(p []uint16)) error {
|
)
|
||||||
cachePath := fmt.Sprintf("cache/%s.raw", d.videoID)
|
|
||||||
|
|
||||||
var reader io.Reader
|
|
||||||
|
|
||||||
|
cachePath := fmt.Sprintf("cache/%s.m4a", videoID)
|
||||||
fetch := true
|
fetch := true
|
||||||
|
|
||||||
if _, err := os.Stat(cachePath); err == nil {
|
if _, err := os.Stat(cachePath); err == nil {
|
||||||
if fptr, err := os.Open(cachePath); err == nil {
|
if fptr, err := os.Open(cachePath); err == nil {
|
||||||
defer fptr.Close()
|
|
||||||
reader = fptr
|
reader = fptr
|
||||||
fetch = false
|
fetch = false
|
||||||
} else {
|
} else {
|
||||||
|
@ -75,48 +46,93 @@ func (d *AudioDownloader) ReadInts(fn func(p []uint16)) error {
|
||||||
if fetch {
|
if fetch {
|
||||||
log.Println("fetching audio stream from youtube...")
|
log.Println("fetching audio stream from youtube...")
|
||||||
|
|
||||||
video, err := d.ytClient.GetVideo(d.videoID)
|
video, err := ytClient.GetVideo(videoID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error fetching video: %v", err)
|
return nil, fmt.Errorf("error fetching video: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
format := video.Formats.FindByItag(ItagM4AAudio)
|
format := video.Formats.FindByItag(ItagM4AAudio)
|
||||||
stream, _, err := d.ytClient.GetStream(video, format)
|
log.Printf("M4A format expected to contain %d bytes", format.ContentLength)
|
||||||
|
stream, _, err := ytClient.GetStream(video, format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error fetching stream: %v", err)
|
return nil, fmt.Errorf("error fetching stream: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: only allow the cached file to be accessed after it has been
|
||||||
|
// successfully downloaded.
|
||||||
cacheFile, err := os.Create(cachePath)
|
cacheFile, err := os.Create(cachePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating cache file: %v", err)
|
return nil, fmt.Errorf("error creating cache file: %v", err)
|
||||||
}
|
}
|
||||||
reader = io.TeeReader(stream, cacheFile)
|
reader = io.TeeReader(stream, cacheFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
var errOut bytes.Buffer
|
return &AudioDownloader{
|
||||||
cmd := exec.Command("ffmpeg", "-i", "-", "-f", DefaultFormat, "-ar", strconv.Itoa(DefaultSampleRate), "-acodec", DefaultAudioCodec, "-")
|
videoID: videoID,
|
||||||
cmd.Stdin = reader
|
ytClient: ytClient,
|
||||||
cmd.Stdout = byteToIntConverter{callback: fn}
|
reader: reader,
|
||||||
cmd.Stderr = &errOut
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
func (d *AudioDownloader) Read(p []byte) (int, error) {
|
||||||
log.Println(errOut.String())
|
return d.reader.Read(p)
|
||||||
return fmt.Errorf("error calling ffmpeg: %v", err)
|
}
|
||||||
|
|
||||||
|
func (d *AudioDownloader) Close() error {
|
||||||
|
if rc, ok := d.reader.(io.ReadCloser); ok {
|
||||||
|
return rc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
videoID := "s_oJYdRlrv0"
|
if r.Method != http.MethodGet {
|
||||||
downloader := NewAudioDownloader(videoID)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
err := downloader.ReadInts(func(p []uint16) {
|
w.Write([]byte("method not allowed"))
|
||||||
// log.Printf("ints = %+v", p[0:16])
|
return
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error calling downloader: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("done")
|
if !strings.HasPrefix(r.URL.Path, "/api/audio") {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
w.Write([]byte("page not found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
videoID := r.URL.Query().Get("video_id")
|
||||||
|
if videoID == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
w.Write([]byte("no video ID provided"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
downloader, err := NewAudioDownloader(videoID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("downloader error: %v", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte("could not download video"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer downloader.Close()
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", ContentTypeAudioM4A)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
n, err := io.Copy(w, downloader)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error writing response: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("wrote %d bytes for video ID %s", n, videoID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
srv := http.Server{
|
||||||
|
ReadTimeout: DefaultTimeout,
|
||||||
|
WriteTimeout: DefaultTimeout,
|
||||||
|
Addr: DefaultHTTPBindAddr,
|
||||||
|
Handler: http.HandlerFunc(handleRequest),
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatal(srv.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue