ytdl

package module
v0.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2020 License: MIT Imports: 17 Imported by: 0

README

ytdl

Go library for downloading YouTube videos

Documentation Build Status Codecov Go ReportCard

Example

package main

import (
	"context"
	"os"

	"github.com/rylio/ytdl"
)

func main() {
	ctx := context.Background()
	client := ytdl.DefaultClient

	videoInfo, err := client.GetVideoInfo(ctx, "https://www.youtube.com/watch?v=WkVvG4QTO9M")
	if err != nil {
		panic(err)
	}

	file, err := os.Create(videoInfo.Title + ".mp4")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	err = client.Download(ctx, videoInfo, videoInfo.Formats[0], file)
	if err != nil {
		panic(err)
	}
}

ytdl CLI

  • To install: go get -u github.com/rylio/ytdl/...

  • Or use Docker image docker pull brucewangno1/ytdl:1.0

Usage
  • ytdl [global options] [youtube url or video id]
  • Or using Docker: docker run -it --rm -v /directory/you/want/to/save/the/download/:/ytdl/ brucewangno1/ytdl:1.0 [global options] "[youtube url or video id]"
Options
  • --help, -h - show help
  • --filter, -f - Filter out formats
    • Syntax: -f key:value1,value2,...,valueN
      • Shortcuts for best/worst(e.g. -f best)
        • best/worst - best/worst video and audio
        • best-video/worst-video - best/worst video
        • best-fps/worst-fps - best/worst video with fps as the first priority
        • best-audio/worst-audio - best/worst audio
    • To exclude: -f !key:value1,...
    • Available keys (See format.go for available values):
      • ext - extension of video
      • res - resolution of video
      • videnc - video encoding
      • audenc - audio encoding
      • prof - youtube video profile
      • audbr - audio bitrate
    • Default filters
      • ext:mp4
      • !videnc:
      • !audenc:
      • best
  • --output, -o - Output to specific path
    • Supports templates, ex: {{.Title}}.{{.Ext}}
    • Defaults to {{.Title}}.{{.Ext}}
    • Supported template variables are Title, Ext, DatePublished, Resolution
    • Pass - to output to stdout, former stdout output is redirected to stderr
  • --info, -i - Just gets video info, outputs to stdout
  • --silent, -s - Disables all output, except for fatal errors
  • --debug, -d - Output debug logs
  • --append, -a - append to output file, instead of truncating
  • --range, -r - specify a range of bytes, placed in http range header, ex: 0-100
  • --download-url, -u - just print download url to, don't do anything else
  • --version, -v - print out ytdl cli version
  • --start-offset - offset the beginning of the video by a duration of time(e.g. 20s or 1m)
  • --download-option, -p - Print video and audio download options and accept input interactively
Examples

Download content based on itag

ytdl -f itag:22 'https://www.youtube.com/watch?v=9bZkp7q19f0'

Download content with the best fps

ytdl -f best-fps 'https://www.youtube.com/watch?v=9bZkp7q19f0'

Get all download formats (Requires jq to be installed)

./ytdl -j 'http://youtube.com/watch?v=9bZkp7q19f0' | jq ".formats"

Extract title of the video (Requires jq to be installed)

ytdl -j 'http://youtube.com/watch?v=9bZkp7q19f0' | jq ".title"

Print download url without downloading the content

ytdl -f itag:22 --download-url 'https://www.youtube.com/watch?v=9bZkp7q19f0'

Print video and audio download options and accept input interactively

ytdl -p 'https://www.youtube.com/watch?v=9bZkp7q19f0'

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

ytdl is released under the MIT License, see LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &Client{
	HTTPClient: http.DefaultClient,
	Logger:     zerolog.Nop(),
}
View Source
var ITAGS = generateItags()

ITAGS is a map of all itags and their attributes

Functions

This section is empty.

Types

type Client added in v0.6.3

type Client struct {
	Logger     zerolog.Logger
	HTTPClient *http.Client
}

func (*Client) Download added in v0.6.3

func (c *Client) Download(cx context.Context, info *VideoInfo, format *Format, dest io.Writer) error

Download is a convenience method to download a format to an io.Writer

func (*Client) GetDownloadURL added in v0.6.3

func (c *Client) GetDownloadURL(cx context.Context, info *VideoInfo, format *Format) (*url.URL, error)

GetDownloadURL gets the download url for a format

func (*Client) GetVideoInfo added in v0.6.3

func (c *Client) GetVideoInfo(cx context.Context, value interface{}) (*VideoInfo, error)

GetVideoInfo fetches info from a url string, url object, or a url string

func (*Client) GetVideoInfoFromID added in v0.6.3

func (c *Client) GetVideoInfoFromID(cx context.Context, id string) (*VideoInfo, error)

GetVideoInfoFromID fetches video info from a youtube video id

type Format

type Format struct {
	Itag
	Adaptive bool
	// FromDASH indicates that the stream
	// was extracted from the DASH manifest file
	FromDASH bool
	Index    *Range
	Init     *Range
	// contains filtered or unexported fields
}

func (*Format) CompareKey added in v0.5.0

func (f *Format) CompareKey(other *Format, key FormatKey) int

func (*Format) ValueForKey added in v0.4.0

func (f *Format) ValueForKey(key FormatKey) interface{}

ValueForKey gets the format value for a format key, used for filtering

type FormatKey

type FormatKey string

FormatKey is a string type containing a key in a video format map

const (
	FormatExtensionKey     FormatKey = "ext"
	FormatResolutionKey    FormatKey = "res"
	FormatVideoEncodingKey FormatKey = "videnc"
	FormatAudioEncodingKey FormatKey = "audenc"
	FormatItagKey          FormatKey = "itag"
	FormatAudioBitrateKey  FormatKey = "audbr"
	FormatFPSKey           FormatKey = "fps"
)

Available format Keys

type FormatList added in v0.5.0

type FormatList []*Format

FormatList is a slice of formats with filtering functionality

func (FormatList) Best added in v0.5.0

func (formats FormatList) Best(key FormatKey) FormatList

func (FormatList) Copy added in v0.5.0

func (formats FormatList) Copy() FormatList

func (FormatList) Extremes added in v0.5.0

func (formats FormatList) Extremes(key FormatKey, best bool) FormatList

func (FormatList) Filter added in v0.5.0

func (formats FormatList) Filter(key FormatKey, values []interface{}) FormatList

func (FormatList) Sort added in v0.5.0

func (formats FormatList) Sort(key FormatKey, reverse bool)

func (FormatList) Subtract added in v0.5.0

func (formats FormatList) Subtract(other FormatList) FormatList

func (FormatList) Worst added in v0.5.0

func (formats FormatList) Worst(key FormatKey) FormatList

type Itag added in v0.6.2

type Itag struct {
	Number        int
	Extension     string
	Resolution    string
	VideoEncoding string
	AudioEncoding string
	AudioBitrate  int
	FPS           int // FPS are frames per second
}

Itag is a youtube is a static youtube video format

type Range added in v0.6.3

type Range struct {
	Start string `json:"start"`
	End   string `json:"end"`
}

type ThumbnailQuality

type ThumbnailQuality string

ThumbnailQuality is a youtube thumbnail quality option

const ThumbnailQualityDefault ThumbnailQuality = "default"

ThumbnailQualityDefault is the default quality thumbnail jpg

const ThumbnailQualityHigh ThumbnailQuality = "hqdefault"

ThumbnailQualityHigh is the high quality thumbnail jpg

const ThumbnailQualityMaxRes ThumbnailQuality = "maxresdefault"

ThumbnailQualityMaxRes is the maximum resolution quality jpg

const ThumbnailQualityMedium ThumbnailQuality = "mqdefault"

ThumbnailQualityMedium is the medium quality thumbnail jpg

const ThumbnailQualitySD ThumbnailQuality = "sddefault"

ThumbnailQualitySD is the standard def quality thumbnail jpg

type VideoInfo added in v0.3.0

type VideoInfo struct {
	ID              string     // The video ID
	Title           string     // The video title
	Description     string     // The video description
	DatePublished   time.Time  // The date the video was published
	Formats         FormatList // Formats the video is available in
	DASHManifestURL string     // URI of the DASH manifest file
	HLSManifestURL  string     // URI of the HLS manifest file
	Keywords        []string   // List of keywords associated with the video
	Uploader        string     // Author of the video
	Song            string
	Artist          string
	Album           string
	Writers         string
	Duration        time.Duration // Duration of the video
	// contains filtered or unexported fields
}

VideoInfo contains the info a youtube video

func GetVideoInfo added in v0.3.0

func GetVideoInfo(cx context.Context, value interface{}) (*VideoInfo, error)

GetVideoInfo fetches info from a url string, url object, or a url string

func (*VideoInfo) GetThumbnailURL added in v0.3.0

func (info *VideoInfo) GetThumbnailURL(quality ThumbnailQuality) *url.URL

GetThumbnailURL returns a url for the thumbnail image with the given quality

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL