Skip to content

Commit 59de295

Browse files
"Added sample: java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UploadVideo.java"
1 parent becd887 commit 59de295

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

  • java/src/main/java/com/google/api/services/samples/youtube/cmdline/data
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.services.samples.youtube.cmdline.data;
16+
17+
import com.google.api.client.auth.oauth2.Credential;
18+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
19+
import com.google.api.client.googleapis.media.MediaHttpUploader;
20+
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
21+
import com.google.api.client.http.InputStreamContent;
22+
import com.google.api.services.samples.youtube.cmdline.Auth;
23+
import com.google.api.services.youtube.YouTube;
24+
import com.google.api.services.youtube.model.Video;
25+
import com.google.api.services.youtube.model.VideoSnippet;
26+
import com.google.api.services.youtube.model.VideoStatus;
27+
import com.google.common.collect.Lists;
28+
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.Calendar;
32+
import java.util.List;
33+
34+
/**
35+
* Upload a video to the authenticated user's channel. Use OAuth 2.0 to
36+
* authorize the request. Note that you must add your video files to the
37+
* project folder to upload them with this application.
38+
*
39+
* @author Jeremy Walker
40+
*/
41+
public class UploadVideo {
42+
43+
/**
44+
* Define a global instance of a Youtube object, which will be used
45+
* to make YouTube Data API requests.
46+
*/
47+
private static YouTube youtube;
48+
49+
/**
50+
* Define a global variable that specifies the MIME type of the video
51+
* being uploaded.
52+
*/
53+
private static final String VIDEO_FILE_FORMAT = "video/*";
54+
55+
private static final String SAMPLE_VIDEO_FILENAME = "sample-video.mp4";
56+
57+
/**
58+
* Upload the user-selected video to the user's YouTube channel. The code
59+
* looks for the video in the application's project folder and uses OAuth
60+
* 2.0 to authorize the API request.
61+
*
62+
* @param args command line args (not used).
63+
*/
64+
public static void main(String[] args) {
65+
66+
// This OAuth 2.0 access scope allows an application to upload files
67+
// to the authenticated user's YouTube channel, but doesn't allow
68+
// other types of access.
69+
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");
70+
71+
try {
72+
// Authorize the request.
73+
Credential credential = Auth.authorize(scopes, "uploadvideo");
74+
75+
// This object is used to make YouTube Data API requests.
76+
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
77+
"youtube-cmdline-uploadvideo-sample").build();
78+
79+
System.out.println("Uploading: " + SAMPLE_VIDEO_FILENAME);
80+
81+
// Add extra information to the video before uploading.
82+
Video videoObjectDefiningMetadata = new Video();
83+
84+
// Set the video to be publicly visible. This is the default
85+
// setting. Other supporting settings are "unlisted" and "private."
86+
VideoStatus status = new VideoStatus();
87+
status.setPrivacyStatus("public");
88+
videoObjectDefiningMetadata.setStatus(status);
89+
90+
// Most of the video's metadata is set on the VideoSnippet object.
91+
VideoSnippet snippet = new VideoSnippet();
92+
93+
// This code uses a Calendar instance to create a unique name and
94+
// description for test purposes so that you can easily upload
95+
// multiple files. You should remove this code from your project
96+
// and use your own standard names instead.
97+
Calendar cal = Calendar.getInstance();
98+
snippet.setTitle("Test Upload via Java on " + cal.getTime());
99+
snippet.setDescription(
100+
"Video uploaded via YouTube Data API V3 using the Java library " + "on " + cal.getTime());
101+
102+
// Set the keyword tags that you want to associate with the video.
103+
List<String> tags = new ArrayList<String>();
104+
tags.add("test");
105+
tags.add("example");
106+
tags.add("java");
107+
tags.add("YouTube Data API V3");
108+
tags.add("erase me");
109+
snippet.setTags(tags);
110+
111+
// Add the completed snippet object to the video resource.
112+
videoObjectDefiningMetadata.setSnippet(snippet);
113+
114+
InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,
115+
UploadVideo.class.getResourceAsStream("/sample-video.mp4"));
116+
117+
// Insert the video. The command sends three arguments. The first
118+
// specifies which information the API request is setting and which
119+
// information the API response should return. The second argument
120+
// is the video resource that contains metadata about the new video.
121+
// The third argument is the actual video content.
122+
YouTube.Videos.Insert videoInsert = youtube.videos()
123+
.insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent);
124+
125+
// Set the upload type and add an event listener.
126+
MediaHttpUploader uploader = thumbnailSet.getMediaHttpUploader();
127+
128+
// Indicate whether direct media upload is enabled. A value of
129+
// "True" indicates that direct media upload is enabled and that
130+
// the entire media content will be uploaded in a single request.
131+
// A value of "False," which is the default, indicates that the
132+
// request will use the resumable media upload protocol, which
133+
// supports the ability to resume an upload operation after a
134+
// network interruption or other transmission failure, saving
135+
// time and bandwidth in the event of network failures.
136+
uploader.setDirectUploadEnabled(false);
137+
138+
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
139+
public void progressChanged(MediaHttpUploader uploader) throws IOException {
140+
switch (uploader.getUploadState()) {
141+
case INITIATION_STARTED:
142+
System.out.println("Initiation Started");
143+
break;
144+
case INITIATION_COMPLETE:
145+
System.out.println("Initiation Completed");
146+
break;
147+
case MEDIA_IN_PROGRESS:
148+
System.out.println("Upload in progress");
149+
System.out.println("Upload percentage: " + uploader.getProgress());
150+
break;
151+
case MEDIA_COMPLETE:
152+
System.out.println("Upload Completed!");
153+
break;
154+
case NOT_STARTED:
155+
System.out.println("Upload Not Started!");
156+
break;
157+
}
158+
}
159+
};
160+
uploader.setProgressListener(progressListener);
161+
162+
// Call the API and upload the video.
163+
Video returnedVideo = videoInsert.execute();
164+
165+
// Print data about the newly inserted video from the API response.
166+
System.out.println("\n================== Returned Video ==================\n");
167+
System.out.println(" - Id: " + returnedVideo.getId());
168+
System.out.println(" - Title: " + returnedVideo.getSnippet().getTitle());
169+
System.out.println(" - Tags: " + returnedVideo.getSnippet().getTags());
170+
System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus());
171+
System.out.println(" - Video Count: " + returnedVideo.getStatistics().getViewCount());
172+
173+
} catch (GoogleJsonResponseException e) {
174+
System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : "
175+
+ e.getDetails().getMessage());
176+
e.printStackTrace();
177+
} catch (IOException e) {
178+
System.err.println("IOException: " + e.getMessage());
179+
e.printStackTrace();
180+
} catch (Throwable t) {
181+
System.err.println("Throwable: " + t.getMessage());
182+
t.printStackTrace();
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)