This topic provides the sample code for using ApsaraVideo Media Processing (MPS) SDK for Java to create a workflow for HTTP-Live-Streaming (HLS) encryption.
Overview
You can also call the AddMediaWorkflow operation to create a workflow for HLS encryption. For more information, see AddMediaWorkflow.
Dependencies
Install MPS SDK for Java. For more information, see Install Alibaba Cloud SDK for Java.
Add other dependencies.
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68.noneautotype</version> </dependency>
Sample code
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.sdk.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowRequest;
import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowResponse;
/**
* ***** Usage notes ******
* This sample code creates a workflow for HLS encryption. If you perform HLS encryption for the first time, you must use this sample code to create a workflow for HLS encryption and run the workflow once to ensure that system configurations are valid.
* The topology of the workflow is complex. You must pay attention to the accuracy of parameter settings when you modify the topology. For more information, see "AddMediaWorkflow" at https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/api-mts-2014-06-18-addmediaworkflow.
* The KeyUri parameter specifies the endpoint used to obtain the decryption key. Make sure that the specified key uniform resource identifier (URI) is valid. Otherwise, the decryption fails.
* For more information about HLS encryption, see xxxxxxxxxxxx.
*/
public class MediaHlsWorkflow {
/** The ID of the MPS queue. To view the ID of an MPS queue, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane. */
final static String PIPELINE_ID = "be3e2a1de40a0cbf****";
/** The ID of the template. For more information about preset templates, see "Preset template details" at https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing. */
final static String TEMPLATE_ID = "S00000001-100020";
final static String OSS_LOCATION = "oss-cn-beijing";
/** The name of the input media bucket that is specified in the MPS console. */
final static String INPUT_BUCKET = "<your bucket name>";
/** The trigger path. The path must end with a forward slash (/). */
final static String INPUT_PATH = "mps-test/demo/";
/** The name of the output media bucket that is specified in the MPS console. */
final static String OUTPUT_BUCKET = "<your bucket name>";
/** The encryption type. Set this parameter to hls-aes-128. */
final static String ENCRYPTION_TYPE = "hls-aes-128";
/** The endpoint used to obtain the decryption key. Example: http://example.aliyundoc.com8. */
final static String HLS_KEY_URI = "http://127.0.0.1:8888";
final static String ACT_START = "Act-Start";
final static String ACT_ENCRYPTION = "Act-HLS-Encryption";
final static String ACT_REPORT = "Act-Report";
public static void main(String[] args) throws Exception {
// Initialize the client.
DefaultAcsClient client = InitClient.initMpsClient();
AddMediaWorkflowResponse response;
try {
response = addMediaWorkflow(client);
System.out.println("RequestId is:" + response.getRequestId());
} catch (Exception e) {
e.printStackTrace();
}
}
public static AddMediaWorkflowResponse addMediaWorkflow(IAcsClient client) throws Exception {
AddMediaWorkflowRequest request = new AddMediaWorkflowRequest();
request.setTopology(createWorkflow().toJSONString());
request.setName("Workflow for HLS encryption");
return client.getAcsResponse(request);
}
/**
* Create the topology of the workflow.
* @return
*/
private static JSONObject createWorkflow() {
JSONObject workflow = new JSONObject();
JSONObject activities = new JSONObject();
activities.put(ACT_START, createStartActivity());
activities.put(ACT_ENCRYPTION, createTrancodeActivity());
activities.put(ACT_REPORT, createReportActivity());
workflow.put("Activities", activities);
workflow.put("Dependencies", createDependencies());
return workflow;
}
/**
* Add a start node to the workflow.
* @return
*/
private static JSONObject createStartActivity() {
JSONObject startActivity = new JSONObject();
startActivity.put("Name", ACT_START);
startActivity.put("Type", "Start");
startActivity.put("Parameters", buildStartParameters());
return startActivity;
}
/**
* Add the input configurations.
* @return
*/
private static JSONObject buildStartParameters() {
JSONObject parameters = new JSONObject();
parameters.put("PipelineId", PIPELINE_ID);
// The structure of the input media bucket.
JSONObject inputFile = new JSONObject();
inputFile.put("Bucket", INPUT_BUCKET);
inputFile.put("Location", OSS_LOCATION);
inputFile.put("ObjectPrefix", INPUT_PATH);
parameters.put("InputFile", inputFile);
return parameters;
}
/**
* Add a transcoding node to the workflow.
* @return
*/
private static JSONObject createTrancodeActivity() {
JSONObject transcodeActivity = new JSONObject();
transcodeActivity.put("Name", ACT_ENCRYPTION);
transcodeActivity.put("Type", "Transcode");
JSONObject transcodeParamters = new JSONObject();
transcodeParamters.put("OutputBucket", OUTPUT_BUCKET);
transcodeParamters.put("OutputLocation", OSS_LOCATION);
transcodeParamters.put("Outputs", buildOutputsConfig());
transcodeActivity.put("Parameters", transcodeParamters);
return transcodeActivity;
}
/**
* Add the output configurations.
* @return
*/
private static JSONArray buildOutputsConfig() {
JSONArray outputs = new JSONArray();
JSONObject output = new JSONObject();
output.put("ObjectRegex", ACT_ENCRYPTION + "/{RunId}/{FileName}");
output.put("TemplateId", TEMPLATE_ID);
output.put("Encryption", buildEncryption());
outputs.add(output);
return outputs;
}
/**
* Add the encryption configurations.
* @return
*/
private static JSONObject buildEncryption() {
JSONObject encryption = new JSONObject();
encryption.put("Type", ENCRYPTION_TYPE);
encryption.put("KeyUri", HLS_KEY_URI);
return encryption;
}
/**
* Add the configurations of the Report node.
* @return
*/
private static JSONObject createReportActivity() {
JSONObject reportActivity = new JSONObject();
reportActivity.put("Name", ACT_REPORT);
JSONObject parameters = new JSONObject();
parameters.put("PublishType","Auto");
reportActivity.put("Parameters", parameters);
reportActivity.put("Type", "Report");
return reportActivity;
}
/**
* Add dependencies.
* The edges in the topology. The edges indicate the dependencies among activities.
* @return
*/
private static JSONObject createDependencies() {
JSONObject dependencies = new JSONObject();
JSONArray subActivityOfStart = new JSONArray();
subActivityOfStart.add(ACT_ENCRYPTION);
dependencies.put(ACT_START, subActivityOfStart);
JSONArray subActivityOfTranscode = new JSONArray();
subActivityOfTranscode.add(ACT_REPORT);
dependencies.put(ACT_ENCRYPTION, subActivityOfTranscode);
dependencies.put(ACT_REPORT, new JSONArray());
return dependencies;
}
}