2017-10-12 108 views
6

爲了生成我的代碼,我修改了谷歌在gitHub上的樣本。當我使用Java youtube api上傳它時,是否可以向視頻添加註釋或「結束屏幕」?

一切都可以,視頻上傳沒有問題,但現在我需要在視頻中添加一些註釋或「結束屏幕」,因爲我需要在用戶看到我的「視頻預覽」後將用戶重定向到我的網站。

所以,工作流程是這樣的:

1)用戶將標題和描述在JSP頁面中。

2)當他點擊按鈕上的軟件需要存儲在我的數據庫用戶的憑據,然後通過所有參數,下面我張貼的方法。 3)該課程將視頻上傳到YouTube。

現在我的問題是: 有一個簡單的方法來「附加」我的鏈接,上傳的視頻?

我應該使用類似的註解,號召性動作或任何類型的疊加消息/按鈕上的視頻區。

覆蓋層是否持續了所有視頻持續時間或僅出現在視頻結尾處並不重要。

我希望有人能幫助我! 我希望谷歌能夠以更好的方式重寫文檔,因爲我瘋狂實施YouTube的API。

這是我的源代碼片段:

public static void upload(String jsonString, String videoPath, String title , String description, String articleTags, HttpServletRequest request) { 

    // This OAuth 2.0 access scope allows an application to upload files 
    // to the authenticated user's YouTube channel, but doesn't allow 
    // other types of access. 
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload"); 

    try { 

     // Authorize the request. 
     JSONObject jsonObj = new JSONObject(jsonString); 

     GoogleCredential credential = new GoogleCredential.Builder() 
              .setClientSecrets(jsonObj.getString("client_id"), jsonObj.getString("client_secret")) 
              .setJsonFactory(Auth.JSON_FACTORY).setTransport(Auth.HTTP_TRANSPORT).build() 
              .setRefreshToken(jsonObj.getString("refresh_token")).setAccessToken(jsonObj.getString("access_token")); 


     // This object is used to make YouTube Data API requests. 
     youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("virtual-cms-video-upload").build(); 

     System.out.println("Uploading: " + videoPath); 

     // Add extra information to the video before uploading. 
     Video videoObjectDefiningMetadata = new Video(); 

     // Set the video to be publicly visible. This is the default 
     // setting. Other supporting settings are "unlisted" and "private." 
     VideoStatus status = new VideoStatus(); 
     status.setPrivacyStatus("public"); 
     videoObjectDefiningMetadata.setStatus(status); 



     // Most of the video's metadata is set on the VideoSnippet object. 
     VideoSnippet snippet = new VideoSnippet(); 

     // This code uses a Calendar instance to create a unique name and 
     // description for test purposes so that you can easily upload 
     // multiple files. You should remove this code from your project 
     // and use your own standard names instead. 
     snippet.setTitle  (title  ); 
     snippet.setDescription(description); 



     if(!articleTags.trim().equals("")){    

      // Set the keyword tags that you want to associate with the video. 
      List<String> tags = new ArrayList<String>(); 

      for(int i = 0; i < articleTags.split(",").length ; i++){ 

       tags.add(articleTags); 

      } 

      snippet.setTags(tags); 

      // Add the completed snippet object to the video resource. 
      videoObjectDefiningMetadata.setSnippet(snippet); 

     } 

     //InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,UploadYouTubeVideo.class.getClassLoader().getResourceAsStream(videoPath)); 
     InputStreamContent mediaContent = new InputStreamContent(VIDEO_FILE_FORMAT,new java.io.FileInputStream(new File(videoPath))); 

     // Insert the video. The command sends three arguments. The first 
     // specifies which information the API request is setting and which 
     // information the API response should return. The second argument 
     // is the video resource that contains metadata about the new video. 
     // The third argument is the actual video content. 
     YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata, mediaContent); 

     // Set the upload type and add an event listener. 
     MediaHttpUploader uploader = videoInsert.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); 

     MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() { 
      public void progressChanged(MediaHttpUploader uploader) throws IOException { 
       switch (uploader.getUploadState()) { 
        case INITIATION_STARTED: 
         System.out.println("Initiation Started"); 
         break; 
        case INITIATION_COMPLETE: 
         System.out.println("Initiation Completed"); 
         break; 
        case MEDIA_IN_PROGRESS: 
         System.out.println("Upload in progress"); 
         System.out.println("Upload percentage: " + uploader.getProgress()); 
         break; 
        case MEDIA_COMPLETE: 
         System.out.println("Upload Completed!"); 
         break; 
        case NOT_STARTED: 
         System.out.println("Upload Not Started!"); 
         break; 
       } 
      } 
     }; 

     uploader.setProgressListener(progressListener); 

     // Call the API and upload the video. 
     Video returnedVideo = videoInsert.execute(); 

     // Print data about the newly inserted video from the API response. 
     System.out.println("\n================== Returned Video ==================\n"); 
     System.out.println(" - Id   : " + returnedVideo.getId()      ); 
     System.out.println(" - Title   : " + returnedVideo.getSnippet().getTitle()  ); 
     System.out.println(" - Tags   : " + returnedVideo.getSnippet().getTags()  ); 
     System.out.println(" - Privacy Status: " + returnedVideo.getStatus().getPrivacyStatus()); 
     System.out.println(" - Video Count : " + returnedVideo.getStatistics().getViewCount()); 

    } catch (Exception ex) { 
     System.err.println("Throwable: " + ex.getMessage()); 
     ex.printStackTrace(); 
    } 
} 

回答

3

不幸的是,這是不可能的,也永遠是據我可以告訴。

不能添加註釋是預期的行爲。請參閱此鏈接:https://issuetracker.google.com/issues/35166657 - 狀態:不會修復(預期的行爲)

顯然,最好的辦法是視頻內編排,但我不相信這是適合你的目的,除非它可以是專用視頻。

+0

非常感謝你!我已經放棄了這項工作,但我很高興有人回答我的問題!無論如何,我在互聯網上發現了同樣的東西,各種號召性用語都被棄用了!所以如果有人試圖做同樣的事情,這似乎是不可能的! –

相關問題