10

はい、それは多くの詳細を伴う長い質問です...私の質問は、Vimeo へのアップロードをセグメントでストリーミングするにはどうすればよいですか?

自分のマシンでコピーしてデバッグしたい場合: 必要なものは次のとおりです。

  • ここに私のコード。
  • ここにある Scribe ライブラリを含めます
  • 少なくとも 10 MB を超える有効なビデオ ファイル (mp4) を用意し、それをディレクトリに配置するC:\test.mp4か、そのコードを変更して自分の場所を指すようにします。
  • それでおしまい!助けてくれてありがとう!

大きな更新:ここのコードに Vimeo の有効な API キーとシークレットを残しました。したがって、Vimeo アカウントを持っている限り、アプリケーションを許可してトークンを入力すれば、すべてのコードが正常に機能するはずです。そのリンクからお気に入りの IDE のプロジェクトにコードをコピーして、私と一緒に修正できるかどうかを確認してください。動くコードをくれた人には賞金をあげます。ありがとう!ああ、このキーとシークレットを長期間使用することは期待しないでください。この問題が解決したら、削除します。:)

問題の概要:問題は、バイトの最後のチャンクを Vimeo に送信し、アップロードを確認すると、すべてのコンテンツの長さが最後のチャンクのみの長さであり、すべてのチャンクが結合されているわけではないという応答が返されることです。する必要があります。

SSCCE 注: SSCCE全体がここにあります。コンパイルできるように、別の場所に置きました。これは非常に短い (約 300 行) わけではありません、自己完結型であることがわかると思いますこれは確かにサンプルです!)。ただし、コードの関連部分をこの投稿に投稿しています。

これがどのように機能するかです:ストリーミング メソッドを介して Vimeo に動画をアップロードする場合 (この時点までのセットアップについては、アップロード API のドキュメントを参照してください)、いくつかのヘッダーを指定する必要があります: エンドポイント、コンテンツの長さ、およびコンテンツの種類. ドキュメントによると、他のヘッダーは無視されます。また、アップロードするファイルのバイト情報のペイロードも指定します。そして、署名して送信します( scribeを使用してこれを行う方法があります)。

私の問題: 1 回のリクエストでビデオを送信するだけで、すべてがうまく機能します。私の問題は、いくつかの大きなファイルをアップロードしている場合です。使用しているコンピューターには、そのバイト情報をすべてロードして HTTP PUT 要求に入れるのに十分なメモリがないため、分割する必要があります。 1 MB セグメント。これは物事がトリッキーになるところです。ドキュメントには、アップロードを「再開」できることが記載されているため、コードでそれを実行しようとしていますが、うまく機能していません。以下に、ビデオを送信するためのコードを示します。私の SSCCE がここにあることを思い出してください。

私が試したこと: Content-Range ヘッダーと関係があると思います... Content-Range ヘッダーの内容を変更するために試したことは次のとおりです...

  • 最初のチャンクにコンテンツ範囲ヘッダーを追加しない
  • コンテンツ範囲ヘッダーにプレフィックスを追加します (それぞれ前のヘッダーの組み合わせを使用):

    • 「バイト」
    • "bytes " (接続エラーをスローします。エラーについては一番下を参照してください) -->ドキュメントには、これが彼らが探しているものであることが示されていますが、ドキュメントにはタイプミスがあると確信しています。 「履歴書」の例の content-range ヘッダーは次の1001-339108/339108ようになります1001-339107/339108。だから…うん…
    • "バイト%20"
    • 「バイト:」
    • "バイト: "
    • "バイト="
    • 「バイト=」
  • コンテンツ範囲ヘッダーにプレフィックスとして何も追加しない

コードは次のとおりです。

/**
* Send the video data
*
* @return whether the video successfully sent
*/
private static boolean sendVideo(String endpoint, File file) throws FileNotFoundException, IOException {
  // Setup File
  long contentLength = file.length();
  String contentLengthString = Long.toString(contentLength);
  FileInputStream is = new FileInputStream(file);
  int bufferSize = 10485760; // 10 MB = 10485760 bytes
  byte[] bytesPortion = new byte[bufferSize];
  int byteNumber = 0;
  int maxAttempts = 1;
  while (is.read(bytesPortion, 0, bufferSize) != -1) {
    String contentRange = Integer.toString(byteNumber);
    long bytesLeft = contentLength - byteNumber;
    System.out.println(newline + newline + "Bytes Left: " + bytesLeft);
    if (bytesLeft < bufferSize) {
      //copy the bytesPortion array into a smaller array containing only the remaining bytes
      bytesPortion = Arrays.copyOf(bytesPortion, (int) bytesLeft);
      //This just makes it so it doesn't throw an IndexOutOfBounds exception on the next while iteration. It shouldn't get past another iteration
      bufferSize = (int) bytesLeft;
    }
    byteNumber += bytesPortion.length;
    contentRange += "-" + (byteNumber - 1) + "/" + contentLengthString;
    int attempts = 0;
    boolean success = false;
    while (attempts < maxAttempts && !success) {
      int bytesOnServer = sendVideoBytes("Test video", endpoint, contentLengthString, "video/mp4", contentRange, bytesPortion, first);
      if (bytesOnServer == byteNumber) {
        success = true;
      } else {
        System.out.println(bytesOnServer + " != " + byteNumber);
        System.out.println("Success is not true!");
      }
      attempts++;
    }
    first = true;
    if (!success) {
      return false;
    }
  }
  return true;
}

/**
* Sends the given bytes to the given endpoint
*
* @return the last byte on the server (from verifyUpload(endpoint))
*/
private static int sendVideoBytes(String videoTitle, String endpoint, String contentLength, String fileType, String contentRange, byte[] fileBytes, boolean addContentRange) throws FileNotFoundException, IOException {
  OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
  request.addHeader("Content-Length", contentLength);
  request.addHeader("Content-Type", fileType);
  if (addContentRange) {
    request.addHeader("Content-Range", contentRangeHeaderPrefix + contentRange);
  }
  request.addPayload(fileBytes);
  Response response = signAndSendToVimeo(request, "sendVideo on " + videoTitle, false);
  if (response.getCode() != 200 && !response.isSuccessful()) {
    return -1;
  }
  return verifyUpload(endpoint);
}

/**
* Verifies the upload and returns whether it's successful
*
* @param endpoint to verify upload to
* @return the last byte on the server
*/
public static int verifyUpload(String endpoint) {
  // Verify the upload
  OAuthRequest request = new OAuthRequest(Verb.PUT, endpoint);
  request.addHeader("Content-Length", "0");
  request.addHeader("Content-Range", "bytes */*");
  Response response = signAndSendToVimeo(request, "verifyUpload to " + endpoint, true);
  if (response.getCode() != 308 || !response.isSuccessful()) {
    return -1;
  }
  String range = response.getHeader("Range");
  //range = "bytes=0-10485759"
  return Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)) + 1;
  //The + 1 at the end is because Vimeo gives you 0-whatever byte where 0 = the first byte
}

signAndSendToVimeo メソッドは次のとおりです。

/**
* Signs the request and sends it. Returns the response.
*
* @param service
* @param accessToken
* @param request
* @return response
*/
public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
  System.out.println(newline + newline
          + "Signing " + description + " request:"
          + ((printBody && !request.getBodyContents().isEmpty()) ? newline + "\tBody Contents:" + request.getBodyContents() : "")
          + ((!request.getHeaders().isEmpty()) ? newline + "\tHeaders: " + request.getHeaders() : ""));
  service.signRequest(accessToken, request);
  printRequest(request, description);
  Response response = request.send();
  printResponse(response, description, printBody);
  return response;
}

printRequestメソッドとprintResponse メソッドからの出力の一部(例... すべての出力はここにあります) を次に示します。最初のチャンクに Content-Range ヘッダーを含めないでください)。contentRangeHeaderPrefixfirst

We're sending the video for upload!


Bytes Left: 15125120


Signing sendVideo on Test video request:
    Headers: {Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes%200-10485759/15125120}

sendVideo on Test video >>> Request
Headers: {Authorization=OAuth oauth_signature="zUdkaaoJyvz%2Bt6zoMvAFvX0DRkc%3D", oauth_version="1.0", oauth_nonce="340477132", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336004", Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes: 0-10485759/15125120}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d

sendVideo on Test video >>> Response
Code: 200
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}


Signing verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d request:
    Headers: {Content-Length=0, Content-Range=bytes */*}

verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Request
Headers: {Authorization=OAuth oauth_signature="FQg8HJe84nrUTdyvMJGM37dpNpI%3D", oauth_version="1.0", oauth_nonce="298157825", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336015", Content-Length=0, Content-Range=bytes */*}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d

verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Response
Code: 308
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-10485759, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body: 


Bytes Left: 4639360


Signing sendVideo on Test video request:
    Headers: {Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes: 10485760-15125119/15125120}

sendVideo on Test video >>> Request
Headers: {Authorization=OAuth oauth_signature="qspQBu42HVhQ7sDpzKGeu3%2Bn8tM%3D", oauth_version="1.0", oauth_nonce="183131870", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336015", Content-Length=15125120, Content-Type=video/mp4, Content-Range=bytes%2010485760-15125119/15125120}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d

sendVideo on Test video >>> Response
Code: 200
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}


Signing verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d request:
    Headers: {Content-Length=0, Content-Range=bytes */*}

verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Request
Headers: {Authorization=OAuth oauth_signature="IdhhhBryzCa5eYqSPKAQfnVFpIg%3D", oauth_version="1.0", oauth_nonce="442087608", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="5cb447d1fc4c3308e2c6531e45bcadf1", oauth_token="460633205c55d3f1806bcab04174ae09", oauth_timestamp="1334336020", Content-Length=0, Content-Range=bytes */*}
Verb: PUT
Complete URL: http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d

4639359 != 15125120
verifyUpload to http://174.129.125.96:8080/upload?ticket_id=5ea64d64547e38e5e3c121852b2d306d >>> Response
Success is not true!
Code: 308
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-4639359, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0}
Body: 

次に、コードはアップロードを完了し、ビデオ情報を設定します (完全なコードで確認できます)。

編集 2:コンテンツ範囲から「%20」を削除しようとしたところ、接続中にこのエラーが発生しました。「bytes%20」を使用するか、「bytes」をまったく追加しないでください...

Exception in thread "main" org.scribe.exceptions.OAuthException: Problems while creating connection.
    at org.scribe.model.Request.send(Request.java:70)
    at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
    at autouploadermodel.VimeoTest.signAndSendToVimeo(VimeoTest.java:282)
    at autouploadermodel.VimeoTest.sendVideoBytes(VimeoTest.java:130)
    at autouploadermodel.VimeoTest.sendVideo(VimeoTest.java:105)
    at autouploadermodel.VimeoTest.main(VimeoTest.java:62)
Caused by: java.io.IOException: Error writing to server
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:622)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:634)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1317)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at org.scribe.model.Response.<init>(Response.java:28)
    at org.scribe.model.Request.doSend(Request.java:110)
    at org.scribe.model.Request.send(Request.java:62)
    ... 5 more
Java Result: 1

編集 1:コードと出力を更新しました。まだ助けが必要です!

4

4 に答える 4

7

私はあなたの問題が単にこの行の結果である可能性があると思います:

request.addHeader("Content-Range", "bytes%20" + contentRange);

"bytes%20"単純に置き換えてみてください"bytes "

出力には、対応するヘッダーの内容が正しくないことがわかります。

Headers: {
    Content-Length=15125120,
    Content-Type=video/mp4,
    Content-Range=bytes%200-10485759/15125120     <-- INCORRECT
}

のトピックについてContent-Range...

コンテンツの最後のブロックの例には、のような範囲が必要14680064-15125119/15125120です。これはHTTP1.1仕様の一部です。

于 2012-04-16T00:00:57.170 に答える
2

ここ

 String contentRange = Integer.toString(byteNumber + 1);

最初の反復では、0からではなく、1から開始します。

ここ

 request.addHeader("Content-Length", contentLength);

現在のチャンクの長さではなく、ファイルコンテンツ全体の長さを入力します。

于 2012-04-11T18:37:13.853 に答える
0

vimeo API ページには次のように書かれています。 、プライバシーなど)。このメソッドを呼び出さないと、ビデオは処理されません。」

このコードを最後に追加して、動作させました。

    request = new OAuthRequest(Verb.PUT, "http://vimeo.com/api/rest/v2");
    request.addQuerystringParameter("method", "vimeo.videos.upload.complete");
    request.addQuerystringParameter("filename", video.getName());
    request.addQuerystringParameter("ticket_id", ticket);
    service.signRequest(token, request);        

    response = request.send();
于 2013-11-28T00:57:47.670 に答える