public static String uploadVideoFileInChunks()

in templates/java/src/main/java/com/facebook/ads/utils/VideoUtils.java [73:149]


  public static String uploadVideoFileInChunks(AdAccount account, File videoFile, int maxRetry, boolean waitForEncoding, ProgressCallback callback) throws APIException, IOException {
    int totalRetry = 0;
    long fileSize = videoFile.length();
    FileInputStream is = null;
    String sessionId;
    String videoId;
    try {
      is = new FileInputStream(videoFile);
      JsonObject start = account.createAdVideo()
          .setUploadPhase(EnumUploadPhase.VALUE_START)
          .setFileSize(fileSize)
          .execute()
          .getRawResponseAsJsonObject();
      sessionId = start.get("upload_session_id").getAsString();
      videoId = start.get("video_id").getAsString();
      long startOffset = Long.parseLong(start.get("start_offset").getAsString());
      long endOffset = Long.parseLong(start.get("end_offset").getAsString());
      byte[] chunk = null;

      boolean transferAllDone = false;
      while (!transferAllDone) {
        int chunkSize = (int)(endOffset - startOffset);
        if (chunk == null || chunkSize != chunk.length) chunk = new byte[chunkSize];
        is.read(chunk);
        JsonObject transfer = null;
        while (totalRetry < maxRetry) {
          try {
            transfer = account.createAdVideo().setUploadPhase(EnumUploadPhase.VALUE_TRANSFER)
                .setVideoFileChunk(chunk)
                .setStartOffset(startOffset)
                .setUploadSessionId(sessionId)
                .execute()
                .getRawResponseAsJsonObject();
            if (endOffset == fileSize) {
              if (callback != null) callback.onProgressUpdate(ProgressCallback.EVENT_UPLOAD_COMPLETE, 100);
              transferAllDone = true;
            } else {
              startOffset = Long.parseLong(transfer.get("start_offset").getAsString());
              endOffset = Long.parseLong(transfer.get("end_offset").getAsString());
              if (callback != null) {
                callback.onProgressUpdate(ProgressCallback.EVENT_UPLOAD_PROGRESS, (int)(startOffset * 100L / fileSize));
              }
            }
            break;
          } catch (APIException e) {
            totalRetry++;
            if (totalRetry >= maxRetry) throw new APIException(
              "Video upload failed after max retry. Video ID: " + videoId
                + "; Upload session ID: " + sessionId
                + "; Last start offset: " + startOffset
                + "; Last end offset: " + endOffset,
              e
            );
          }
        }
      }
    } finally {
      if (is != null) is.close();
    }

    boolean success = account.createAdVideo()
        .setUploadPhase(EnumUploadPhase.VALUE_FINISH)
        .setUploadSessionId(sessionId)
        .setTitle("chunked video upload")
        .execute()
        .getRawResponseAsJsonObject().get("success").getAsBoolean();

    if (!success) {
      throw new APIException("Video upload failed unexpectedly. Video ID: " + videoId + ", Upload session ID: " + sessionId);
    }
    if (waitForEncoding) {
      if (!waitForVideoEncoding(videoId, account.getContext(), callback)) {
        throw new APIException("Video encoding failed. videoId=" + videoId);
      }
    }
    return videoId;
  }