-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_video.go
61 lines (49 loc) · 1.5 KB
/
upload_video.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"google.golang.org/api/youtube/v3"
)
var (
filename = flag.String("filename", "", "Name of video file to upload")
title = flag.String("title", "Test Title", "Video title")
description = flag.String("description", "Test Description", "Video description")
category = flag.String("category", "22", "Video category")
keywords = flag.String("keywords", "", "Comma separated list of video keywords")
privacy = flag.String("privacy", "unlisted", "Video privacy status")
)
func main() {
flag.Parse()
if *filename == "" {
log.Fatalf("You must provide a filename of a video file to upload")
}
client := getClient(youtube.YoutubeUploadScope)
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
upload := &youtube.Video{
Snippet: &youtube.VideoSnippet{
Title: *title,
Description: *description,
CategoryId: *category,
},
Status: &youtube.VideoStatus{PrivacyStatus: *privacy},
}
// The API returns a 400 Bad Request response if tags is an empty string.
if strings.Trim(*keywords, "") != "" {
upload.Snippet.Tags = strings.Split(*keywords, ",")
}
call := service.Videos.Insert("snippet,status", upload)
file, err := os.Open(*filename)
defer file.Close()
if err != nil {
log.Fatalf("Error opening %v: %v", *filename, err)
}
response, err := call.Media(file).Do()
handleError(err, "")
fmt.Printf("Upload successful! Video ID: %v\n", response.Id)
}