Skip to content

Latest commit

 

History

History
506 lines (386 loc) · 16.6 KB

README.md

File metadata and controls

506 lines (386 loc) · 16.6 KB

Asset

(asset())

Overview

Operations related to asset/vod api

Available Operations

getAll

Retrieve assets

Example Usage

package hello.world;

import java.lang.Exception;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.GetAssetsResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            GetAssetsResponse res = sdk.asset().getAll()
                .call();

            if (res.data().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Response

GetAssetsResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*

create

To upload an asset, your first need to request for a direct upload URL and only then actually upload the contents of the asset.

Once you created a upload link, you have 2 options, resumable or direct upload. For a more reliable experience, you should use resumable uploads which will work better for users with unreliable or slow network connections. If you want a simpler implementation though, you should just use a direct upload.

Direct Upload

For a direct upload, make a PUT request to the URL received in the url field of the response above, with the raw video file as the request body. response above:

Resumable Upload

Livepeer supports resumable uploads via Tus. This section provides a simple example of how to use tus-js-client to upload a video file.

From the previous section, we generated a URL to upload a video file to Livepeer on POST /api/asset/request-upload. You should use the tusEndpoint field of the response to upload the video file and track the progress:

# This assumes there is an `input` element of `type="file"` with id
`fileInput` in the HTML


const input = document.getElementById('fileInput');

const file = input.files[0];

const upload = new tus.Upload(file, {
  endpoint: tusEndpoint, // URL from `tusEndpoint` field in the
`/request-upload` response
  metadata: {
    filename,
    filetype: 'video/mp4',
  },
  uploadSize: file.size,
  onError(err) {
    console.error('Error uploading file:', err);
  },
  onProgress(bytesUploaded, bytesTotal) {
    const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
    console.log('Uploaded ' + percentage + '%');
  },
  onSuccess() {
    console.log('Upload finished:', upload.url);
  },
});

const previousUploads = await upload.findPreviousUploads();

if (previousUploads.length > 0) {
  upload.resumeFromPreviousUpload(previousUploads[0]);
}

upload.start();

Note: If you are using tus from node.js, you need to add a custom URL storage to enable resuming from previous uploads. On the browser, this is enabled by default using local storage. In node.js, add urlStorage: new tus.FileUrlStorage("path/to/tmp/file"), to the UploadFile object definition above.

Example Usage

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.NewAssetPayload;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.RequestUploadResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewAssetPayload req = NewAssetPayload.builder()
                .name("filename.mp4")
                .staticMp4(true)
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    TranscodeProfile.builder()
                        .bitrate(3000000L)
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .quality(23L)
                        .fps(30L)
                        .fpsDen(1L)
                        .gop("2")
                        .profile(TranscodeProfileProfile.H264_BASELINE)
                        .encoder(TranscodeProfileEncoder.H264)
                        .build()))
                .build();

            RequestUploadResponse res = sdk.asset().create()
                .request(req)
                .call();

            if (res.data().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Parameters

Parameter Type Required Description
request NewAssetPayload ✔️ The request object to use for the request.

Response

RequestUploadResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*

createViaUrl

Upload asset via URL

Example Usage

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.NewAssetFromUrlPayload;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.UploadAssetResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewAssetFromUrlPayload req = NewAssetFromUrlPayload.builder()
                .name("filename.mp4")
                .url("https://s3.amazonaws.com/my-bucket/path/filename.mp4")
                .staticMp4(true)
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    TranscodeProfile.builder()
                        .bitrate(3000000L)
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .quality(23L)
                        .fps(30L)
                        .fpsDen(1L)
                        .gop("2")
                        .profile(TranscodeProfileProfile.H264_BASELINE)
                        .encoder(TranscodeProfileEncoder.H264)
                        .build()))
                .build();

            UploadAssetResponse res = sdk.asset().createViaUrl()
                .request(req)
                .call();

            if (res.twoHundredApplicationJsonData().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Parameters

Parameter Type Required Description
request NewAssetFromUrlPayload ✔️ The request object to use for the request.

Response

UploadAssetResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*

get

Retrieves an asset

Example Usage

package hello.world;

import java.lang.Exception;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.GetAssetResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            GetAssetResponse res = sdk.asset().get()
                .assetId("<value>")
                .call();

            if (res.asset().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Parameters

Parameter Type Required Description
assetId String ✔️ ID of the asset

Response

GetAssetResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*

update

Patch an asset

Example Usage

package hello.world;

import java.lang.Exception;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.AssetPatchPayload;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.UpdateAssetResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            UpdateAssetResponse res = sdk.asset().update()
                .assetId("<value>")
                .assetPatchPayload(AssetPatchPayload.builder()
                    .name("filename.mp4")
                    .playbackPolicy(PlaybackPolicy.builder()
                        .type(Type.WEBHOOK)
                        .webhookId("1bde4o2i6xycudoy")
                        .webhookContext(Map.ofEntries(
                            Map.entry("streamerId", "my-custom-id")))
                        .refreshInterval(600d)
                        .build())
                    .build())
                .call();

            if (res.asset().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Parameters

Parameter Type Required Description
assetId String ✔️ ID of the asset
assetPatchPayload AssetPatchPayload ✔️ N/A

Response

UpdateAssetResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*

delete

Delete an asset

Example Usage

package hello.world;

import java.lang.Exception;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.DeleteAssetResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            DeleteAssetResponse res = sdk.asset().delete()
                .assetId("<value>")
                .call();

            // handle response
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Parameters

Parameter Type Required Description
assetId String ✔️ ID of the asset

Response

DeleteAssetResponse

Errors

Error Object Status Code Content Type
models/errors/SDKError 4xx-5xx */*