clipper/backend/cmd/ytdebug/main.go

98 lines
2.0 KiB
Go
Raw Normal View History

package main
import (
"context"
"flag"
"fmt"
2021-10-08 14:38:35 +00:00
"io"
"log"
2021-10-08 14:38:35 +00:00
"net/http"
"os"
"strings"
"sync"
"time"
2021-10-28 01:23:45 +00:00
"git.netflux.io/rob/clipper/media"
"git.netflux.io/rob/clipper/youtube"
youtubev2 "github.com/kkdai/youtube/v2"
)
func main() {
var (
2021-10-08 14:38:35 +00:00
verbose bool
printMode bool
downloadMode bool
audioOnly bool
videoOnly bool
)
flag.BoolVar(&verbose, "v", false, "verbose output")
2021-10-08 14:38:35 +00:00
flag.BoolVar(&printMode, "print", true, "print format info")
flag.BoolVar(&downloadMode, "download", false, "download all media to ./debug")
flag.BoolVar(&audioOnly, "audio", false, "only print audio formats")
flag.BoolVar(&videoOnly, "video", false, "only print video formats")
flag.Parse()
videoID := flag.Arg(0)
ctx := context.Background()
var youtubeClient youtubev2.Client
video, err := youtubeClient.GetVideoContext(ctx, videoID)
if err != nil {
log.Fatal(err)
}
formats := video.Formats
2021-10-08 14:38:35 +00:00
if downloadMode {
downloadAll(formats)
return
}
switch {
case audioOnly:
2021-10-28 01:23:45 +00:00
formats = media.FilterYoutubeAudio(formats)
case videoOnly:
2021-10-28 01:23:45 +00:00
formats = media.FilterYoutubeVideo(formats)
}
fmt.Println("In descending order of preference:")
for n, f := range formats {
fmt.Printf("%d: %s\n", n+1, youtube.FormatDebugString(&f, verbose))
}
}
2021-10-08 14:38:35 +00:00
func downloadAll(formats youtubev2.FormatList) {
var wg sync.WaitGroup
for i := range formats {
format := formats[i]
wg.Add(1)
go func() {
defer wg.Done()
start := time.Now()
outpath := fmt.Sprintf("./debug/%s.%s-itag-%d", strings.ReplaceAll(format.MimeType, "/", "'"), format.Quality, format.ItagNo)
output, err := os.Create(outpath)
if err != nil {
log.Fatalf("error opening output file: %v", err)
}
resp, err := http.Get(format.URL)
if err != nil {
log.Fatalf("error fetching media: %v", err)
}
defer resp.Body.Close()
n, err := io.Copy(output, resp.Body)
if err != nil {
log.Fatalf("error reading media: %v", err)
}
dur := time.Since(start)
log.Printf("downloaded itag %d, %d bytes in %v secs", format.ItagNo, n, dur.Seconds())
}()
}
wg.Wait()
}