Skip to content

Commit

Permalink
fix: put URL in quotes for yt-dlp command (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 committed Sep 27, 2023
1 parent bddf496 commit 9301d4b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
46 changes: 46 additions & 0 deletions cmd/airplay-local/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"fmt"

"github.com/Trojan295/discord-airplay/pkg/codec"
"github.com/Trojan295/discord-airplay/pkg/sources"
"go.uber.org/zap"
)

var (
logger *zap.Logger
ctx context.Context
youtubeFetcher *sources.YoutubeFetcher
)

func main() {
logger, _ = zap.NewProduction()
defer logger.Sync()

ctx = context.Background()
youtubeFetcher = sources.NewYoutubeFetcher()

songs, err := youtubeFetcher.LookupSongs(ctx, "https://www.youtube.com/watch?v=AjiSzMFEDgo&pp=ygUNZGVhZG1hdTUgbGl2ZQ%3D%3D")
//songs, err := youtubeFetcher.LookupSongs(ctx, "https://www.youtube.com/watch?v=AjiSzMFEDgo")
if err != nil {
logger.Fatal("failed to get songs", zap.Error(err))
}

logger.Info("got songs", zap.Any("songs", songs))

reader, err := youtubeFetcher.GetDCAData(ctx, songs[0])
if err != nil {
logger.Fatal("failed to get dca data", zap.Error(err))
}

opusChan := make(chan []byte, 10)
go func() {
codec.StreamDCAData(ctx, reader, opusChan, nil)
close(opusChan)
}()
for data := range opusChan {
fmt.Println(data)
}
}
22 changes: 11 additions & 11 deletions pkg/sources/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,23 @@ func (s *YoutubeFetcher) GetDCAData(ctx context.Context, song *bot.Song) (io.Rea
go func(w io.WriteCloser) {
defer w.Close()

ytArgs := s.getYTdlpGetDataArgs(song)
ytCmd := strings.Join(append([]string{"yt-dlp"}, ytArgs...), " ")
ytArgs := []string{"-x", "-o", "-", "--force-overwrites", "'" + song.URL + "'"}

seekArg := ""
ffmpegArgs := []string{"-i", "pipe:0"}
if song.StartPosition > 0 {
seekArg = fmt.Sprintf("-ss %d", int64(song.StartPosition.Seconds()))
ffmpegArgs = append(ffmpegArgs, "-ss", song.StartPosition.String())
}
dcaCmd := exec.CommandContext(ctx, "sh", "-c", fmt.Sprintf("%s | ffmpeg -i pipe: %s -f s16le -ar 48000 -ac 2 pipe:1 | dca", ytCmd, seekArg))
ffmpegArgs = append(ffmpegArgs, "-f", "s16le", "-ar", "48000", "-ac", "2", "pipe:1")

downloadCmd := exec.CommandContext(ctx,
"sh", "-c", fmt.Sprintf("yt-dlp %s | ffmpeg %s | dca",
strings.Join(ytArgs, " "),
strings.Join(ffmpegArgs, " ")))

bw := bufio.NewWriterSize(writer, downloadBuffer)
dcaCmd.Stdout = bw
downloadCmd.Stdout = bw

if err := dcaCmd.Run(); err != nil {
if err := downloadCmd.Run(); err != nil {
log.Printf("while executing get DCA data pipe: %v", err)
}

Expand All @@ -97,7 +101,3 @@ func (s *YoutubeFetcher) GetDCAData(ctx context.Context, song *bot.Song) (io.Rea

return reader, nil
}

func (s *YoutubeFetcher) getYTdlpGetDataArgs(song *bot.Song) []string {
return []string{"-x", "-o", "-", "--force-overwrites", song.URL}
}

0 comments on commit 9301d4b

Please sign in to comment.