使用java的google API實作youtube影片上傳by YouTubeService
文章推薦指數: 80 %
import java.util.regex.Pattern;. public class Youtubeupload { private String username = null;// 以youtube帳號申請的頻道名稱,非google帳號
魚魚的小水族箱
跳到主文
小小工程師的數位生活,stayhungry,stayfoolish
部落格全站分類:數位生活
相簿
部落格
留言
名片
Apr11Wed201216:59
使用java的googleAPI實作youtube影片上傳byYouTubeService
廢話不多說,請由程式碼學習,jar請自己去找google要
以下為程式碼
----------------------------------------------------------------------------
importcom.google.gdata.client.Service;importcom.google.gdata.client.youtube.YouTubeService;importcom.google.gdata.data.PlainTextConstruct;importcom.google.gdata.data.TextConstruct;importcom.google.gdata.data.media.MediaFileSource;importcom.google.gdata.data.media.mediarss.MediaCategory;importcom.google.gdata.data.media.mediarss.MediaDescription;importcom.google.gdata.data.media.mediarss.MediaKeywords;importcom.google.gdata.data.media.mediarss.MediaPlayer;importcom.google.gdata.data.media.mediarss.MediaTitle;importcom.google.gdata.data.youtube.CommentEntry;importcom.google.gdata.data.youtube.PlaylistEntry;importcom.google.gdata.data.youtube.PlaylistFeed;importcom.google.gdata.data.youtube.PlaylistLinkEntry;importcom.google.gdata.data.youtube.PlaylistLinkFeed;importcom.google.gdata.data.youtube.UserEventEntry;importcom.google.gdata.data.youtube.UserEventFeed;importcom.google.gdata.data.youtube.VideoEntry;importcom.google.gdata.data.youtube.VideoFeed;importcom.google.gdata.data.youtube.YouTubeMediaGroup;importcom.google.gdata.data.youtube.YouTubeNamespace;importcom.google.gdata.data.youtube.YtPublicationState;importcom.google.gdata.util.AuthenticationException;importcom.google.gdata.util.ServiceException;
importjava.io.BufferedReader;importjava.io.File;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.URL;importjava.util.List;importjava.util.regex.Matcher;importjava.util.regex.Pattern;
publicclassYoutubeupload{privateStringusername=null;//以youtube帳號申請的頻道名稱,非google帳號privateStringpassword=null;//google密碼privateStringdeveloperKey=null;//developer_key請向google申請privateStringfilePath=null;//欲上傳檔案路徑privateStringvideoTitle=null;//設定影片資訊-標題privateStringvideoKeyword="";//設定影片資訊-privateStringdescription="";//設定影片資訊-標題publicStringgetUsername(){returnusername;}
publicvoidsetUsername(Stringusername){this.username=username;}
publicStringgetPassword(){returnpassword;}
publicvoidsetPassword(Stringpassword){this.password=password;}
publicStringgetDeveloperKey(){returndeveloperKey;}
publicvoidsetDeveloperKey(StringdeveloperKey){this.developerKey=developerKey;}
publicStringgetFilePath(){returnfilePath;}
publicvoidsetFilePath(StringfilePath){this.filePath=filePath;}
publicStringgetVideoTitle(){returnvideoTitle;}
publicvoidsetVideoTitle(StringvideoTitle){this.videoTitle=videoTitle;}
publicStringgetVideoKeyword(){returnvideoKeyword;}
publicvoidsetVideoKeyword(StringvideoKeyword){this.videoKeyword=videoKeyword;}
publicStringgetDescription(){returndescription;}
publicvoidsetDescription(Stringdescription){this.description=description;}
publicvoidstart()throwsIOException,ServiceException{if(username==null||password==null||developerKey==null||filePath==null||videoTitle==null)//必要參數thrownewIllegalArgumentException("checkusername,password,developerKey,filePath,videoTitleagain,please");StringuploadUrl="http://uploads.gdata.youtube.com/feeds/api/users/"+username+"/uploads";//上傳位址
YouTubeServiceservice=newYouTubeService("gdataSample-YouTubeAuth-1",developerKey);try{service.setUserCredentials(username,password);}catch(AuthenticationExceptione){System.out.println("Invalidlogincredentials.");System.exit(1);}
FilevideoFile=newFile(filePath);if(!videoFile.exists()){System.out.println("Sorry,thatvideodoesn'texist.");return;}
StringmimeType="video/x-flv";if(filePath.toLowerCase().indexOf(".flv")!=-1)mimeType="video/x-flv";elseif(filePath.toLowerCase().indexOf(".mp4")!=-1)mimeType="video/mp4";elseif(filePath.toLowerCase().indexOf(".wmv")!=-1)mimeType="video/x-ms-wmv";
VideoEntrynewEntry=newVideoEntry();
YouTubeMediaGroupmg=newEntry.getOrCreateMediaGroup();
mg.addCategory(newMediaCategory(YouTubeNamespace.CATEGORY_SCHEME,"Nonprofit"));////設定影片資訊-類別:非營利組織與行動主義mg.setTitle(newMediaTitle());mg.getTitle().setPlainTextContent(videoTitle);mg.setKeywords(newMediaKeywords());mg.getKeywords().addKeyword(videoKeyword);//設定影片資訊-標籤,以「,」分隔mg.setDescription(newMediaDescription());mg.getDescription().setPlainTextContent(description);//設定影片資訊-說明MediaFileSourcems=newMediaFileSource(videoFile,mimeType);newEntry.setMediaSource(ms);
VideoEntryentry=service.insert(newURL(uploadUrl),newEntry);if(entry.isDraft()){
System.out.println("Videoisnotlive");
YtPublicationStatepubState=entry.getPublicationState();
if(pubState.getState()==YtPublicationState.State.PROCESSING){System.out.println("Videoisstillbeingprocessed.");}elseif(pubState.getState()==YtPublicationState.State.REJECTED){
System.out.println("Videohasbeenrejectedbecause:"+pubState.getDescription());System.out.println("Forhelpvisit:"+pubState.getHelpUrl());
}elseif(pubState.getState()==YtPublicationState.State.FAILED){
System.out.println("Videofaileduploadingbecause:"+pubState.getDescription());System.out.println("Forhelpvisit:"+pubState.getHelpUrl());}}System.out.println("Videouploadedsuccessfully!");}
publicstaticvoidmain(String[]args)throwsServiceException,IOException{Stringusername="username";//以youtube帳號申請的頻道名稱,非google帳號Stringpassword="password";//google密碼StringdeveloperKey="developerKey ";//向googleyoutube申請的keyStringfilePath="C:\\test\\test.mp4";StringvideoTitle="影片標題";//設定影片資訊-標題StringvideoKeyword="影片關鍵字一,影片關鍵字二";//以「,」號分隔設定的關鍵字Stringdescription="";//影片說明Youtubeuploadtestupload=newYoutubeupload();testupload.setUsername(username);testupload.setPassword(password);testupload.setDeveloperKey(developerKey);testupload.setFilePath(filePath);testupload.setVideoTitle(videoTitle);testupload.setVideoKeyword(videoKeyword);testupload.setDescription(description);testupload.start();}
}
全站熱搜
創作者介紹
Will(小威)
魚魚的小水族箱
Will(小威)發表在痞客邦留言(1)人氣()
E-mail轉寄
全站分類:數位生活個人分類:RD此分類上一篇:轉貼:rsh以root登入
此分類下一篇:[JAVA]List
延伸文章資訊
- 1YouTube Data API (v3): Introduction With Spring Boot - DZone
Java YouTube Data API. Learn more about app development using the YouTube Data API. YouTube is a ...
- 2Java Code Samples | YouTube Data API - Google Developers
Lets your application retrieve YouTube content while also enabling YouTube users to manage their ...
- 3youtube/api-samples - GitHub
Code samples for YouTube APIs, including the YouTube Data API, YouTube Analytics API, and YouTube...
- 4YouTube Java API入门_dnc8371的博客
在本教程中,我将介绍Google的YouTube API ,该API可让您使用YouTube的功能来启用应用程序。 YouTube是“杀手级”互联网应用程序之一,其流量占互联网总 ...
- 5Java YouTube類代碼示例- 純淨天空
本文整理匯總了Java中com.google.api.services.youtube.YouTube類的典型用法代碼示例。如果您正苦於以下問題:Java YouTube類的具體用法?Java ...