Video Uploading Status in Vimeo
Asked Answered
P

2

10

I am uploading video in Vimeo using my Application,But some of my videos status are not getting updated Screenshot of the video in Vimeo website

I have followed the api document provided in this link: https://developer.vimeo.com/api/upload/videos:

I am using below code:

public  boolean sendVideo(String file1, String completeURi, String     endpoint, String id) throws FileNotFoundException, IOException {

    File file = new File(file1);

    long contentLength = file.length();
    String contentLengthString = Long.toString(contentLength);
    FileInputStream is = new FileInputStream(file);

    int bufferSize = 20485760; 
    byte[] bytesPortion = new byte[bufferSize];
    int byteNumber = 0;

    while (is.read(bytesPortion, 0, bufferSize) != -1) {
        String contentRange = Integer.toString(byteNumber);
        boolean success = false;
        int bytesOnServer = 0;

        while (!success) {
            long bytesLeft = contentLength - (bytesOnServer);
            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;
            }
             bytesOnServer = sendVideoBytes("Vimeo Video upload", endpoint, contentLengthString, "video/mp4", contentRange, bytesPortion, first,isuploaded);

            AppLog.e("bytesOnServer", "===contentLength===" + bytesOnServer +"&&=="+contentLengthString);

            if (bytesOnServer >= Integer.parseInt(contentLengthString)) {

                System.out.println("Success is  true!");
                return true;

            } else {
                contentRange = (bytesOnServer + 1) + "-" + (Integer.parseInt(contentLengthString)) + "/" + (Integer.parseInt(contentLengthString));

                System.out.println(bytesOnServer + " != " + contentLength);
                System.out.println("Success is not true!"+contentRange);

                success=false;
                first = true;
            }

        }



    }
    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,boolean isuploaded) 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", "bytes " + contentRange);
    }


    request.addPayload(fileBytes);

    Response response = signAndSendToVimeo(request, "sendVideo with file bytes " + videoTitle, false);



    if (response.getCode() != 200 && !response.isSuccessful()) {
        return -1;
    }


    return verifyUpload(endpoint, contentLength, contentRange,isuploaded);
}

/**
 * Verifies the upload and returns whether it's successful
 *
 * @param
 * @param contentLength
 * @param endpoint      to verify upload to  @return the last byte on the server
 */
public static int verifyUpload(String endpoint, String contentLength, String contentRange,boolean isuploaded) {
    // 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);

    AppLog.e("verifyUpload", "" + response.getCode());
    if (response.getCode() != 308 || !response.isSuccessful()) {
        return -1;
    }
    String range = response.getHeader("Range");

    AppLog.e("After verify","==range header contains"+Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)));
    //range = "bytes=0-10485759"
    return Integer.parseInt(range.substring(range.lastIndexOf("-") + 1)); //+1 remove
    //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
}

public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
    String newline = "\n";

    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(OAuthConstants.EMPTY_TOKEN, request);



    Response response = request.send();
   // AppLog.e("Uplaod Video aftre Response", "" + response.getCode());

    return response;
}

Can anyone help with a working code for Android?? Thanks in advance!!

Permanganate answered 6/10, 2016 at 7:14 Comment(8)
Can you provide more thorough details of what you are doing and share the relevant upload code?Hophead
Hello Kevin ,Thanks for replying ,I can't paste whole code here. Can u provide your contact email if possible?? I need support please help !!!!!Permanganate
Nancy, you should be able to post it in your original question by editing it. If you cannot do this, then I suggest another means of uploading it, such as on GitHub (perhaps a gist).Hophead
Ok Kevin I have updated my question.Permanganate
At first glance this code appears to follow the correct flow, but I have not run with it. Have you spent time debugging it? Are you getting responses for each network request, and if not, which request is failing for you?Hophead
The Range header that I get in Verify Reqest is greater than the file size ,I think that is the reason the status of video is still uploading ,But for a small file size (within my provided buffer size), I get correct range header.Permanganate
Hello Kevin , Can you please provide me your feedback?Permanganate
I have not had any time to run your code, but I suspect there is an issue with the input stream read, or your byte array. The read method will return the bytes that it was able to read, so you could use that for further debugging. Also, you may want to look into using a BufferedInputStream rather than a FileInputStream.Hophead
H
2

I recently had the same problem with vimeo. The api returns the state VIMUploadState_Succeeded for the upload process, but if you try to watch the video at vimeo.com it is stucked in the uploading state and it shows a black screen in the app when you try to reproduce the video.

I got the following answers from their support team:

It looks like this specific video didn't change from the uploaded state to a failure state.

Sorry but the videos provided in those links never finished uploading and are lost. They will need to be re-uploaded.

In our platform there are several videos uploaded everyday and it seems to have no pattern to identify when this uploading problem will happen. If it is not a problem for you to re-upload the video, vimeo can be a good solution since its price is really good in comparison to other video platforms, otherwise you should look for another video storage/playback solution.

Hedve answered 19/10, 2016 at 23:47 Comment(0)
C
0

It is difficult to check up your code without proper debugging.

Here's the workaround for your code with a little more functions added

You can check the code here

Cardiff answered 18/10, 2016 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.