廢話不多說,請由程式碼學習,jar請自己去找google要

以下為程式碼

----------------------------------------------------------------------------

import com.google.gdata.client.Service;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaPlayer;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.CommentEntry;
import com.google.gdata.data.youtube.PlaylistEntry;
import com.google.gdata.data.youtube.PlaylistFeed;
import com.google.gdata.data.youtube.PlaylistLinkEntry;
import com.google.gdata.data.youtube.PlaylistLinkFeed;
import com.google.gdata.data.youtube.UserEventEntry;
import com.google.gdata.data.youtube.UserEventFeed;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.data.youtube.YtPublicationState;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Youtubeupload {

private String username = null;// 以youtube帳號申請的頻道名稱,非google帳號
private String password = null;//google密碼
private String developerKey = null;//developer_key請向google申請
private String filePath = null;//欲上傳檔案路徑
private String videoTitle = null;//設定影片資訊-標題
private String videoKeyword = "";//設定影片資訊-
private String description = "";//設定影片資訊-標題

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getDeveloperKey() {
return developerKey;
}

public void setDeveloperKey(String developerKey) {
this.developerKey = developerKey;
}

public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getVideoTitle() {
return videoTitle;
}

public void setVideoTitle(String videoTitle) {
this.videoTitle = videoTitle;
}

public String getVideoKeyword() {
return videoKeyword;
}

public void setVideoKeyword(String videoKeyword) {
this.videoKeyword = videoKeyword;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public void start() throws IOException, ServiceException {

if(username == null || password == null || developerKey == null || filePath == null || videoTitle == null)// 必要參數
throw new IllegalArgumentException("check username, password, developerKey, filePath, videoTitle again, please");


String uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/"
+ username + "/uploads";//上傳位址

YouTubeService service = new YouTubeService(
"gdataSample-YouTubeAuth-1", developerKey);
try {
service.setUserCredentials(username, password);
} catch (AuthenticationException e) {
System.out.println("Invalid login credentials.");
System.exit(1);
}

File videoFile = new File(filePath);
if (!videoFile.exists()) {
System.out.println("Sorry, that video doesn't exist.");
return;
}

String mimeType = "video/x-flv";
if (filePath.toLowerCase().indexOf(".flv") != -1)
mimeType = "video/x-flv";
else if (filePath.toLowerCase().indexOf(".mp4") != -1)
mimeType = "video/mp4";
else if (filePath.toLowerCase().indexOf(".wmv") != -1)
mimeType = "video/x-ms-wmv";

VideoEntry newEntry = new VideoEntry();

YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();

mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME,
"Nonprofit"));// //設定影片資訊-類別:非營利組織與行動主義
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(videoTitle);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword(videoKeyword);// 設定影片資訊-標籤,以「,」分隔
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent(description);// 設定影片資訊-說明
MediaFileSource ms = new MediaFileSource(videoFile, mimeType);
newEntry.setMediaSource(ms);

VideoEntry entry = service.insert(new URL(uploadUrl), newEntry);
if (entry.isDraft()) {

System.out.println("Video is not live");

YtPublicationState pubState = entry.getPublicationState();

if (pubState.getState() == YtPublicationState.State.PROCESSING) {
System.out.println("Video is still being processed.");
} else if (pubState.getState() == YtPublicationState.State.REJECTED) {

System.out.println("Video has been rejected because: "
+ pubState.getDescription());
System.out.println("For help visit: " + pubState.getHelpUrl());

} else if (pubState.getState() == YtPublicationState.State.FAILED) {

System.out.println("Video failed uploading because: "
+ pubState.getDescription());
System.out.println("For help visit:" + pubState.getHelpUrl());
}
}
System.out.println("Video uploaded successfully!");
}

public static void main(String[] args) throws ServiceException, IOException {
String username = "username";// 以youtube帳號申請的頻道名稱,非google帳號
String password = "password";//google密碼
String developerKey = "developerKey ";//向google youtube申請的key
String filePath = "C:\\test\\test.mp4";
String videoTitle = "影片標題";// 設定影片資訊-標題
String videoKeyword = "影片關鍵字一,影片關鍵字二";//以「,」號分隔設定的關鍵字
String description = "";//影片說明

Youtubeupload testupload = new Youtubeupload();
testupload.setUsername(username);
testupload.setPassword(password);
testupload.setDeveloperKey(developerKey);
testupload.setFilePath(filePath);
testupload.setVideoTitle(videoTitle);
testupload.setVideoKeyword(videoKeyword);
testupload.setDescription(description);
testupload.start();
}

}

Will(小威) 發表在 痞客邦 PIXNET 留言(1) 人氣()


留言列表 (1)

發表留言
  • android 初學者
  • 大大可以麻煩給個source code 讓我學習一下嗎 謝謝
    ftchinet@msn.com