youtube

package module
v2.1.1-0...-0c8b739 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2020 License: MIT Imports: 12 Imported by: 0

README

Download Youtube Video in Golang

GitHub license GoDoc Build Status Coverage

This package is a Youtube video download package, for more detail refer https://github.com/rg3/youtube-dl for more download options.

Overview

Install:

go get github.com/kkdai/youtube/v2

OR

git clone https://github.com/kkdai/youtube.git
cd youtube
go run ./cmd/youtubedr

Install in Termux

pkg install youtubedr

Usage

Use the binary directly

It's really simple to use, just get the video id from youtube url - ex: https://www.youtube.com/watch?v=rFejpH_tAHM, the video id is rFejpH_tAHM

$ youtubedr QAGDGja7kbs
$ youtubedr https://www.youtube.com/watch?v=rFejpH_tAHM
Use this package in your golang program

Please check out the example_test.go for example code.

Options:

option type description default value
-d string the output directory $HOME/Movies/youtubedr
-o string the output file name ( ext will auto detect on default value ) [video's title].ext
-d string the Socks 5 proxy (e.g. 10.10.10.10:7878)
-q string the output file quality (medium, hd720)
-i string the output file itag (13, 17 etc..) 0
-info bool show information of available streams (quality, itag, MIMEtype)

Example:

  • Get information of dotGo-2015-rob-pike video for downloading

    go get github.com/kkdai/youtube/v2/youtubedr

    Download video from dotGo 2015 - Rob Pike - Simplicity is Complicated

    youtubedr -info https://www.youtube.com/watch?v=rFejpH_tAHM
    
    Title: dotGo 2015 - Rob Pike - Simplicity is Complicated
    Author: dotconferences
    -----available streams-----
    itag:  18 , quality: medium , type: video/mp4; codecs="avc1.42001E, mp4a.40.2"
    itag:  22 , quality:  hd720 , type: video/mp4; codecs="avc1.64001F, mp4a.40.2"
    itag: 137 , quality: hd1080 , type: video/mp4; codecs="avc1.640028"
    itag: 248 , quality: hd1080 , type: video/webm; codecs="vp9"
    ........
    
  • Download dotGo-2015-rob-pike-video

    go get github.com/kkdai/youtube/v2/youtubedr

    Download video from dotGo 2015 - Rob Pike - Simplicity is Complicated

    youtubedr https://www.youtube.com/watch?v=rFejpH_tAHM
    
  • Download video to specific folder and name

    go get github.com/kkdai/youtube/v2/youtubedr

    Download video from dotGo 2015 - Rob Pike - Simplicity is Complicated to current directory and name the file to simplicity-is-complicated.mp4

    youtubedr -d ./ -o simplicity-is-complicated.mp4 https://www.youtube.com/watch?v=rFejpH_tAHM
    
  • Download video with specific quality

    go get github.com/kkdai/youtube/v2/youtubedr

    Download video from dotGo 2015 - Rob Pike - Simplicity is Complicated with specific quality

    youtubedr -q medium https://www.youtube.com/watch?v=rFejpH_tAHM
    
    Special case by quality hd1080:

    Installation of ffmpeg is necessary for hd1080

    ffmpeg   //check ffmpeg is installed, if not please download ffmpeg and set to your PATH.
    youtubedr -q hd1080 https://www.youtube.com/watch?v=rFejpH_tAHM
    
  • Download video with specific itag

    go get github.com/kkdai/youtube/v2/youtubedr

    Download video from dotGo 2015 - Rob Pike - Simplicity is Complicated

    youtubedr -i 18 https://www.youtube.com/watch?v=rFejpH_tAHM
    

How it works

  • Parse the video ID you input in URL
    • ex: https://www.youtube.com/watch?v=rFejpH_tAHM, the video id is rFejpH_tAHM
  • Get video information via video id.
    • Use URL: http://youtube.com/get_video_info?video_id=
  • Parse and decode video information.
    • Download URL in "url="
    • title in "title="
  • Download video from URL
    • Need the string combination of "url"

Inspired

Project52

It is one of my project 52.

License

This package is licensed under MIT license. See LICENSE for details.

Documentation

Overview

Package youtube implement youtube download package in go.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrCipherNotFound             = errors.New("cipher not found")
	ErrInvalidCharactersInVideoID = errors.New("invalid characters in video id")
	ErrVideoIDMinLength           = errors.New("the video id must be at least 10 characters long")
	ErrReadOnClosedResBody        = errors.New("http: read on closed response body")
)

Functions

This section is empty.

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
}

Client offers methods to download video metadata and video streams.

Example

ExampleDownload : Example code for how to use this package for download video.

videoID := "BaW_jenozKc"
client := youtube.Client{}

video, err := client.GetVideo(videoID)
if err != nil {
	panic(err)
}

resp, err := client.GetStream(video, &video.Formats[0])
if err != nil {
	panic(err)
}
defer resp.Body.Close()

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

_, err = io.Copy(file, resp.Body)
if err != nil {
	panic(err)
}
Output:

func (*Client) GetStream

func (c *Client) GetStream(video *Video, format *Format) (*http.Response, error)

GetStream returns the HTTP response for a specific format

func (*Client) GetStreamContext

func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *Format) (*http.Response, error)

GetStreamContext returns the HTTP response for a specific format with a context

func (*Client) GetStreamURL

func (c *Client) GetStreamURL(video *Video, format *Format) (string, error)

GetStreamURL returns the url for a specific format

func (*Client) GetStreamURLContext

func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error)

GetStreamURL returns the url for a specific format with a context

func (*Client) GetVideo

func (c *Client) GetVideo(url string) (*Video, error)

GetVideo fetches video metadata

func (*Client) GetVideoContext

func (c *Client) GetVideoContext(ctx context.Context, url string) (*Video, error)

GetVideoContext fetches video metadata with a context

type ErrPlayabiltyStatus

type ErrPlayabiltyStatus struct {
	Status string
	Reason string
}

func (ErrPlayabiltyStatus) Error

func (err ErrPlayabiltyStatus) Error() string

type ErrResponseStatus

type ErrResponseStatus struct {
	Status string
	Reason string
}

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    string `json:"contentLength"`
	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) FindByItag

func (list FormatList) FindByItag(itagNo int) *Format

func (FormatList) FindByQuality

func (list FormatList) FindByQuality(quality string) *Format

type Video

type Video struct {
	ID              string
	Title           string
	Author          string
	Duration        time.Duration
	Formats         FormatList
	DASHManifestURL string // URI of the DASH manifest file
	HLSManifestURL  string // URI of the HLS manifest file
	ResponseData    playerResponseData
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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