Download Youtube Video in Golang

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

  • 所有者: kkdai/youtube
  • 平台: Linux, Mac, Windows
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

用 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)

主要指標

概覽
名稱與所有者kkdai/youtube
主編程語言Go
編程語言Go (語言數: 2)
平台Linux, Mac, Windows
許可證MIT License
所有者活动
創建於2015-12-15 07:15:27
推送於2025-04-22 21:53:27
最后一次提交2025-04-22 23:16:22
發布數48
最新版本名稱v2.10.3 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數3.6k
關注者數60
派生數460
提交數485
已啟用問題?
問題數187
打開的問題數37
拉請求數124
打開的拉請求數6
關閉的拉請求數24
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

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.