Skip to content

Commit

Permalink
Add option to pass extra args ytdlp
Browse files Browse the repository at this point in the history
  • Loading branch information
fakelag committed Sep 16, 2024
1 parent 6631ea9 commit 82b208d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions youtubeapi/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ type Youtube struct {
executor cmd.CommandExecutor
streamUrlTimeout time.Duration
streamUrlExpireRegex *regexp.Regexp
ytdlpArgs []string
}

func NewYoutubeAPI() *Youtube {
yt := &Youtube{
executor: &cmd.DefaultCommandExecutor{},
streamUrlTimeout: time.Second * 30,
streamUrlExpireRegex: regexp.MustCompile("(expire)(\\/|=)(\\d+)(\\/|=|&|$)"),
ytdlpArgs: make([]string, 0),
}

return yt
Expand All @@ -97,6 +99,10 @@ func (yt *Youtube) SetCmdExecutor(exec cmd.CommandExecutor) {
yt.executor = exec
}

func (yt *Youtube) SetYtDlpArgs(extraArgs []string) {
yt.ytdlpArgs = extraArgs
}

func (yt *Youtube) SearchYoutubeMedia(numSearchResults int, videoIdOrSearchTerm string) ([]*YoutubeMedia, error) {
replacer := strings.NewReplacer(
"\"", "",
Expand All @@ -119,6 +125,10 @@ func (yt *Youtube) SearchYoutubeMedia(numSearchResults int, videoIdOrSearchTerm
"--print-json",
}

if len(yt.ytdlpArgs) > 0 {
args = append(args, yt.ytdlpArgs...)
}

stdout, err := yt.YtDlpExec(yt.streamUrlTimeout, args)

if err != nil {
Expand Down Expand Up @@ -205,6 +215,10 @@ func (yt *Youtube) GetYoutubeMedia(videoIdOrSearchTerm string) (*YoutubeMedia, e
"--print-json",
}

if len(yt.ytdlpArgs) > 0 {
args = append(args, yt.ytdlpArgs...)
}

stdout, err := yt.YtDlpExec(yt.streamUrlTimeout, args)

if err != nil {
Expand Down Expand Up @@ -255,6 +269,10 @@ func (yt *Youtube) GetYoutubePlaylist(playlistIdOrUrl string) (*YoutubePlaylist,
"--flat-playlist",
}

if len(yt.ytdlpArgs) > 0 {
args = append(args, yt.ytdlpArgs...)
}

stdout, err := yt.YtDlpExec(yt.streamUrlTimeout, args)

if err != nil {
Expand Down Expand Up @@ -304,6 +322,10 @@ func (yt *Youtube) ListFormats(videoIdOrUrl string) ([]*YtDlpVideoFormat, error)
"--print-json",
}

if len(yt.ytdlpArgs) > 0 {
args = append(args, yt.ytdlpArgs...)
}

stdout, err := yt.YtDlpExec(yt.streamUrlTimeout, args)

if err != nil {
Expand Down

0 comments on commit 82b208d

Please sign in to comment.