-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUploadThumbnail.java
185 lines (155 loc) · 7.69 KB
/
UploadThumbnail.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.guozh.upload;
/**
* Created by [email protected] on 2019/1/24
* Description:
*/
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.http.InputStreamContent;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTube.Thumbnails.Set;
import com.google.api.services.youtube.model.ThumbnailSetResponse;
import com.google.common.collect.Lists;
import java.io.*;
import java.util.List;
/**
* This sample uses MediaHttpUploader to upload an image and then calls the
* API's youtube.thumbnails.set method to set the image as the custom thumbnail
* for a video.
*
* @author Ibrahim Ulukaya
*/
public class UploadThumbnail {
/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;
/**
* Define a global variable that specifies the MIME type of the image
* being uploaded.
*/
private static final String IMAGE_FILE_FORMAT = "image/png";
/**
* Prompt the user to specify a video ID and the path for a thumbnail
* image. Then call the API to set the image as the thumbnail for the video.
*
* @param args command line args (not used).
*/
public static void main(String[] args) {
// This OAuth 2.0 access scope allows for full read/write access to the
// authenticated user's account.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "uploadthumbnail");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
"youtube-cmdline-uploadthumbnail-sample").build();
// Prompt the user to enter the video ID of the video being updated.
String videoId = getVideoIdFromUser();
System.out.println("You chose " + videoId + " to upload a thumbnail.");
// Prompt the user to specify the location of the thumbnail image.
File imageFile = getImageFromUser();
System.out.println("You chose " + imageFile + " to upload.");
// Create an object that contains the thumbnail image file's
// contents.
InputStreamContent mediaContent = new InputStreamContent(
IMAGE_FILE_FORMAT, new BufferedInputStream(new FileInputStream(imageFile)));
mediaContent.setLength(imageFile.length());
// Create an API request that specifies that the mediaContent
// object is the thumbnail of the specified video.
Set thumbnailSet = youtube.thumbnails().set(videoId, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = thumbnailSet.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
// Set the upload state for the thumbnail image.
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch (uploader.getUploadState()) {
// This value is set before the initiation request is
// sent.
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
// This value is set after the initiation request
// completes.
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
// This value is set after a media file chunk is
// uploaded.
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
// This value is set after the entire media file has
// been successfully uploaded.
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
// This value indicates that the upload process has
// not started yet.
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Upload the image and set it as the specified video's thumbnail.
ThumbnailSetResponse setResponse = thumbnailSet.execute();
// Print the URL for the updated video's thumbnail image.
System.out.println("\n================== Uploaded Thumbnail ==================\n");
System.out.println(" - Url: " + setResponse.getItems().get(0).getDefault().getUrl());
} catch (GoogleJsonResponseException e) {
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
}
}
/*
* Prompts the user to enter a YouTube video ID and return the user input.
*/
private static String getVideoIdFromUser() throws IOException {
String inputVideoId = "";
System.out.print("Please enter a video Id to update: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
inputVideoId = bReader.readLine();
if (inputVideoId.length() < 1) {
// Exit if the user does not specify a video ID.
System.out.print("Video Id can't be empty!");
System.exit(1);
}
return inputVideoId;
}
/*
* Prompt the user to enter the path for the thumbnail image being uploaded.
*/
private static File getImageFromUser() throws IOException {
String path = "";
System.out.print("Please enter the path of the image file to upload: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
path = bReader.readLine();
if (path.length() < 1) {
// Exit if the user does not provide a path to the image file.
System.out.print("Path can not be empty!");
System.exit(1);
}
return new File(path);
}
}