youtubeuploader

package module
v1.25.5 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

README

Youtube Uploader

Tests

Scripted uploads to youtube.

  • upload video files from local disk or from the web.
  • ratelimit upload bandwidth

Download

Grab a precompiled binary for Linux, Mac or Windows or build it yourself.

Setup

Youtube API

Talking to the Youtube API requires oauth2 authentication. As such, you must:

  1. Create an account on the Google Developers Console
  2. Create a new project for this app
  3. Enable the Youtube API (APIs & Services -> Enable APIs and Services -> Click 'ENABLE APIS AND SERVICES' top right). Select 'YouTube Data API v3'
  4. Create OAuth consent screen (APIs & Services -> OAuth Consent Screen)
    • Add a test user in "Audience -> Test users". This can be any Google User account but it should correspond with the Youtube account where videos will be uploaded.
  5. Create Credentials (APIs & Services -> Credentials -> click 'CREATE CREDENTIALS'), select 'OAuth client ID', select 'Web application'
  6. Download the client secrets JSON file (click download icon next to newly created client ID) and save it as file client_secrets.json in the same directory as the utility e.g.
{
  "web": {
    "client_id": "xxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
    "project_id": "youtubeuploader-yyyyy",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "xxxxxxxxxxxxxxxxxxxx",
    "redirect_uris": [
      "http://localhost:8080/oauth2callback"
    ]
  }
}

NOTE 1 Google will apply 'private' status on videos uploaded to newly created projects - from Google's Announcement:

All videos uploaded via the videos.insert endpoint from unverified API projects created after 28 July 2020 will be restricted to private viewing mode. To lift this restriction, each project must undergo an audit to verify compliance with the Terms of Service.

See Issue #86 for more information.

NOTE 2 By default you will only be able to upload ~6 videos every 24 hours due to quota restrictions set by Google. See Issue #119 for more information.

Usage

At a minimum, just specify a filename:

./youtubeuploader -filename blob.mp4

If it is the first time you've run the utility, a browser window should popup and prompt you to provide Youtube credentials. A token will be created and stored in request.token file in the local directory for subsequent use. To run the utility on a headless-server, generate the token file locally first, then simply copy the token file along with youtubeuploader and client_secrets.json to the remote host.

Full list of options:

Usage:
  -cache string
        token cache file (default "request.token")
  -caption string
        caption filename. Can be a URL
  -categoryId string
        video category Id
  -chunksize int
        size (in bytes) of each upload chunk. A zero value will cause all data to be uploaded in a single request (default 16777216)
  -debug
        turn on verbose log output
  -description string
        video description (default "uploaded by youtubeuploader")
  -filename string
        video filename. Can be a URL. Read from stdin with '-'
  -language string
        video language (default "en")
  -limitBetween string
        only rate limit between these times e.g. 10:00-14:00 (local time zone)
  -metaJSON string
        JSON file containing title,description,tags etc (optional)
  -metaJSONout string
        filename to write uploaded video metadata into (optional)
  -notify
        notify channel subscribers of new video. Specify '-notify=false' to disable. (default true)
  -oAuthPort int
        TCP port to listen on when requesting an oAuth token (default 8080)
  -playlistID value
        playlistID to add the video to. Can be used multiple times
  -privacy string
        video privacy status (default "private")
  -quiet
        suppress progress indicator
  -ratelimit int
        rate limit upload in Kbps. No limit by default
  -recordingDate value
        recording date e.g. 2024-11-23
  -secrets string
        Client Secrets configuration (default "client_secrets.json")
  -sendFilename
        send original file name to YouTube (default true)
  -tags string
        comma separated list of video tags
  -thumbnail string
        thumbnail filename. Can be a URL
  -title string
        video title
  -version
        show version

NOTE: When specifying a URL as the filename, the data will be streamed through the localhost (download from remote host, then upload to Youtube)

If -quiet is specified, no upload progress will be displayed. Current progress can be output by sending signal USR1 to the process e.g. kill -USR1 <pid> (Linux/Unix only).

Metadata

Video title, description etc can specified via the command line flags or via a JSON file using the -metaJSON flag. An example JSON file would be:

{
      "title": "my test title",
      "description": "my test description",
      "tags": [
            "test tag1",
            "test tag2"
      ],
      "privacyStatus": "private",
      "madeForKids": false,
      "embeddable": true,
      "license": "creativeCommon",
      "publicStatsViewable": true,
      "publishAt": "2017-06-01T12:05:00+02:00",
      "categoryId": "10",
      "recordingDate": "2017-05-21",
      "playlistIds": [
            "xxxxxxxxxxxxxxxxxx",
            "yyyyyyyyyyyyyyyyyy"
      ],
      "playlistTitles": [
            "my test playlist"
      ],
      "language": "fr",
      "localizations": {
            "en": {
                  "title": "My English Title",
                  "description": "My English description"
            },
            "it": {
                  "title": "Il mio titolo in italiano",
                  "description": "La mia descrizione in italiano"
            }
      },
      "containsSyntheticMedia": false
}
  • all fields are optional
  • use \n in the description to insert newlines
  • times can be provided in one of two formats: yyyy-mm-dd (UTC) or yyyy-mm-ddThh:mm:ss+zz:zz
  • any values supplied via -metaJSON will take precedence over flags

Credit

Based on Go Youtube API Sample code

Thanks to github.com/tokland/youtube-upload for insight into how to update playlists.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildOAuthHTTPClient

func BuildOAuthHTTPClient(ctx context.Context, scopes []string, oAuthPort int) (*http.Client, error)

BuildOAuthHTTPClient takes the user through the three-legged OAuth flow. It opens a browser in the native OS or outputs a URL, then blocks until the redirect completes to the /oauth2callback URI. It returns an instance of an HTTP client that can be passed to the constructor of the YouTube client.

func Open

func Open(filename string, mediaType MediaType) (io.ReadCloser, int64, error)

func Run

func Run(ctx context.Context, transport *limiter.LimitTransport, config Config, videoReader io.ReadCloser) error

func SetSignalNotify

func SetSignalNotify(c chan os.Signal)

Types

type Cache

type Cache interface {
	Token() (*oauth2.Token, error)
	PutToken(*oauth2.Token) error
}

Cache specifies the methods that implement a Token cache.

type CacheFile

type CacheFile string

CacheFile implements Cache. Its value is the name of the file in which the Token is stored in JSON format.

func (CacheFile) PutToken

func (f CacheFile) PutToken(tok *oauth2.Token) error

PutToken stores the token in the token cache

func (CacheFile) Token

func (f CacheFile) Token() (*oauth2.Token, error)

Token retreives the token from the token cache

type CallbackStatus

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

CallbackStatus is returned from the oauth2 callback

type Config

type Config struct {
	Filename          string
	Thumbnail         string
	Caption           string
	Title             string
	Description       string
	Language          string
	CategoryId        string
	Tags              string
	Privacy           string
	Quiet             bool
	RateLimit         int
	MetaJSON          string
	MetaJSONOut       string
	LimitBetween      string
	PlaylistIDs       []string
	OAuthPort         int
	ShowAppVersion    bool
	Chunksize         int
	NotifySubscribers bool
	SendFileName      bool
	RecordingDate     Date
}

type Date

type Date struct {
	time.Time
}

func (*Date) Set

func (d *Date) Set(s string) (err error)

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) (err error)

type MediaType

type MediaType int
const (
	UNKNOWN MediaType = iota
	VIDEO
	IMAGE
	CAPTION
)

type Playlistx

type Playlistx struct {
	Id            string
	Title         string
	PrivacyStatus string
}

func (*Playlistx) AddVideoToPlaylist

func (plx *Playlistx) AddVideoToPlaylist(service *youtube.Service, videoID string) error

type VideoMeta

type VideoMeta struct {
	// snippet
	Title       string   `json:"title,omitempty"`
	Description string   `json:"description,omitempty"`
	CategoryId  string   `json:"categoryId,omitempty"`
	Tags        []string `json:"tags,omitempty"`

	// status
	PrivacyStatus          string `json:"privacyStatus,omitempty"`
	Embeddable             bool   `json:"embeddable,omitempty"`
	License                string `json:"license,omitempty"`
	PublicStatsViewable    bool   `json:"publicStatsViewable,omitempty"`
	PublishAt              Date   `json:"publishAt,omitempty"`
	MadeForKids            bool   `json:"madeForKids,omitempty"`
	ContainsSyntheticMedia bool   `json:"containsSyntheticMedia,omitempty"`

	// recording details
	RecordingDate Date `json:"recordingDate,omitempty"`

	PlaylistIDs    []string `json:"playlistIds,omitempty"`
	PlaylistTitles []string `json:"playlistTitles,omitempty"`

	// BCP-47 language code e.g. 'en','es'
	Language string `json:"language,omitempty"`

	Localizations map[string]youtube.VideoLocalization `json:"localizations,omitempty"`
}

func LoadVideoMeta

func LoadVideoMeta(config Config) (*VideoMeta, *youtube.Video, error)

Directories

Path Synopsis
cmd
youtubeuploader command
internal

Jump to

Keyboard shortcuts

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