本示例介绍如何使用Alibaba Cloud SDK for Java调用视频异步检测任务进行视频审核。

前提条件

在使用本教程之前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-green -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-green</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>2.8.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
    </dependencies>
  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

代码示例

  • 传入视频URL进行检测:
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.http.ProtocolType;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import java.util.*;
    
    public class Asyncscan1 {
    
        public static void main(String[] args) throws Exception {
            IClientProfile profile = DefaultProfile.getProfile(
                    "<your-region-id>", // 您的可用区ID
                    "<your-access-key-id>", // 您的AccessKey ID
                    "<your-access-key-secret>"); // 您的AccessKey Secret
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            // 指定请求方式
            request.setSysMethod(MethodType.POST);
            // 指定请求地址
            request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
            // 指定请求方法
            request.setSysUriPattern("/green/video/asyncscan");
            // 指定请求版本
            request.setSysVersion("2018-05-09");
            // 支持HTTP和HTTPS
            request.setSysProtocol(ProtocolType.HTTPS);
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", "https://player.alicdn.com/ad/citybrain.mp4");
    
            tasks.add(task);
            /**
             * 设置要检测的场景, 计费是按照该处传递的场景进行
             * 视频默认1秒截取一帧,您可以自行控制截帧频率,收费按照视频的截帧数量以及每一帧的检测场景进行计费
             * 举例:1分钟的视频截帧60张,检测色情和暴恐涉政2个场景,收费按照60张暴恐+60张暴恐涉政进行计费
             * porn: porn表示色情场景检测,terrorism表示暴恐涉政场景检测
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
    
            request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间
             */
            request.setSysConnectTimeout(3000);
            request.setSysReadTimeout(6000);
            try {
                CommonResponse response = client.getCommonResponse(request);
                JSONObject dataJson = JSON.parseObject(response.getData());
                System.out.println(JSON.toJSONString(dataJson, true));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
  • 上传本地视频文件进行检测:
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.http.ProtocolType;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    import java.util.*;
    
    public class Asyncscan2 {
    
        public static void main(String[] args) throws Exception {
            IClientProfile profile = DefaultProfile.getProfile(
                    "<your-region-id>", // 您的可用区ID
                    "<your-access-key-id>", // 您的AccessKey ID
                    "<your-access-key-secret>"); // 您的AccessKey Secret
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            // 指定请求方式
            request.setSysMethod(MethodType.POST);
            // 指定请求地址
            request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
            // 指定请求方法
            request.setSysUriPattern("/green/video/asyncscan");
            // 指定请求版本
            request.setSysVersion("2018-05-09");
            // 支持HTTP和HTTPS
            request.setSysProtocol(ProtocolType.HTTPS);
    
            /**
             * 如果您要检测的文件存于本地服务器上,可以通过下述代码片生成url,
             * 再将返回的url作为视频地址传递到服务端进行检测
             */
            String url = null;
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            try{
                url = uploader.uploadFile("D:/8014062.mp4");
            }catch (Exception e){
                e.printStackTrace();
            }
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
    
            tasks.add(task);
            /**
             * 设置要检测的场景, 计费是按照该处传递的场景进行
             * 视频默认1秒截取一帧,您可以自行控制截帧频率,收费按照视频的截帧数量以及每一帧的检测场景进行计费
             * 举例:1分钟的视频截帧60张,检测色情和暴恐涉政2个场景,收费按照60张暴恐+60张暴恐涉政进行计费
             * porn: porn表示色情场景检测,terrorism表示暴恐涉政场景检测
             */
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
    
            request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            /**
             * 请务必设置超时时间
             */
            request.setSysConnectTimeout(3000);
            request.setSysReadTimeout(10000);
            try {
                CommonResponse response = client.getCommonResponse(request);
                JSONObject dataJson = JSON.parseObject(response.getData());
                System.out.println(JSON.toJSONString(dataJson, true));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
  • 上传视频文件二进制数据进行检测:
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.aliyuncs.CommonRequest;
    import com.aliyuncs.CommonResponse;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.green.extension.uploader.ClientUploader;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.MethodType;
    import com.aliyuncs.http.ProtocolType;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    import org.apache.commons.io.FileUtils;
    import java.io.File;
    import java.util.*;
    
    public class AsyncscanEIO {
    
        public static void main(String[] args) throws Exception {
            IClientProfile profile = DefaultProfile.getProfile(
                    "<your-region-id>", // 您的可用区ID
                    "<your-access-key-id>", // 您的AccessKey ID
                    "<your-access-key-secret>"); // 您的AccessKey Secret
            IAcsClient client = new DefaultAcsClient(profile);
    
            CommonRequest request = new CommonRequest();
            // 指定请求方式
            request.setSysMethod(MethodType.POST);
            // 指定请求地址
            request.setSysDomain("green.cn-hangzhou.aliyuncs.com");
            // 指定请求方法
            request.setSysUriPattern("/green/video/asyncscan");
            // 指定请求版本
            request.setSysVersion("2018-05-09");
            // 支持HTTP和HTTPS
            request.setSysProtocol(ProtocolType.HTTPS);
    
            // 如果您要检测的文件存于本地服务器上,可以通过下述代码片生成url,
            // 再将返回的url作为视频地址传递到服务端进行检测
            ClientUploader uploader = ClientUploader.getVideoClientUploader(profile, false);
            byte[] videoBytes;
            String url = null;
            try{
                //这里读取本地文件作为二进制数据,当做输入做为示例, 实际使用中请直接替换成您的视频二进制数据
                videoBytes = FileUtils.readFileToByteArray(new File("/User/dfd345b0ff15f9134e18e254d8014062.mp4"));
                //上传到服务端
                url = uploader.uploadBytes(videoBytes);
            }catch (Exception e){
                e.printStackTrace();
            }
    
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task = new LinkedHashMap<String, Object>();
            task.put("dataId", UUID.randomUUID().toString());
            task.put("url", url);
            tasks.add(task);
            // 设置要检测的场景, 计费是按照该处传递的场景进行
            // 视频默认1秒截取一帧,您可以自行控制截帧频率,收费按照视频的截帧数量以及每一帧的检测场景进行计费
            // 举例:1分钟的视频截帧60张,检测色情和暴恐涉政2个场景,收费按照60张暴恐+60张暴恐涉政进行计费
            // porn: porn表示色情场景检测,terrorism表示暴恐涉政场景检测
            JSONObject data = new JSONObject();
            data.put("scenes", Arrays.asList("porn", "terrorism"));
            data.put("tasks", tasks);
    
            request.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
    
            // 请务必设置超时时间
            request.setSysConnectTimeout(3000);
            request.setSysReadTimeout(10000);
            try {
                CommonResponse response = client.getCommonResponse(request);
                JSONObject dataJson = JSON.parseObject(response.getData());
                System.out.println(JSON.toJSONString(dataJson, true));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }

执行结果

本文代码示例正确的返回结果类似如下:
{
    "msg":"OK",
    "code":200,
    "data":[
        {
            "msg":"OK",
            "code":200,
            "dataId":"14574ce8-a71e-4fc4-b70e-25082692f320",
            "taskId":"vipZ********0g",
            "url":"http://aligreen-misc-shan********"
        }
    ],
    "requestId":"8642C40F-EA5B-4D62-A86A-0D9831E512BF"
}