Coder Social home page Coder Social logo

youtube's Introduction

Download Youtube Video in Golang

GitHub license Go Reference Build Status Coverage

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

This tool is meant to be used to download CC0 licenced content, we do not support nor recommend using it for illegal activities.

Overview

Installation

Install via go get

Please ensure you have installed Go 1.21 or later.

go get github.com/kkdai/youtube/v2

From source code

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

Mac

brew install youtubedr

in Termux

pkg install youtubedr

You can also find this package in

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 download QAGDGja7kbs
$ youtubedr download 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.

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 download 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 download -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 download -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 download -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 download -q 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.

youtube's People

Contributors

appleboy avatar aracoool avatar benjamesfleming avatar cloudingcity avatar corny avatar cybre avatar davincible avatar elproxy avatar fr3fou avatar ilmanzo avatar itscaro avatar itsclairton avatar john-black-3k avatar julian-chu avatar kamilkabir9 avatar kasseltrankos avatar kkc avatar kkdai avatar nitrix avatar nksapphire avatar robotomize avatar satont avatar shadiestgoat avatar shinku-chen avatar supersandro2000 avatar sxwr avatar tobychui avatar tystuyfzand avatar wakeful-cloud avatar xoltia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

youtube's Issues

please add codecs, birate, range, init to StreamList data

type Stream struct {
	ItagNo   int    `json:"itag"`
	URL      string `json:"url"`
	MimeType string `json:"mimeType"`
	Quality  string `json:"quality"`
	Cipher   string `json:"signatureCipher"`
}

Please add codecs, bitrate, range, init of AdaptiveFormats to the Stream struct please ...

Playlist support

Hi, I'm developing a program in which I need to download a playlist of videos; so far I've found a YouTube API link to list all videos in a playlist, and it contains all the videos' Ids. For now, I've taken these ids, and then I can download the videos individually.

Do you have any plans to add this? I can perhaps work on it if you'd like, as it's not too large. Luckily, the API link contains lots of information for each video, so you wouldn't have to call decodeUrl, which at the moment I do.

https://youtube.com/list_ajax?style=json&action_get_list=1&list={playlist-id}&hl=en is the API url, I stole it from https://github.com/Tyrrrz/YoutubeExplode/blob/master/YoutubeExplode/ReverseEngineering/Responses/PlaylistResponse.cs

The API's return objects are in the form

{
    "author": string,
    "views": int,
    "title": string,
    "description": string,
    "video": {
        "user_id": string,
        "encrypted_id": string,
        "keywords": string,
        "title": string,
        ... ...
    }[]
}

itag option not working

installed the latest version (2.0.2) in termux but the -i option is not working, it always downloads the best quality audio/video as if the option is not given.

Where is the binary? Newbie issue

Complete newbie question!!!

After the installation where is the youtubedr binary located?

I am running Ubuntu 20.04.

I installed go today along with youtubedr. I tried both install methods and no errors but I cannot find youtubedr. I search my home directory and cannot seem to find it. I thought it would be in ~/go/bin/ but no luck. I can see the source in ~/go/src/github/kkdai/youtube

I am not sure what I did wrong?

Thank you

Need to show proper message when youtube is not able to download private video

when I try youtubedr nINQjT7Zr9w, I see

2020/06/16 00:24:07 download to dir= /Users/kakashi/Movies/youtubedr
2020/06/16 00:24:07 Using http without proxy.
2020/06/16 00:24:07 Found video id: 'nINQjT7Zr9w'
2020/06/16 00:24:07 url: https://youtube.com/get_video_info?video_id=nINQjT7Zr9w&eurl=https://youtube.googleapis.com/v/nINQjT7Zr9w
panic: interface conversion: interface {} is nil, not map[string]interface {}

goroutine 1 [running]:
github.com/kkdai/youtube.getVideoTitleAuthor(0xc0000d18c8, 0xc0000d18c8, 0x1346a71, 0xf, 0xc0001c0428)
        /Users/kakashi/Programs/Github/youtube/youtube.go:447 +0x2eb
github.com/kkdai/youtube.(*Youtube).parseVideoInfo(0xc0000d8000, 0x0, 0x0)
        /Users/kakashi/Programs/Github/youtube/youtube.go:221 +0x2b5
github.com/kkdai/youtube.(*Youtube).DecodeURL(0xc0000d8000, 0x7ffeefbff939, 0xb, 0x1, 0x0)
        /Users/kakashi/Programs/Github/youtube/youtube.go:73 +0x153
main.main()
        /Users/kakashi/Programs/Github/youtube/youtubedr/main.go:56 +0x541
exit status 2

This is because it's the private video, I think we should handle this kind of error.

Need to check if command line argument is given

Description

if we don't specify video ID as following, you will see an improper error message.

$ go run youtubedr/main.go

2020/06/15 23:09:16 []
2020/06/15 23:09:16 download to dir= /Users/kakashi/Movies/youtubedr
2020/06/15 23:09:16 Using http without proxy.
2020/06/15 23:09:16 Found video id: ''
err: findVideoID error=the video id must be at least 10 characters long

instead, we should print default usage to let users know how to use this tool

discussion: improve project layout and etc...

now youtube have more code and functions than before. To improve readability and maintainability, we would like to reorganize project layout, some plans are under discussion:

  • project layout: change to cmd/pkg(internal)/etc
  • reorganize errors
  • move some functions from youtube to own package

Thanks for any suggestion in advance.

v2 is impossible to import

Trying to import v2 in a project, copying the provided example_test.go file, yields the following error.
image

Even after updating the go.mod version like so
image
It still errors out with

require github.com/kkdai/youtube: version "v2.0.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2

From what I understand the module directive in the go.mod file should be updated with /v2 like so

module github.com/kkdai/youtube/v2

Add AdaptiveFormats support

Youbue provides 2 different formats (Formats, AdaptiveFormats). I thought we should make user be able to download AdaptiveFormats videos.

err: parse video info failed, err=no stream map found in the server's answer

$ youtubedr https://www.youtube.com/watch?v=rFejpH_tAHM
2020/04/01 16:38:58 [https://www.youtube.com/watch?v=rFejpH_tAHM]
2020/04/01 16:38:58 download to dir= /home/egorov/Movies/yotubedr
2020/04/01 16:38:58 Found video id: 'rFejpH_tAHM'
2020/04/01 16:38:58 url: http://youtube.com/get_video_info?video_id=rFejpH_tAHM
err: parse video info failed, err=no stream map found in the server's answer
$ go version
go version go1.14 linux/amd64

Incorrect argument index in example

The Usage section in the readme has currentFile, _ := filepath.Abs(os.Args[0]), which points to the main executable.

I think it should be currentFile, _ := filepath.Abs(os.Args[1]). If I am misunderstanding, then disregard.

Index out of range when trying to download

Hello, I am trying to download a video through your library and I'm getting the following:

[niko:~/Downloads] 2s $ youtubedr https://www.youtube.com/watch?v=FHpvI8oGsuQ
2019/05/27 21:28:09 [https://www.youtube.com/watch?v=FHpvI8oGsuQ]
2019/05/27 21:28:09 download to dir= /home/niko/Downloads
2019/05/27 21:28:09 Found video id: 'FHpvI8oGsuQ'
2019/05/27 21:28:09 url: http://youtube.com/get_video_info?video_id=FHpvI8oGsuQ
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/kkdai/youtube.(*Youtube).StartDownload(0xc42001c4e0, 0xc4200d2960, 0x14, 0x7949c0, 0xc42018ebf0)
        /home/niko/go/src/github.com/kkdai/youtube/youtube.go:59 +0x3b6
main.main()
        /home/niko/go/src/github.com/kkdai/youtube/youtubedr/main.go:20 +0x29a

Any clues?

Use code

Hi!
I've a request. I'm building a youtube client written on go, and i want to use a part of your code in my application. Obviously if you accept, i going to giving you the credits, and link this project.

What do you think?

PD: Sorry for my english

missing document for options

"youtubedr -h" will print information for options, but missing document in README
document should describe options and provide some examples.

image

Need to show message to let user know there is no downloadable hd1080 video

Issue

When I tried go run youtubedr/main.go -q hd1080 Ap0huJwyT7g, it will show the following messages

check ffmpeg is installed....
2020/07/11 21:42:48 Download url= 

2020/07/11 21:42:48 Downloading video file...
2020/07/11 21:42:48 Http.Get
error: Get : unsupported protocol scheme ""
target: 

err: Get : unsupported protocol scheme ""

this message is not able to make users know there is no 1080 video

Change on player_response json

the program is failing in youtube.go at line 248, because the struct for player_response is wrong(maybe youtube has changed the response). the struct has a field called Cipher but in youtube response is called signatureCipher and has no URL field

Can't download music video?

Hi, I've found that when download a music video it will get the error: error parsing signature tokens
but there's no error when it's not a muisc video.

Is the google api changed or something?

I'm using v2.0.1 btw.

url_encoded_fmt_stream_map is null and ...

Hello 😄

Sadly, some videos didn't have url_encoded_fmt_stream_map or got 403 code, so didn't work.

ex.1) 3bNITQR4Uso

<... skip ...>
url_encoded_fmt_stream_map => 
fflags => bulleit_block_player_pause_until_ad_start%3Dtrue% ...
<... skip ...>

(i made a small tool for test it with D and just split it like :("=", "\n"). nvm...)

ex.2) -k5iFLYXmWU

2018/12/11 00:46:06 reading answer: non 200[code=403] status code received: '<nil>'

(with youtubedr.exe)

Should add support of itag.

Like youtube-dl and ytdl, youtubedr should also have itag option , in which we can download different format like webm and m4a also for audio.

youtubedr can not work properly

youtubedr https://www.youtube.com/watch?v=54e6lBE3BoQ

2019/12/30 09:08:57 [https://www.youtube.com/watch?v=54e6lBE3BoQ]
2019/12/30 09:08:57 download to dir= /Users/huzhang2/Movies/youtubedr
2019/12/30 09:08:57 Found video id: '54e6lBE3BoQ'
2019/12/30 09:08:57 url: https://youtube.com/get_video_info?video_id=54e6lBE3BoQ
2019/12/30 09:08:58 Title: 190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷 Author: FanTastiCut Library Stream found: quality 'hd720', format 'video/mp4; codecs="avc1.64001F, mp4a.40.2"'
2019/12/30 09:08:58 Title: 190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷 Author: FanTastiCut Library Stream found: quality 'medium', format 'video/webm; codecs="vp8.0, vorbis"'
2019/12/30 09:08:58 Title: 190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷 Author: FanTastiCut Library Stream found: quality 'medium', format 'video/mp4; codecs="avc1.42001E, mp4a.40.2"'
2019/12/30 09:08:58 Download url= https://r1---sn-i3belne6.googlevideo.com/videoplayback?expire=1577689738&ei=Kk4JXoibGOyKs8IP6JqHmAU&ip=2001%3A420%3A5899%3A1252%3A55d7%3Acadf%3Ae1ea%3Aaa87&id=o-ABFQfCJ-KXfJTZy6op7hVn8moDnyilJ8uflnbzEVtNjo&itag=22&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-i3belne6%2Csn-i3b7kn7d&ms=au%2Crdu&mv=m&mvi=0&pl=39&initcwndbps=2277500&mime=video%2Fmp4&ratebypass=yes&dur=227.091&lmt=1546958970013208&mt=1577668064&fvip=1&fexp=23842630&c=WEB&txp=2211222&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgf0tmXkOumnpdF1EmxL5y_ane5tgzTzxFuOtnrKeffQICICG-UusXI6iX0pPLNSD2qrZZqxX6KnZTEtpJEjQj-nLL

2019/12/30 09:08:58 Download to file= /Users/huzhang2/Movies/youtubedr/190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷.mov

2019/12/30 09:08:58 reading answer: non 200[code=403] status code received: ''
2019/12/30 09:08:58 Download url= https://r1---sn-i3belne6.googlevideo.com/videoplayback?expire=1577689738&ei=Kk4JXoibGOyKs8IP6JqHmAU&ip=2001%3A420%3A5899%3A1252%3A55d7%3Acadf%3Ae1ea%3Aaa87&id=o-ABFQfCJ-KXfJTZy6op7hVn8moDnyilJ8uflnbzEVtNjo&itag=43&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-i3belne6%2Csn-i3b7kn7d&ms=au%2Crdu&mv=m&mvi=0&pl=39&initcwndbps=2277500&mime=video%2Fwebm&gir=yes&clen=20520392&ratebypass=yes&dur=0.000&lmt=1546959331386623&mt=1577668064&fvip=1&fexp=23842630&c=WEB&txp=2201222&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgf0tmXkOumnpdF1EmxL5y_ane5tgzTzxFuOtnrKeffQICICG-UusXI6iX0pPLNSD2qrZZqxX6KnZTEtpJEjQj-nLL

2019/12/30 09:08:58 Download to file= /Users/huzhang2/Movies/youtubedr/190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷.mov

2019/12/30 09:08:59 reading answer: non 200[code=403] status code received: ''
2019/12/30 09:08:59 Download url= https://r1---sn-i3belne6.googlevideo.com/videoplayback?expire=1577689738&ei=Kk4JXoibGOyKs8IP6JqHmAU&ip=2001%3A420%3A5899%3A1252%3A55d7%3Acadf%3Ae1ea%3Aaa87&id=o-ABFQfCJ-KXfJTZy6op7hVn8moDnyilJ8uflnbzEVtNjo&itag=18&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-i3belne6%2Csn-i3b7kn7d&ms=au%2Crdu&mv=m&mvi=0&pl=39&initcwndbps=2277500&mime=video%2Fmp4&gir=yes&clen=14547071&ratebypass=yes&dur=227.091&lmt=1546958919927019&mt=1577668064&fvip=1&fexp=23842630&c=WEB&txp=2211222&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHylml4wRAIgf0tmXkOumnpdF1EmxL5y_ane5tgzTzxFuOtnrKeffQICICG-UusXI6iX0pPLNSD2qrZZqxX6KnZTEtpJEjQj-nLL

2019/12/30 09:08:59 Download to file= /Users/huzhang2/Movies/youtubedr/190107 Trouble Maker - Trouble Maker | Dance cover by 莉爷.mov

2019/12/30 09:08:59 reading answer: non 200[code=403] status code received: ''
err: non 200 status code received

youtubedr https://www.youtube.com/watch?v=E2_1y_9zGQs
2019/12/30 09:13:11 [https://www.youtube.com/watch?v=E2_1y_9zGQs]
2019/12/30 09:13:11 download to dir= /Users/huzhang2/Movies/youtubedr
2019/12/30 09:13:11 Found video id: 'E2_1y_9zGQs'
2019/12/30 09:13:11 url: https://youtube.com/get_video_info?video_id=E2_1y_9zGQs
err: parse video info failed, err=no stream map found in the server's answer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.