youtubedl

package module
v0.0.0-...-9df4c24 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 8 Imported by: 0

README

youtube-dl-go

A Go library that wraps youtube-dl's Python API.

coverage report GoDoc pipeline status

youtube-dl-go is a simple wrapper to the popular youtube-dl application for Python. This allows you to access the youtube-dl binary programmatically in any application that you write.

The motivation for creating this library was simple: many of the other pure Go solutions for downloading YouTube videos were lacking in features or were woefully out of date, making them difficult to use and implement in actual projects. By wrapping youtube-dl with this library, we get easy access to an already well developed download solution while also having an easy to use API for our Go applications.

This package is still in a very alpha stage. It is not being actively developed, as its main purpose is to provide a good youtube-dl interface for my other project, Prismriver. That being said however, I am more than happy to respond to feature requests to increase the functionality of this library.

Usage

All downloads are initiated by creating a Downloader instance. There is an Output function which will allow you to specify where the file is downloaded, and supports various output templates from youtube-dl. Downloads can be run by calling Run, which blocks the program until the download completes, or with RunProgress, which provides channels to track the download's progress and wait for a completed download.

package main

import (
	"fmt"
	"gitlab.com/ttpcodes/youtube-dl-go"
)

func main() {
	// Guess what my favorite song is as of this writing?
	downloader := youtubedl.NewDownloader("https://www.youtube.com/watch?v=Qu_OzBsgRcI")
	downloader.Output("~/Downloads/RealOrFake")
	path, err := downloader.Run()
	if err != nil {
		fmt.Print(err)
		return
	}
	fmt.Printf("File was downloaded to %s", path)
	
	// My other favorite song, which I'll use to show channel usage:
	downloader2 := youtubedl.NewDownloader("https://www.youtube.com/watch?v=6nDrD2WNSJU")
	downloader2.Output("~/Downloads/" + youtubedl.ID)
	progressChan, resultChan, err := downloader2.RunProgress()
	if err != nil {
	    fmt.Print(err)
	}
	for progress := range progressChan {
		fmt.Printf("Current progress: %f", progress)
	}
	result := <- resultChan
	if result.Err != nil {
		fmt.Print(err)
		return
	}
	fmt.Printf("File 2 was downloaded to %s", result.Path)
}

When run, the program will output something similar to this:

File was downloaded to /home/ttpcodes/Downloads/RealOrFake.mkv
Current progress: 0.000000
Current progress: 0.050000
Current progress: 0.050000
Current progress: 0.100000
Current progress: 0.250000
Current progress: 0.500000
Current progress: 1.000000
Current progress: 1.950000
Current progress: 3.950000
Current progress: 7.900000
Current progress: 15.750000
Current progress: 31.500000
... # Omitted progress lines
Current progress: 60.800000
Current progress: 71.650000
Current progress: 93.300000
Current progress: 100.000000
File 2 was downloaded to /home/ttpcodes/Downloads/6nDrD2WNSJU.mkv

API

API documentation can be found on GoDoc.

Installation

Any Go method of package management should support the installation of this package. My preferred package management method is dep:

dep ensure gitlab.com/ttpcodes/youtube-dl-go

LICENSE

This project is licensed under the MIT License.

Documentation

Overview

Package youtube-dl-go wraps youtube-dl's Python API.

youtube-dl-go is a simple wrapper to the popular youtube-dl application for Python. This allows you to access the youtube-dl binary programmatically in any application that you write.

Index

Constants

View Source
const (
	// Represents the ID of the video.
	ID = "%(id)s"
)

Represents format templates that can be used for Downloader output paths.

Variables

This section is empty.

Functions

This section is empty.

Types

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Represents a youtube-dl command instance for downloading a track. Do not instantiate this yourself. Use NewDownloader to create an instance.

func NewDownloader

func NewDownloader(url string) *Downloader

Creates a new Downloader instance set to download the provided url.

func (*Downloader) Format

func (d *Downloader) Format(format string) *Downloader

Sets the video/audio format for the downloader.

func (Downloader) GetExtractor

func (d Downloader) GetExtractor() (string, error)

Gets the name of the extractor that would be used to handle a given URL.

URLs not matching any extractors result in a "generic" extractor being returned.

func (Downloader) GetInfo

func (d Downloader) GetInfo() (Info, error)

Gets the info of the video, akin to calling youtube-dl with the --dump-json flag.

This will return either an Info instance representing the video info or the encountered error.

func (*Downloader) NoPlaylist

func (d *Downloader) NoPlaylist()

Sets the downloader to avoid downloading playlists, instead downloading the first item, if any.

func (*Downloader) Output

func (d *Downloader) Output(path string) *Downloader

Sets the output path for the downloader.

func (Downloader) Run

func (d Downloader) Run() (string, error)

Runs the downloader. This method blocks until complete. For a channel-based solution, use RunProgress.

This will return the path of the downloaded file as a string or any errors that were encountered.

func (Downloader) RunProgress

func (d Downloader) RunProgress() (chan float64, chan Result, error)

Runs the downloader. This method returns channels that allow to track a download's progress and receive the result once the download is done. For a simpler solution, use Run.

The float64 channel returns percent progress of the download, the Result channel returns a Result struct representing the command's result and contains the command's path or any encountered errors, and the error indicates an error when setting up the command call.

type Info

type Info struct {
	// The duration of the video, in seconds.
	Duration float64 `json:"duration"`
	// The extractor used for the video.
	Extractor string `json:"extractor"`
	// The ID of the video.
	ID string `json:"id"`
	// The title of the video.
	Title string `json:"title"`
	// The default video codec of the video, if any.
	VCodec string `json:"vcodec"`
	// The main URL the media is accessible from.
	WebpageURL string `json:"webpage_url"`
}

Represents the information about a video.

type Result

type Result struct {
	// Represents an error if any was encountered.
	Err error
	// Represents the output path of the Downloader if successful.
	Path string
}

Represents the result of a Downloader when called with RunProgress.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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