Documentation ¶
Overview ¶
Package youtube implement youtube download package in go.
Index ¶
- Constants
- func ExtractVideoID(videoID string) (string, error)
- type Client
- func (c *Client) GetPlaylist(url string) (*Playlist, error)
- func (c *Client) GetPlaylistContext(ctx context.Context, url string) (*Playlist, error)
- func (c *Client) GetStream(video *Video, format *Format) (io.ReadCloser, int64, error)
- func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *Format) (io.ReadCloser, int64, error)
- func (c *Client) GetStreamURL(video *Video, format *Format) (string, error)
- func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error)
- func (c *Client) GetVideo(url string) (*Video, error)
- func (c *Client) GetVideoContext(ctx context.Context, url string) (*Video, error)
- func (c *Client) VideoFromPlaylistEntry(entry *PlaylistEntry) (*Video, error)
- func (c *Client) VideoFromPlaylistEntryContext(ctx context.Context, entry *PlaylistEntry) (*Video, error)
- type DecipherOperation
- type DecipherOperationsCache
- type ErrPlayabiltyStatus
- type ErrResponseStatus
- type ErrUnexpectedStatusCode
- type Format
- type FormatList
- func (list FormatList) AudioChannels(n int) (result FormatList)
- func (list FormatList) FindByItag(itagNo int) *Format
- func (list FormatList) FindByQuality(quality string) *Format
- func (list FormatList) Quality(quality string) (result FormatList)
- func (list FormatList) Sort()
- func (list FormatList) Type(t string) (result FormatList)
- type Playlist
- type PlaylistEntry
- type SimpleCache
- type Thumbnail
- type Thumbnails
- type Video
Examples ¶
Constants ¶
const ( ErrCipherNotFound = constError("cipher not found") ErrInvalidCharactersInVideoID = constError("invalid characters in video id") ErrVideoIDMinLength = constError("the video id must be at least 10 characters long") ErrReadOnClosedResBody = constError("http: read on closed response body") ErrNotPlayableInEmbed = constError("embedding of this video has been disabled") ErrInvalidPlaylist = constError("no playlist detected or invalid playlist ID") )
Variables ¶
This section is empty.
Functions ¶
func ExtractVideoID ¶
ExtractVideoID extracts the videoID from the given string
Types ¶
type Client ¶
type Client struct { // Debug enables debugging output through log package Debug bool // HTTPClient can be used to set a custom HTTP client. // If not set, http.DefaultClient will be used HTTPClient *http.Client // contains filtered or unexported fields }
Client offers methods to download video metadata and video streams.
Example ¶
ExampleDownload : Example code for how to use this package for download video.
package main import ( "io" "os" youtube "github.com/marstay/youtubedl/v2" ) func main() { videoID := "BaW_jenozKc" client := youtube.Client{} video, err := client.GetVideo(videoID) if err != nil { panic(err) } stream, _, err := client.GetStream(video, &video.Formats[0]) if err != nil { panic(err) } file, err := os.Create("video.mp4") if err != nil { panic(err) } defer file.Close() _, err = io.Copy(file, stream) if err != nil { panic(err) } }
Output:
func (*Client) GetPlaylist ¶
GetPlaylist fetches playlist metadata
func (*Client) GetPlaylistContext ¶
GetPlaylistContext fetches playlist metadata, with a context, along with a list of Videos, and some basic information for these videos. Playlist entries cannot be downloaded, as they lack all the required metadata, but can be used to enumerate all IDs, Authors, Titles, etc.
func (*Client) GetStreamContext ¶
func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *Format) (io.ReadCloser, int64, error)
GetStream returns the stream and the total size for a specific format with a context.
func (*Client) GetStreamURL ¶
GetStreamURL returns the url for a specific format
func (*Client) GetStreamURLContext ¶
func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error)
GetStreamURLContext returns the url for a specific format with a context
func (*Client) GetVideoContext ¶
GetVideoContext fetches video metadata with a context
func (*Client) VideoFromPlaylistEntry ¶
func (c *Client) VideoFromPlaylistEntry(entry *PlaylistEntry) (*Video, error)
func (*Client) VideoFromPlaylistEntryContext ¶
type DecipherOperation ¶
type DecipherOperationsCache ¶
type DecipherOperationsCache interface { Get(videoID string) []DecipherOperation Set(video string, operations []DecipherOperation) }
type ErrPlayabiltyStatus ¶
func (ErrPlayabiltyStatus) Error ¶
func (err ErrPlayabiltyStatus) Error() string
type ErrResponseStatus ¶
func (ErrResponseStatus) Error ¶
func (err ErrResponseStatus) Error() string
type ErrUnexpectedStatusCode ¶
type ErrUnexpectedStatusCode int
ErrUnexpectedStatusCode is returned on unexpected HTTP status codes
func (ErrUnexpectedStatusCode) Error ¶
func (err ErrUnexpectedStatusCode) Error() string
type Format ¶
type Format struct { ItagNo int `json:"itag"` URL string `json:"url"` MimeType string `json:"mimeType"` Quality string `json:"quality"` Cipher string `json:"signatureCipher"` Bitrate int `json:"bitrate"` FPS int `json:"fps"` Width int `json:"width"` Height int `json:"height"` LastModified string `json:"lastModified"` ContentLength int64 `json:"contentLength,string"` QualityLabel string `json:"qualityLabel"` ProjectionType string `json:"projectionType"` AverageBitrate int `json:"averageBitrate"` AudioQuality string `json:"audioQuality"` ApproxDurationMs string `json:"approxDurationMs"` AudioSampleRate string `json:"audioSampleRate"` AudioChannels int `json:"audioChannels"` // InitRange is only available for adaptive formats InitRange *struct { Start string `json:"start"` End string `json:"end"` } `json:"initRange"` // IndexRange is only available for adaptive formats IndexRange *struct { Start string `json:"start"` End string `json:"end"` } `json:"indexRange"` }
type FormatList ¶
type FormatList []Format
func (FormatList) AudioChannels ¶
func (list FormatList) AudioChannels(n int) (result FormatList)
AudioChannels returns a new FormatList filtered by the matching AudioChannels
func (FormatList) FindByItag ¶
func (list FormatList) FindByItag(itagNo int) *Format
FindByItag returns the first format matching the itag number
func (FormatList) FindByQuality ¶
func (list FormatList) FindByQuality(quality string) *Format
FindByQuality returns the first format matching Quality or QualityLabel
func (FormatList) Quality ¶
func (list FormatList) Quality(quality string) (result FormatList)
Quality returns a new FormatList filtered by quality, quality label or itag, but not audio quality
func (FormatList) Type ¶
func (list FormatList) Type(t string) (result FormatList)
Type returns a new FormatList filtered by mime type of video
type Playlist ¶
type Playlist struct { ID string Title string Author string Videos []*PlaylistEntry }
Example ¶
Example usage for playlists: downloading and checking information.
package main import ( "fmt" "io" "os" "strings" youtube "github.com/marstay/youtubedl/v2" ) func main() { playlistID := "PLQZgI7en5XEgM0L1_ZcKmEzxW1sCOVZwP" client := youtube.Client{} playlist, err := client.GetPlaylist(playlistID) if err != nil { panic(err) } /* ----- Enumerating playlist videos ----- */ header := fmt.Sprintf("Playlist %s by %s", playlist.Title, playlist.Author) println(header) println(strings.Repeat("=", len(header)) + "\n") for k, v := range playlist.Videos { fmt.Printf("(%d) %s - '%s'\n", k+1, v.Author, v.Title) } /* ----- Downloading the 1st video ----- */ entry := playlist.Videos[0] video, err := client.VideoFromPlaylistEntry(entry) if err != nil { panic(err) } // Now it's fully loaded. fmt.Printf("Downloading %s by '%s'!\n", video.Title, video.Author) stream, _, err := client.GetStream(video, &video.Formats[0]) if err != nil { panic(err) } file, err := os.Create("video.mp4") if err != nil { panic(err) } defer file.Close() _, err = io.Copy(file, stream) if err != nil { panic(err) } println("Downloaded /video.mp4") }
Output:
func (*Playlist) UnmarshalJSON ¶
type PlaylistEntry ¶
type SimpleCache ¶
type SimpleCache struct {
// contains filtered or unexported fields
}
func NewSimpleCache ¶
func NewSimpleCache() *SimpleCache
func (SimpleCache) Get ¶
func (s SimpleCache) Get(videoID string) []DecipherOperation
Get : get cache when it has same video id and not expired
func (SimpleCache) GetCacheBefore ¶
func (s SimpleCache) GetCacheBefore(videoID string, time time.Time) []DecipherOperation
GetCacheBefore : can pass time for testing
func (*SimpleCache) Set ¶
func (s *SimpleCache) Set(videoID string, operations []DecipherOperation)
Set : set cache with default expiration
type Thumbnails ¶
type Thumbnails []Thumbnail
type Video ¶
type Video struct { ID string Title string Description string Author string Duration time.Duration PublishDate time.Time Formats FormatList Thumbnails Thumbnails DASHManifestURL string // URI of the DASH manifest file HLSManifestURL string // URI of the HLS manifest file }
func (*Video) FilterQuality ¶
FilterQuality reduces the format list to formats matching the quality