Skip to content

Commit

Permalink
added yt-dlp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed May 8, 2023
1 parent 6c5f525 commit 5833ad1
Show file tree
Hide file tree
Showing 6 changed files with 540 additions and 0 deletions.
326 changes: 326 additions & 0 deletions completers/yt-dlp_completer/cmd/root.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions completers/yt-dlp_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/yt-dlp_completer/cmd"

func main() {
cmd.Execute()
}
79 changes: 79 additions & 0 deletions pkg/actions/tools/ytdlp/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ytdlp

import (
"encoding/json"

"github.com/rsteube/carapace"
)

type dump struct {
ID string `json:"id"`
Title string `json:"title"`
Formats []struct {
FormatID string `json:"format_id"`
FormatNote string `json:"format_note"`
Ext string `json:"ext"`
Protocol string `json:"protocol"`
Acodec string `json:"acodec"`
Vcodec string `json:"vcodec"`
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
Fps float64 `json:"fps"`
Rows int `json:"rows,omitempty"`
Columns int `json:"columns,omitempty"`
Fragments []struct {
URL string `json:"url"`
Duration float64 `json:"duration"`
} `json:"fragments,omitempty"`
Resolution string `json:"resolution"`
AspectRatio float64 `json:"aspect_ratio"`
HTTPHeaders struct {
UserAgent string `json:"User-Agent"`
Accept string `json:"Accept"`
AcceptLanguage string `json:"Accept-Language"`
SecFetchMode string `json:"Sec-Fetch-Mode"`
} `json:"http_headers"`
AudioExt string `json:"audio_ext"`
VideoExt string `json:"video_ext"`
Format string `json:"format"`
Asr int `json:"asr,omitempty"`
Filesize int `json:"filesize,omitempty"`
SourcePreference int `json:"source_preference,omitempty"`
AudioChannels int `json:"audio_channels,omitempty"`
Quality float64 `json:"quality,omitempty"`
HasDrm bool `json:"has_drm,omitempty"`
Tbr float64 `json:"tbr,omitempty"`
Language any `json:"language,omitempty"`
LanguagePreference int `json:"language_preference,omitempty"`
Preference any `json:"preference,omitempty"`
DynamicRange any `json:"dynamic_range,omitempty"`
Abr float64 `json:"abr,omitempty"`
Container string `json:"container,omitempty"`
Vbr float64 `json:"vbr,omitempty"`
FilesizeApprox int `json:"filesize_approx,omitempty"`
} `json:"formats"`
Thumbnails []struct {
URL string `json:"url"`
Preference int `json:"preference"`
ID string `json:"id"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
Resolution string `json:"resolution,omitempty"`
} `json:"thumbnails"`
Subtitles map[string][]struct {
Ext string `json:"ext"`
URL string `json:"url"`
Name string `json:"name"`
} `json:"subtitles"`
}

func actionDump(url string, f func(d dump) carapace.Action) carapace.Action {
return carapace.ActionExecCommand("yt-dlp", "--dump-json", url)(func(output []byte) carapace.Action {
var d dump
if err := json.Unmarshal(output, &d); err != nil {
return carapace.ActionMessage(err.Error())
}
return f(d)
})
}
14 changes: 14 additions & 0 deletions pkg/actions/tools/ytdlp/extractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ytdlp

import (
"strings"

"github.com/rsteube/carapace"
)

func ActionExtractors() carapace.Action {
return carapace.ActionExecCommand("yt-dlp", "--list-extractors")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValues(lines[:len(lines)-1]...)
})
}
94 changes: 94 additions & 0 deletions pkg/actions/tools/ytdlp/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ytdlp

import (
"fmt"
"math"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
)

func ActionOutputFormats() carapace.Action {
return carapace.ActionValues(
"avi",
"flv",
"mkv",
"mov",
"mp4",
"webm",
)
}

func ActionSubtitleFormats() carapace.Action {
return carapace.ActionValues(
"ass",
"lrc",
"srt",
"vtt",
)
}

func ActionThumbnailFormats() carapace.Action {
return carapace.ActionValues(
"jpg",
"png",
"webp",
)
}

func ActionVideoFormats() carapace.Action {
return carapace.ActionValues(
"avi",
"flv",
"gif",
"mkv",
"mov",
"mp4",
"webm",
"aac",
"aiff",
"alac",
"flac",
"m4a",
"mka",
"mp3",
"ogg",
"opus",
"vorbis",
"wav",
)
}

func ActionFormats(url string) carapace.Action {
return actionDump(url, func(d dump) carapace.Action {
vals := make([]string, 0)
for _, format := range d.Formats {
description := fmt.Sprintf("%v - %v - %v - %v",
format.Resolution,
strings.Split(format.Vcodec, ".")[0],
strings.Split(format.Acodec, ".")[0],
math.Round(format.Abr),
)

s := style.Default
switch {
case format.Vcodec == "none":
s = style.Yellow
}

vals = append(vals, format.FormatID, description, s) // TODO enrich description
}
return carapace.ActionStyledValuesDescribed(vals...)
})
}

func ActionSubtitles(url string) carapace.Action {
return actionDump(url, func(d dump) carapace.Action {
vals := make([]string, 0)
for id, s := range d.Subtitles {
vals = append(vals, id, s[0].Name)
}
return carapace.ActionValuesDescribed(vals...)
})
}
20 changes: 20 additions & 0 deletions pkg/actions/tools/ytdlp/sponsorblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ytdlp

import "github.com/rsteube/carapace"

func ActionSponsorblockCategories() carapace.Action {
return carapace.ActionValues(
"sponsor",
"intro",
"outro",
"selfpromo",
"preview",
"filler",
"interaction",
"music_offtopic",
"poi_highlight",
"chapter",
"all",
"default",
)
}

0 comments on commit 5833ad1

Please sign in to comment.