edit-icon download-icon

Add media workflow

Last Updated: Mar 28, 2018

The user can assemble activities provided by MPS, such as transcode activity and screenshot activity into a topology. The topology is as follows:

Topology type

  1. package com.aliyun.mts;
  2. import com.alibaba.fastjson.annotation.JSONField;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class Topology {
  6. @JSONField(name = "Activities")
  7. private Map<String, Activity> activities;
  8. @JSONField(name = "Dependencies")
  9. private Map<String, List<String>> dependencies;
  10. public Map<String, List<String>> dependencies() {
  11. return this.getDependencies();
  12. }
  13. public Topology() {
  14. }
  15. public Topology(Map<String, Activity> activities, Map<String, List<String>> dependencies) {
  16. this.setActivities(activities);
  17. this.setDependencies(dependencies);
  18. }
  19. public Map<String, Activity> getActivities() {
  20. return activities;
  21. }
  22. public Map<String, List<String>> getDependencies() {
  23. return dependencies;
  24. }
  25. public void setActivities(Map<String, Activity> activities) {
  26. this.activities = activities;
  27. }
  28. public void setDependencies(Map<String, List<String>> dependencies) {
  29. this.dependencies = dependencies;
  30. }
  31. }

ActivityType enumeration type

  1. package com.aliyun.mts;
  2. /**
  3. * Created by zhongyizengzy on 18/3/22.
  4. */
  5. public enum ActivityType {
  6. Start, Transcode, Snapshot, MediaInfo, Analysis, Cover, Summary, Censor, Report, UploadVerify, GenerateMasterPlayList, AudioGroup, SubtitleGroup, PackageConfig
  7. }

Activity type

  1. package com.aliyun.mts;
  2. import com.alibaba.fastjson.annotation.JSONField;
  3. import java.util.Map;
  4. public class Activity {
  5. @JSONField(name = "Type")
  6. private String type;
  7. @JSONField(name = "Parameters")
  8. private Map<String, String> parameters;
  9. public Activity() {
  10. }
  11. public Map<String, String> parameters() {
  12. return this.getParameters();
  13. }
  14. public Activity(String type, Map<String, String> parameters) {
  15. this.setType(type);
  16. this.setParameters(parameters);
  17. }
  18. public String getType() {
  19. return type;
  20. }
  21. public Map<String, String> getParameters() {
  22. return parameters;
  23. }
  24. public void setType(String type) {
  25. this.type = type;
  26. }
  27. public void setParameters(Map<String, String> parameters) {
  28. this.parameters = parameters;
  29. }
  30. }

AddMediaWorkflow type

  1. package com.aliyun.mts;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.exceptions.ServerException;
  7. import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowRequest;
  8. import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowResponse;
  9. import com.aliyuncs.profile.DefaultProfile;
  10. import org.apache.commons.lang.exception.ExceptionUtils;
  11. import java.io.UnsupportedEncodingException;
  12. import java.net.URLEncoder;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. public class AddMediaWorkflow {
  18. //Step 1 .set region:cn-hangzhou、cn-shenzhen、cn-shanghai、cn-beijing
  19. private static final String REGION = "cn-shenzhen";
  20. private static final String OSS_REGION = "oss-cn-shenzhen";
  21. private static final String mtsEndpoint = "mts." + REGION + ".aliyuncs.com";
  22. //Step 2.set accesskey & keySecret
  23. private static String accessKeyId = "";
  24. private static String accessKeySecret = "";
  25. //Step 3.set mps transcoding queue id
  26. private static String PIPELINE_ID = "38bba54d524448be92d277caaa8da118";
  27. private static DefaultAcsClient aliyunClient;
  28. static {
  29. try {
  30. DefaultProfile.addEndpoint(REGION, REGION, "Mts", mtsEndpoint);
  31. } catch (ClientException e) {
  32. System.out.print(ExceptionUtils.getStackTrace(e));
  33. System.exit(1);
  34. }
  35. aliyunClient = new DefaultAcsClient(DefaultProfile.getProfile(REGION, accessKeyId, accessKeySecret));
  36. }
  37. public static void main(String[] args) throws ClientException {
  38. AddMediaWorkflowRequest request = new AddMediaWorkflowRequest();
  39. request.setName("Sequential-workflow");
  40. Topology topology = new Topology();
  41. HashMap<String, Activity> activities = new HashMap<String, Activity>();
  42. Activity startNode = new Activity();
  43. startNode.setType(ActivityType.Start.name());
  44. HashMap<String, String> startNodeParameters = new HashMap<String, String>();
  45. JSONObject inputFile = new JSONObject();
  46. inputFile.put("Bucket", "mtb-sz-in");
  47. inputFile.put("Location", OSS_REGION);
  48. inputFile.put("ObjectPrefix", "media/");
  49. startNodeParameters.put("InputFile", inputFile.toString());
  50. startNodeParameters.put("PipelineId", PIPELINE_ID);
  51. startNode.setParameters(startNodeParameters);
  52. activities.put("startNode", startNode);
  53. Activity transcode = new Activity();
  54. transcode.setType(ActivityType.Transcode.name());
  55. HashMap<String, String> transcodingParameters = new HashMap<String, String>();
  56. JSONArray outputs = new JSONArray();
  57. JSONObject output = new JSONObject();
  58. try {
  59. output.put("OutputObject", URLEncoder.encode("transcode/{ObjectPrefix}/{FileName}.{ExtName}", "UTF-8"));
  60. } catch (UnsupportedEncodingException e) {
  61. System.exit(1);
  62. }
  63. output.put("TemplateId", "S00000001-000070");
  64. outputs.add(output);
  65. transcodingParameters.put("Outputs", outputs.toJSONString());
  66. transcodingParameters.put("OutputBucket", "mtb-sz-out");
  67. transcodingParameters.put("OutputLocation", OSS_REGION);
  68. transcode.setParameters(transcodingParameters);
  69. activities.put("transcodingNode", transcode);
  70. Activity report = new Activity();
  71. report.setType(ActivityType.Report.name());
  72. HashMap<String, String> reportParameters = new HashMap<String, String>();
  73. report.setParameters(reportParameters);
  74. activities.put("reportNode", report);
  75. topology.setActivities(activities);
  76. HashMap<String, List<String>> dependencies = new HashMap<String, List<String>>();
  77. dependencies.put("startNode", Arrays.asList("transcodingNode"));
  78. dependencies.put("transcodingNode", Arrays.asList("reportNode"));
  79. dependencies.put("reportNode", new ArrayList<String>());
  80. topology.setDependencies(dependencies);
  81. request.setTopology(JSONObject.toJSONString(topology));
  82. try {
  83. AddMediaWorkflowResponse response = aliyunClient.getAcsResponse(request);
  84. System.out.println(JSONObject.toJSONString(response));
  85. } catch (ServerException e) {
  86. System.out.println("Code:" + e.getErrCode() + " Msg:" + e.getMessage());
  87. } catch (ClientException e) {
  88. System.out.println("Code:" + e.getErrCode() + " Msg:" + e.getMessage());
  89. }
  90. }
  91. }
Thank you! We've received your feedback.