Download Youtube Video in Golang

用 Golang 下载 Youtube 视频。(Download Youtube Video in Golang)

  • Owner: kkdai/youtube
  • Platform: Linux, Mac, Windows
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

用 Golang 下载 YouTube 视频

该软件包是Youtube 视频下载软件包,有关更多详细信息,请参阅 https://github.com/rg3/youtube-dl

安装:

go get github.com/kkdai/youtube

或者

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

在 Termux 中安装

pkg install youtubedr

用法

直接使用二进制

使用起来真的很简单,只需从 youtube 网址获取视频 ID -- 例如:

https://www.youtube.com/watch?v=rFejpH_tAHM

视频 id 是 rFejpH_tAHM。

$ youtubedr QAGDGja7kbs
$ youtubedr https://www.youtube.com/watch?v=rFejpH_tAHM

将此包导入您的 golang 程序中

package main
import (
    "flag"
    "fmt"
    "log"
    "os/user"
    "path/filepath"
    . "github.com/kkdai/youtube"
)
func main() {
    flag.Parse()
    log.Println(flag.Args())
    usr, _ := user.Current()
    currentDir := fmt.Sprintf("%v/Movies/youtubedr", usr.HomeDir)
    log.Println("download to dir=", currentDir)
    y := NewYoutube(true)
    arg := flag.Arg(0)
    if err := y.DecodeURL(arg); err != nil {
        fmt.Println("err:", err)
    }
    if err := y.StartDownload(currentDir, "dl.mp4", "medium",0); err != nil {
        fmt.Println("err:", err)
    }
}

选项

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)

示例:

  • 获取dotGo-2015-rob-pike 视频的信息以供下载

    go get github.com/kkdai/youtube/youtubedr
    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"
    ........
        
  • 下载 dotGo-2015-rob-pike-video

    go get github.com/kkdai/youtube/youtubedr
    dotGo 2015 - Rob Pike - Simplicity is Complicated 下载视频
    youtubedr https://www.youtube.com/watch?v=rFejpH_tAHM
        
  • 下载视频到特定的文件夹和名称

    go get github.com/kkdai/youtube/youtubedr
    下载 dotGo 2015 - Rob Pike - Simplicity is Complicated 到当前目录,并将文件命名为simple-is-complicated.mp4
    youtubedr -d ./ -o simplicity-is-complicated.mp4 https://www.youtube.com/watch?v=rFejpH_tAHM
        
  • 下载具有特定质量的视频

    go get github.com/kkdai/youtube/youtubedrdotGo 2015 - Rob Pike - Simplicity is Complicated 下载特定质量的视频
    youtubedr -q medium https://www.youtube.com/watch?v=rFejpH_tAHM
        

    hd1080 质量的特例:

    hd1080 需要安装 ffmpeg
    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
        
  • 下载指定 itag 的视频

    go get github.com/kkdai/youtube/youtubedrdotGo 2015 - Rob Pike - Simplicity is Complicated 下载视频
    youtubedr -i 18 https://www.youtube.com/watch?v=rFejpH_tAHM    


工作原理

  • 解析你在 URL 中输入的视频 id
    • 例如:
      https://www.youtube.com/watch?v=rFejpH_tAHM        
      其视频 id 是 rFejpH_tAHM
  • 通过视频 id 取得视频信息。
  • 分析和解码视频信息。
    • 下载 URL 在 "url="
    • title in "title="
  • 从 URL 下载视频
    • 需要 "url" 的字符串组合

灵感来源

Project52

它是我的 project 52 之一。

许可

此包在 MIT 许可下使用。有关详细信息,请参阅 LICENSE。

(The first version translated by vz on 2020.08.01)

Overview

Name With Ownerkkdai/youtube
Primary LanguageGo
Program languageGo (Language Count: 2)
PlatformLinux, Mac, Windows
License:MIT License
Release Count46
Last Release Namev2.10.1 (Posted on )
First Release Namev1.0.0 (Posted on )
Created At2015-12-15 07:15:27
Pushed At2024-04-23 18:49:26
Last Commit At2024-04-22 01:55:34
Stargazers Count3.1k
Watchers Count57
Fork Count409
Commits Count472
Has Issues Enabled
Issues Count167
Issue Open Count24
Pull Requests Count120
Pull Requests Open Count6
Pull Requests Close Count21
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Download Youtube Video in Golang

GitHub license GoDoc Build Status

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

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"

Install

go get github.com/kkdai/youtube

Usage


package main

import (
	"flag"
	"fmt"
	"log"
	"os/user"
	"path/filepath"

	. "github.com/kkdai/youtube"
)

func main() {
	flag.Parse()
	log.Println(flag.Args())
	usr, _ := user.Current()
	currentDir := fmt.Sprintf("%v/Movies/youtubedr", usr.HomeDir)
	log.Println("download to dir=", currentDir)
	y := NewYoutube(true)
	arg := flag.Arg(0)
	if err := y.DecodeURL(arg); err != nil {
		fmt.Println("err:", err)
	}
	if err := y.StartDownload(filepath.Join(currentDir, "dl.mp4")); err != nil {
		fmt.Println("err:", err)
	}
}

Use the binary directly

go get github.com/kkdai/youtube/youtubedr

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

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

Inspired

Project52

It is one of my project 52.

License

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

To the top