Video Moderation SDK is for scan videos to detect whether there is inappropriate content, such as adult or violence.

Overview

  • Asynchronized api call is supported.
  • You can submit video scanning tasks by provide urls, local file system path or binary file stream content。

Preparation

Please make sure preparation has been done before starting:
  1. Create AccessKeyId and AccessKeySecret in Alibaba Cloud console. Please refer to AccessKey Creation.
  2. Install dependecies, please refer to Install Java dependencies.

Submit video moderation tasks

Sample code:
  • Input video url to task submission
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONObject;
    3. import com.aliyuncs.DefaultAcsClient;
    4. import com.aliyuncs.IAcsClient;
    5. import com.aliyuncs.exceptions.ClientException;
    6. import com.aliyuncs.exceptions.ServerException;
    7. import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    8. import com.aliyuncs.http.FormatType;
    9. import com.aliyuncs.http.HttpResponse;
    10. import com.aliyuncs.profile.DefaultProfile;
    11. import com.aliyuncs.profile.IClientProfile;
    12. import java.util.*;
    13. /**
    14. * TODO Add class description here
    15. *
    16. * @author jialun.wjl
    17. * @create 2018/12/24 19:20
    18. */
    19. public class Main {
    20. public static void main(String[] args) throws Exception {
    21. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "your accessKeyId", " your accessKeySecret");
    22. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    23. IAcsClient client = new DefaultAcsClient(profile);
    24. VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
    25. videoAsyncScanRequest.setAcceptFormat(FormatType.JSON); // API return format
    26. videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // method
    27. List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
    28. Map<String, Object> task = new LinkedHashMap<String, Object>();
    29. task.put("dataId", UUID.randomUUID().toString());
    30. task.put("url", "Video http/https url which should be available to access by internete");
    31. tasks.add(task);
    32. /**
    33. * Set the scenes. Each scene will be chareged separately
    34. * By default, the frame cutting interval will be 1 second. You can customize it by settinng interval. Fee will be charged per frame per scene
    35. * porn: porn for adult content filtering, terrorism for terrorism or violence content filtering
    36. */
    37. JSONObject data = new JSONObject();
    38. data.put("scenes", Arrays.asList("porn", "terrorism"));
    39. data.put("tasks", tasks);
    40. videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    41. /**
    42. * Timeout setting is required.
    43. */
    44. videoAsyncScanRequest.setConnectTimeout(3000);
    45. videoAsyncScanRequest.setReadTimeout(6000);
    46. try {
    47. HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    48. if(httpResponse.isSuccess()){
    49. JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
    50. System.out.println(JSON.toJSONString(jsonObject, true));
    51. }else{
    52. System.out.println("response not success. status:" + httpResponse.getStatus());
    53. }
    54. } catch (ServerException e) {
    55. e.printStackTrace();
    56. } catch (ClientException e) {
    57. e.printStackTrace();
    58. }
    59. }
    60. }
  • Submit local video files to moderate
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONObject;
    3. import com.aliyuncs.DefaultAcsClient;
    4. import com.aliyuncs.IAcsClient;
    5. import com.aliyuncs.exceptions.ClientException;
    6. import com.aliyuncs.exceptions.ServerException;
    7. import com.aliyuncs.green.extension.uploader.ClientUploader;
    8. import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    9. import com.aliyuncs.http.FormatType;
    10. import com.aliyuncs.http.HttpResponse;
    11. import com.aliyuncs.profile.DefaultProfile;
    12. import com.aliyuncs.profile.IClientProfile;
    13. import java.util.*;
    14. public class Main {
    15. public static void main(String[] args) throws Exception {
    16. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", " Your accessKeyId", " Your accessKeySecret");
    17. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    18. IAcsClient client = new DefaultAcsClient(profile);
    19. VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
    20. videoAsyncScanRequest.setAcceptFormat(FormatType.JSON);
    21. videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
    22. /**
    23. * If your videos are stored in local file system, you can generate urls according the local code samples
    24. * Then you can use the generated url to submit moderation task
    25. */
    26. String url = null;
    27. ClientUploader uploader = ClientUploader.getVideoClientUploader(client);
    28. try{
    29. url = uploader.uploadFile("d:/test.mp4");
    30. }catch (Exception e){
    31. e.printStackTrace();
    32. }
    33. List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
    34. Map<String, Object> task = new LinkedHashMap<String, Object>();
    35. task.put("dataId", UUID.randomUUID().toString());
    36. task.put("url", url);
    37. tasks.add(task);
    38. /**
    39. * Set the scenes. Each scene will be chareged separately
    40. * By default, the frame cutting interval will be 1 second. You can customize it by settinng interval. Fee will be charged per frame per scene
    41. * porn: porn for adult content filtering, terrorism for terrorism or violence content filtering
    42. */
    43. JSONObject data = new JSONObject();
    44. data.put("scenes", Arrays.asList("porn", "terrorism"));
    45. data.put("tasks", tasks);
    46. videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    47. /**
    48. * Timeout setting is required
    49. */
    50. videoAsyncScanRequest.setConnectTimeout(3000);
    51. videoAsyncScanRequest.setReadTimeout(10000);
    52. try {
    53. HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    54. if(httpResponse.isSuccess()){
    55. JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
    56. System.out.println(JSON.toJSONString(jsonObject, true));
    57. }else{
    58. System.out.println("response not success. status:" + httpResponse.getStatus());
    59. }
    60. } catch (ServerException e) {
    61. e.printStackTrace();
    62. } catch (ClientException e) {
    63. e.printStackTrace();
    64. }
    65. }
    66. }
  • Submit file stream of video files to moderate
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONObject;
    3. import com.aliyuncs.DefaultAcsClient;
    4. import com.aliyuncs.IAcsClient;
    5. import com.aliyuncs.exceptions.ClientException;
    6. import com.aliyuncs.exceptions.ServerException;
    7. import com.aliyuncs.green.extension.uploader.ClientUploader;
    8. import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    9. import com.aliyuncs.http.FormatType;
    10. import com.aliyuncs.http.HttpResponse;
    11. import com.aliyuncs.profile.DefaultProfile;
    12. import com.aliyuncs.profile.IClientProfile;
    13. import java.util.*;
    14. public class Main {
    15. public static void main(String[] args) throws Exception {
    16. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
    17. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    18. IAcsClient client = new DefaultAcsClient(profile);
    19. VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
    20. videoAsyncScanRequest.setAcceptFormat(FormatType.JSON);
    21. videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
    22. /**
    23. * You can use the following code to transfer your local videos to internet available url
    24. * Then use the url to submit moderation task
    25. */
    26. ClientUploader uploader = ClientUploader.getVideoClientUploader(client);
    27. byte[] videoBytes = null;
    28. String url = null;
    29. try{
    30. //please replace with your local file binary stream
    31. videoBytes = FileUtils.readFileToByteArray(new File("/Users/01fb4ab6420b5f34623e13b82b51ef87.jpg"));
    32. //uploading
    33. url = uploader.uploadBytes(videoBytes);
    34. }catch (Exception e){
    35. System.out.println("upload file to server fail.", e);
    36. }
    37. List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
    38. Map<String, Object> task = new LinkedHashMap<String, Object>();
    39. task.put("dataId", UUID.randomUUID().toString());
    40. task.put("url", url);
    41. tasks.add(task);
    42. /**
    43. * Set the scenes. Each scene will be chareged separately
    44. * By default, the frame cutting interval will be 1 second. You can customize it by settinng interval. Fee will be charged per frame per scene
    45. * porn: porn for adult content filtering, terrorism for terrorism or violence content filtering
    46. */
    47. JSONObject data = new JSONObject();
    48. data.put("scenes", Arrays.asList("porn", "terrorism"));
    49. data.put("tasks", tasks);
    50. videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    51. /**
    52. * timeout setting is required
    53. */
    54. videoAsyncScanRequest.setConnectTimeout(3000);
    55. videoAsyncScanRequest.setReadTimeout(10000);
    56. try {
    57. HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    58. if(httpResponse.isSuccess()){
    59. JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
    60. System.out.println(JSON.toJSONString(jsonObject, true));
    61. }else{
    62. System.out.println("response not success. status:" + httpResponse.getStatus());
    63. }
    64. } catch (ServerException e) {
    65. e.printStackTrace();
    66. } catch (ClientException e) {
    67. e.printStackTrace();
    68. }
    69. }
    70. }
  • Submit live video streaming to moderate
    1. import com.alibaba.fastjson.JSON;
    2. import com.alibaba.fastjson.JSONObject;
    3. import com.aliyuncs.DefaultAcsClient;
    4. import com.aliyuncs.IAcsClient;
    5. import com.aliyuncs.exceptions.ClientException;
    6. import com.aliyuncs.exceptions.ServerException;
    7. import com.aliyuncs.green.extension.uploader.ClientUploader;
    8. import com.aliyuncs.green.model.v20180509.VideoAsyncScanRequest;
    9. import com.aliyuncs.http.FormatType;
    10. import com.aliyuncs.http.HttpResponse;
    11. import com.aliyuncs.profile.DefaultProfile;
    12. import com.aliyuncs.profile.IClientProfile;
    13. import java.util.*;
    14. public class Main {
    15. public static void main(String[] args) throws Exception {
    16. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
    17. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
    18. IAcsClient client = new DefaultAcsClient(profile);
    19. VideoAsyncScanRequest videoAsyncScanRequest = new VideoAsyncScanRequest();
    20. videoAsyncScanRequest.setAcceptFormat(FormatType.JSON);
    21. videoAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
    22. List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
    23. Map<String, Object> task = new LinkedHashMap<String, Object>();
    24. task.put("dataId", UUID.randomUUID().toString());
    25. //url of live video stream
    26. task.put("url", "http://xxxx/test.mp4");
    27. tasks.add(task);
    28. /**
    29. * Set the scenes. Each scene will be chareged separately
    30. * By default, the frame cutting interval will be 1 second. You can customize it by settinng interval. Fee will be charged per frame per scene
    31. * porn: porn for adult content filtering, terrorism for terrorism or violence content filtering
    32. */
    33. JSONObject data = new JSONObject();
    34. data.put("scenes", Arrays.asList("porn", "terrorism"));
    35. data.put("live", true);
    36. data.put("tasks", tasks);
    37. videoAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    38. /**
    39. * Timeout setting is required
    40. */
    41. videoAsyncScanRequest.setConnectTimeout(3000);
    42. videoAsyncScanRequest.setReadTimeout(10000);
    43. try {
    44. HttpResponse httpResponse = client.doAction(videoAsyncScanRequest);
    45. if(httpResponse.isSuccess()){
    46. JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
    47. System.out.println(JSON.toJSONString(jsonObject, true));
    48. }else{
    49. System.out.println("response not success. status:" + httpResponse.getStatus());
    50. }
    51. } catch (ServerException e) {
    52. e.printStackTrace();
    53. } catch (ClientException e) {
    54. e.printStackTrace();
    55. }
    56. }
    57. }

Query moderation results

Sample Code:
  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.exceptions.ServerException;
  7. import com.aliyuncs.green.model.v20180509.VideoAsyncScanResultsRequest;
  8. import com.aliyuncs.http.FormatType;
  9. import com.aliyuncs.http.HttpResponse;
  10. import com.aliyuncs.profile.DefaultProfile;
  11. import com.aliyuncs.profile.IClientProfile;
  12. import java.util.*;
  13. /**
  14. * TODO Add class description here
  15. *
  16. * @author jialun.wjl
  17. * @create 2018/12/24 19:22
  18. */
  19. public class Main {
  20. public static void main(String[] args) throws Exception {
  21. IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "Your accessKeyId", "Your accessKeySecret");
  22. DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
  23. IAcsClient client = new DefaultAcsClient(profile);
  24. VideoAsyncScanResultsRequest videoAsyncScanResultsRequest = new VideoAsyncScanResultsRequest();
  25. videoAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON);
  26. List<String> taskList = new ArrayList<String>();
  27. // Input the taskId which return when submitting moderation tasks
  28. taskList.add("vi3pnWxOlikyx6KJNtY7Naza-1pZ$MN");
  29. videoAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskList).getBytes("UTF-8"), "UTF-8", FormatType.JSON);
  30. /**
  31. * Timeout setting is required
  32. */
  33. videoAsyncScanResultsRequest.setConnectTimeout(3000);
  34. videoAsyncScanResultsRequest.setReadTimeout(6000);
  35. try {
  36. HttpResponse httpResponse = client.doAction(videoAsyncScanResultsRequest);
  37. if(httpResponse.isSuccess()){
  38. JSONObject jsonObject = JSON
  39. .parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
  40. System.out.println(JSON.toJSONString(jsonObject, true));
  41. }else{
  42. System.out.println("response not success. status:" + httpResponse.getStatus());
  43. }
  44. } catch (ServerException e) {
  45. e.printStackTrace();
  46. } catch (ClientException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }