Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added yt-dlp #1683

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
18 changes: 18 additions & 0 deletions pkg/actions/tools/ytdlp/extractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ytdlp

import (
"strings"

"github.com/rsteube/carapace"
)

// ActionExtractors completes extractors
//
// 1News
// 1tv
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]...)
})
}
118 changes: 118 additions & 0 deletions pkg/actions/tools/ytdlp/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package ytdlp

import (
"fmt"
"math"
"strings"

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

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

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

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

// ActionVideoFormats completes video formats
//
// avi
// flv
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",
)
}

// ActionFormats completes formats
//
// 133 (426x240 - avc1 - none - 0)
// 134 (640x360 - avc1 - none - 0)
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...)
})
}

// ActionSubtitles completes subtitles
//
// en (English)
// fr (French)
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...)
})
}
24 changes: 24 additions & 0 deletions pkg/actions/tools/ytdlp/sponsorblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ytdlp

import "github.com/rsteube/carapace"

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