本文为您介绍如何使用视频点播服务端 Java SDK 将音频、视频、图片等媒资文件上传至视频点播服务。
媒资上传到视频点播中会产生存储费用,具体参见媒资存储计费。
视频点播服务端 SDK 封装了 ApplyUploadInfo - 获取上传地址和凭证和 CommitUploadInfo - 确认上传接口以及上传逻辑,您只需简单配置即可进行上传。
说明
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.helper.VodUploadMediaProcessListener; import com.volcengine.helper.VodUploadProgressListener; import com.volcengine.model.beans.Functions; import com.volcengine.model.beans.FunctionsWorkflowTemplate; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.business.VodUploadTemplate; import com.volcengine.service.vod.model.request.VodUploadMediaRequest; import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse; import java.util.ArrayList; import java.util.List; public class VodUploadMediaDemo { public static void main(String[] args) { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); // 点播空间名称 String space = "your space name"; // 文件路径 String filePath = "your file path"; // 上传功能函数,可用于实现截图、设置媒资信息、触发工作流进行转码等功能。详见[上传功能函数说明](https://www.volcengine.com/docs/4/1185644) List<Functions> functionsList = new ArrayList<>(); Functions getMetaFunc = Functions.GetMetaFunction(); functionsList.add(getMetaFunc); Functions snapShotFunc = Functions.SnapShotFunction(2.3); functionsList.add(snapShotFunc); Functions addOptionInfo = Functions.AddOptionInfoFunction("hls测试视频", "test", "素材测试,视频文件", 0, true); functionsList.add(addOptionInfo); List<String> impTemplateIds = new ArrayList<>(); impTemplateIds.add("imp template id"); FunctionsWorkflowTemplate impTemplate = new FunctionsWorkflowTemplate(impTemplateIds, "imp"); List<String> transcodeTemplateIds = new ArrayList<>(); transcodeTemplateIds.add("transcode template id"); FunctionsWorkflowTemplate transcodeTemplate = new FunctionsWorkflowTemplate(transcodeTemplateIds, "transcode"); List<FunctionsWorkflowTemplate> templates = new ArrayList<>(); templates.add(impTemplate); templates.add(transcodeTemplate); Functions workflowFunc = Functions.StartWorkFlowFunction(templates); functionsList.add(workflowFunc); VodUploadMediaRequest vodUploadMediaRequest = VodUploadMediaRequest.newBuilder() .setSpaceName(space) .setFilePath(filePath) .setFileName("hello/vod/video.mp4") .setFunctions(JSON.toJSONString(functionsList)) .setStorageClass(1) // .setUploadHostPrefer("") .build(); // 需要进度条功能时添加相应 listener,如无需求,传 null 值即可 VodUploadProgressListener listener = new VodUploadMediaProcessListener(); try { VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMedia(vodUploadMediaRequest, listener); if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); System.exit(-1); } System.out.println(vodCommitUploadInfoResponse.toString()); System.out.println(vodCommitUploadInfoResponse.getResult().getData().getVid()); System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); } catch (Exception e) { e.printStackTrace(); } } }
视频点播服务端 SDK 封装了 URL 批量拉取上传 - UploadMediaByUrl 接口,您只需简单配置即可进行上传。完整的上传流程请见服务端上传。
说明
package com.volcengine.example.vod.upload; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.business.VodUploadTemplate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class VodUploadMediaByUrlDemo { public static void main(String[] args) throws Exception { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); try { com.volcengine.service.vod.model.request.VodUrlUploadRequest.Builder reqBuilder = com.volcengine.service.vod.model.request.VodUrlUploadRequest.newBuilder(); reqBuilder.setSpaceName("your space"); com.volcengine.service.vod.model.business.VodUrlUploadURLSet.Builder uRLSetsBuilder = com.volcengine.service.vod.model.business.VodUrlUploadURLSet.newBuilder(); uRLSetsBuilder.setSourceUrl(""); uRLSetsBuilder.setStorageClass(1); uRLSetsBuilder.setFileExtension(".mp4"); uRLSetsBuilder.setCallbackArgs("my java callback args"); Map<String, String> customUrlHeaders = new HashMap<>(); customUrlHeaders.put("your header key", "your header value"); uRLSetsBuilder.putAllCustomURLHeaders(customUrlHeaders); VodUploadTemplate impTemplate = VodUploadTemplate.newBuilder() .addTemplateIds("imp template id") .setTemplateType("imp") .build(); VodUploadTemplate transcodeTemplate = VodUploadTemplate.newBuilder() .addTemplateIds( "transcode template id") .setTemplateType("transcode") .build(); uRLSetsBuilder.addTemplates(impTemplate); uRLSetsBuilder.addTemplates(transcodeTemplate); reqBuilder.addURLSets(uRLSetsBuilder); com.volcengine.service.vod.model.response.VodUrlUploadResponse resp = vodService.uploadMediaByUrl(reqBuilder.build()); if (resp.getResponseMetadata().hasError()) { System.out.println(resp.getResponseMetadata().getError()); System.exit(-1); } System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.helper.VodUploadMediaProcessListener; import com.volcengine.helper.VodUploadProgressListener; import com.volcengine.model.beans.Functions; import com.volcengine.model.beans.FunctionsWorkflowTemplate; import com.volcengine.model.request.vod.VodStreamUploadRequest; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.business.VodUploadTemplate; import com.volcengine.service.vod.model.request.VodUploadMediaRequest; import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse; import java.util.ArrayList; import java.util.List; public class VodUploadMediaDemo { // 上传流 public static void main(String[] args) { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); // 点播空间名称 String space = "your space name"; // 上传功能函数,可用于实现截图、设置媒资信息、触发工作流进行转码等功能。详见[上传功能函数说明](https://www.volcengine.com/docs/4/1185644) List<Functions> functionsList = new ArrayList<>(); Functions getMetaFunc = Functions.GetMetaFunction(); functionsList.add(getMetaFunc); Functions snapShotFunc = Functions.SnapShotFunction(0); functionsList.add(snapShotFunc); Functions addOptionInfo = Functions.AddOptionInfoFunction("hls测试视频", "test", "素材测试,视频文件", 0, true); functionsList.add(addOptionInfo); List<String> impTemplateIds = new ArrayList<>(); impTemplateIds.add("imp template id"); FunctionsWorkflowTemplate impTemplate = new FunctionsWorkflowTemplate(impTemplateIds, "imp"); List<String> transcodeTemplateIds = new ArrayList<>(); transcodeTemplateIds.add("transcode template id"); FunctionsWorkflowTemplate transcodeTemplate = new FunctionsWorkflowTemplate(transcodeTemplateIds, "transcode"); List<FunctionsWorkflowTemplate> templates = new ArrayList<>(); templates.add(impTemplate); templates.add(transcodeTemplate); Functions workflowFunc = Functions.StartWorkFlowFunction(templates); functionsList.add(workflowFunc); VodStreamUploadRequest vodStreamUploadRequest = new VodStreamUploadRequest(); vodStreamUploadRequest.setSpaceName(space); vodStreamUploadRequest.setContent(null); vodStreamUploadRequest.setSize(0L); vodStreamUploadRequest.setChunkSize(0L); vodStreamUploadRequest.setFileName("hello/vod/video.mp4"); vodStreamUploadRequest.setFunctions(JSON.toJSONString(functionsList)); vodStreamUploadRequest.setStorageClass(1); try { VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.streamUploadMedia(vodStreamUploadRequest); if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); System.exit(-1); } System.out.println(vodCommitUploadInfoResponse.toString()); System.out.println(vodCommitUploadInfoResponse.getResult().getData().getVid()); System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); } catch (Exception e) { e.printStackTrace(); } } }
视频点播服务端 SDK 封装了 ApplyUploadInfo - 获取上传地址和凭证和 CommitUploadInfo - 确认上传接口以及上传逻辑,您只需简单配置即可进行上传。
说明
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.service.vod.Const; import com.volcengine.model.beans.Functions; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.request.VodUploadMaterialRequest; import com.volcengine.model.request.vod.VodStreamUploadRequest; import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse; import java.util.ArrayList; import java.util.List; /** * 上传素材示例 * 本示例代码主要演示如何上传视频素材。 * 同时,代码中以注释块的形式提供了上传图片、上传字幕并绑定至指定视频等其他示例,你可以根据需要取消相应代码块的注释来进行测试。 */ public class VodUploadMaterialDemo { public static void main(String[] args) { // 1. 初始化 VOD 服务 // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // 2. 设置访问密钥 Access Key ID (AK) 和 Secret Access Key (SK) // 请在 ~/.volc/config 文件中配置 AK/SK,或通过环境变量设置,SDK 会自动读取。详细说明,请见 https://www.volcengine.com/docs/4/65641 // 测试阶段,可按需临时调用 setAccessKey 和 setSecretKey 方法进行设置。但是请不要将 AK/SK 直接存储在项目代码里,以防泄漏。 // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); // 3. 设置上传参数 String space = "your space name"; String filePath = "your file path"; // 上传功能函数,可用于实现截图、设置媒资信息、触发工作流进行转码等功能。详见[上传功能函数说明](https://www.volcengine.com/docs/4/1185644) List<Functions> functionsList = new ArrayList<>(); Functions getMetaFunc = Functions.GetMetaFunction(); functionsList.add(getMetaFunc); // 截取指定时间的封面图 Functions snapShotFunc = Functions.SnapShotFunction(2.3); functionsList.add(snapShotFunc); // 添加视频标题、标签、分类等信息 Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试视频", "test", "素材测试,视频文件", Const.CategoryVideo, "mp4"); functionsList.add(addOptionInfo); VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() .setSpaceName(space) .setFilePath(filePath) .setFileType(Const.FileTypeMedia) .setFunctions(JSON.toJSONString(functionsList)) // .setUploadHostPrefer("") .build(); try { VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest, null); if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); System.exit(-1); } System.out.println(vodCommitUploadInfoResponse.toString()); System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); } catch (Exception e) { e.printStackTrace(); } } // // 视频素材流上传 // public static void main(String[] args) { // // Create a VOD instance in the specified region. // // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); // IVodService vodService = VodServiceImpl.getInstance(); // // // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // // vodService.setAccessKey("your ak"); // // vodService.setSecretKey("your sk"); // // String space = "your space name"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试视频", "test", "素材测试,视频文件", Const.CategoryVideo, "mp4"); // functionsList.add(addOptionInfo); // // VodStreamUploadRequest vodUploadMaterialRequest = new VodStreamUploadRequest(); // vodUploadMaterialRequest.setSpaceName(space); // vodUploadMaterialRequest.setFileType(Const.FileTypeMedia); // vodUploadMaterialRequest.setContent(null); // vodUploadMaterialRequest.setSize(0L); // vodUploadMaterialRequest.setChunkSize(0L); // vodUploadMaterialRequest.setFileName("hello/vod/material.mp4"); // vodUploadMaterialRequest.setFunctions(JSON.toJSONString(functionsList)); // vodUploadMaterialRequest.setStorageClass(1); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.streamUploadMaterial(vodUploadMaterialRequest); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } //// 图片素材上传 // public static void main(String[] args) { // // Create a VOD instance in the specified region. // // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); // IVodService vodService = VodServiceImpl.getInstance(); // // // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // // vodService.setAccessKey("your ak"); // // vodService.setSecretKey("your sk"); // // String space = "your space name"; // String filePath = "your file path"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试图片", "test", "素材测试,图片文件", Const.CategoryImage, "jpg"); // functionsList.add(addOptionInfo); // // VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() // .setSpaceName(space) // .setFilePath(filePath) // .setFileType(Const.FileTypeImage) // .setFileName("hello/vod/image.jpg") // .setFunctions(JSON.toJSONString(functionsList)) // .build(); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest,null); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } // // 图片素材流上传 // public static void main(String[] args) { // // Create a VOD instance in the specified region. // // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); // IVodService vodService = VodServiceImpl.getInstance(); // // // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // // vodService.setAccessKey("your ak"); // // vodService.setSecretKey("your sk"); // // String space = "your space name"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试图片", "test", "素材测试,图片文件", Const.CategoryImage, "jpg"); // functionsList.add(addOptionInfo); // // VodStreamUploadRequest vodUploadMaterialRequest = new VodStreamUploadRequest(); // vodUploadMaterialRequest.setSpaceName(space); // vodUploadMaterialRequest.setFileType(Const.FileTypeImage); // vodUploadMaterialRequest.setContent(null); // vodUploadMaterialRequest.setSize(0L); // vodUploadMaterialRequest.setChunkSize(0L); // vodUploadMaterialRequest.setFileName("hello/vod/material.jpeg"); // vodUploadMaterialRequest.setFunctions(JSON.toJSONString(functionsList)); // vodUploadMaterialRequest.setStorageClass(1); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.streamUploadMaterial(vodUploadMaterialRequest); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } //// 字幕素材上传 // public static void main(String[] args) { // // Create a VOD instance in the specified region. // // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); // IVodService vodService = VodServiceImpl.getInstance(); // // // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // // vodService.setAccessKey("your ak"); // // vodService.setSecretKey("your sk"); // // String space = "your space name"; // String filePath = "your file path"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试字幕", "test", "素材测试,字幕文件", Const.CategorySubtitle, "vtt"); // functionsList.add(addOptionInfo); // // FunctionsCaptionInput captionInput = new FunctionsCaptionInput(); // captionInput.setVid("your vid"); // 绑定的视频 Vid // captionInput.setFid("same as vid"); // 与 Vid 保持一致 // captionInput.setLanguage("eng-US"); // 字幕语言 // captionInput.setFormat("webvtt"); // 字幕格式 // captionInput.setTitle("字幕标题"); // 字幕标题 // captionInput.setActionType("upload"); // 操作类型,固定为 upload // captionInput.setAutoPublish(true); // 上传后是否自动发布 // captionInput.setSource("MU"); // 字幕来源,MU 表示 Material Upload // // 调用 CaptionUploadFunction 方法并传入对象 // Functions captionUploadFunction = CaptionUploadFunction(captionInput); // functionsList.add(captionUploadFunction); // // VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() // .setSpaceName(space) // .setFilePath(filePath) // .setFileType(Const.FileTypeObject) // .setFileName("hello/vod/object.vtt") // .setFunctions(JSON.toJSONString(functionsList)) // .build(); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest,null); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } // 字幕素材流上传 // public static void main(String[] args) { // // Create a VOD instance in the specified region. // // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); // IVodService vodService = VodServiceImpl.getInstance(); // // // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // // vodService.setAccessKey("your ak"); // // vodService.setSecretKey("your sk"); // // String space = "your space name"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试字幕", "test", "素材测试,字幕文件", Const.CategorySubtitle, "vtt"); // functionsList.add(addOptionInfo); // // VodStreamUploadRequest vodUploadMaterialRequest = new VodStreamUploadRequest(); // vodUploadMaterialRequest.setSpaceName(space); // vodUploadMaterialRequest.setFileType(Const.FileTypeObject); // vodUploadMaterialRequest.setContent(null); // vodUploadMaterialRequest.setSize(0L); // vodUploadMaterialRequest.setChunkSize(0L); // vodUploadMaterialRequest.setFileName("hello/vod/material.vtt"); // vodUploadMaterialRequest.setFunctions(JSON.toJSONString(functionsList)); // vodUploadMaterialRequest.setStorageClass(1); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.streamUploadMaterial(vodUploadMaterialRequest); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } }
进度条功能用于展示媒资上传进度,您可根据自身业务需求使用进度条功能。进度条功能采用观察者模式,您可以通过实现 VodUploadProgressListener 接口中的 progressChanged() 方法集成相应的业务逻辑。
说明
需使用 Java SDK v1.0.97 或以上版本。
示例代码如下:
package com.volcengine.helper; // 实现 VodUploadProgressListener 方法展示上传进度条 public class VodUploadMediaProcessListener implements VodUploadProgressListener { /** * 已成功上传总字节数 */ private long bytesUploaded = 0; /** * 原文件大小 */ private long fileSize = -1; // 使用进度条需要您重写回调方法,以下为参考示例 @Override public void progressChanged(VodUploadProgressEvent progressEvent) { long bytes = progressEvent.getByteSize(); VodUploadProgressEventType eventType = progressEvent.getEventType(); switch (eventType) { // 文件大小事件通知。SDK 获得文件大小时会触发该事件,通知文件大小 case FILE_SIZE_EVENT: this.fileSize = bytes; // 业务逻辑 System.out.println("file size is " + this.fileSize); break; // 传输事件通知,每个分片传输完成时会触发该事件,通知写入的分片字节数 case UPLOAD_BYTES_EVENT: this.bytesUploaded += bytes; if (this.bytesUploaded != this.fileSize - 1) { int percent = (int) (this.bytesUploaded * 100.0 / this.fileSize); System.out.println("uploaded " + percent + "%"); } else { int percent = 99; System.out.println("uploaded " + percent + "%"); } // 业务逻辑 break; default: break; } } }
您可以通过配置上传事件通知及时获取上传的进展和状态,详细介绍请见上传事件通知。
您可以通过工作流实现上传后自动转码,详细介绍请见工作流。
音视频资源上传至视频点播服务之后,默认为未发布状态,此时无法获取到播放地址。因此在音视频播放前,您需要先修改音视频状态为已发布。具体操作请参见修改音视频发布状态。
FileName 参数设置了文件名称,但上传后控制台显示"未命名",为什么没有显示我设置的文件名称? 这是因为在视频点播服务中,FileName 参数实际上指的是文件路径(即媒资在存储桶中的唯一标识和存储位置),而不是用户可见的文件名称。如需设置控制台显示的文件名称,您需要通过上传功能函数设置 FileTitle 参数。详见上传功能函数说明。
参数 | 说明 |
|---|---|
FileType |
|
RecordType |
|
Category |
|
通过正确组合这三个参数,可确保文件上传至正确的域名并生成匹配的元数据。示例:
FileType 设为 media 或留空。RecordType 设为 1 或留空。Category 无需设置(仅当 RecordType=2 时生效)。FileType 设为 image(图片使用图片上传域名)。RecordType 设为 2(素材)。Category 设为 image(进一步指定为图片素材)。FileType 设为 object(非音视频/图片的文件类型)。RecordType 设为 2(素材)。Category 设为 subtitle(进一步指定为字幕素材)。说明
通过服务端 SDK 上传素材时,接口已默认设置 RecordType 为 2,用户无需再设置。
视频点播当前仅支持手动分类。您可在视频点播控制台指定空间内的分类管理页面创建分类并获取分类 ID,具体请见分类管理。
如果您使用客户端上传 SDK 进行上传,您需要在应用服务端通过 AK 和 SK 在本地签出临时上传 Token,不依赖外网。如希望同时生成多个临时上传 Token,您可以循环调用生成方法。更多信息,请见客户端上传。
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.model.sts2.SecurityToken2; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.util.Time; public class GetUploadSts2Demo { public static void main(String[] args) throws Exception { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); //set expire time SecurityToken2 sts2WithExpire = vodService.getUploadSts2WithExpire(Time.Hour); System.out.println(JSON.toJSONString(sts2WithExpire)); //expire after one hour by default SecurityToken2 sts2 = vodService.getUploadSts2(); System.out.println(JSON.toJSONString(sts2)); } }
接口请求参数和返回参数说明详见 ApplyUploadInfo。
package com.volcengine.example.vod.upload; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; public class VodApplyUploadInfoDemo { public static void main(String[] args) throws Exception { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); try { com.volcengine.service.vod.model.request.VodApplyUploadInfoRequest.Builder reqBuilder = com.volcengine.service.vod.model.request.VodApplyUploadInfoRequest.newBuilder(); reqBuilder.setSpaceName("your SpaceName"); reqBuilder.setSessionKey("your SessionKey"); reqBuilder.setFileSize(0); reqBuilder.setFileType("your FileType"); reqBuilder.setFileName("your FileName"); reqBuilder.setStorageClass(1); reqBuilder.setFileExtension("your FileExtension"); reqBuilder.setClientNetWorkMode("your ClientNetWorkMode"); reqBuilder.setClientIDCMode("your ClientIDCMode"); reqBuilder.setNeedFallback(false); reqBuilder.setUploadHostPrefer("your UploadHostPrefer"); com.volcengine.service.vod.model.response.VodApplyUploadInfoResponse resp = vodService.applyUploadInfo(reqBuilder.build()); if (resp.getResponseMetadata().hasError()) { System.out.println(resp.getResponseMetadata().getError()); System.exit(-1); } System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }
接口请求参数和返回参数说明详见 QueryUploadTaskInfo。
package com.volcengine.example.vod.upload; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; public class VodQueryUploadTaskInfoDemo { public static void main(String[] args) throws Exception { // Create a VOD instance in the specified region. // IVodService vodService = VodServiceImpl.getInstance("cn-north-1"); IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); try { com.volcengine.service.vod.model.request.VodQueryUploadTaskInfoRequest.Builder reqBuilder = com.volcengine.service.vod.model.request.VodQueryUploadTaskInfoRequest.newBuilder(); reqBuilder.setJobIds("your JobIds"); com.volcengine.service.vod.model.response.VodQueryUploadTaskInfoResponse resp = vodService.queryUploadTaskInfo(reqBuilder.build()); if (resp.getResponseMetadata().hasError()) { System.out.println(resp.getResponseMetadata().getError()); System.exit(-1); } System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } } }
接口请求参数和返回参数说明详见 ApplyUploadInfo 及 CommitUploadInfo。
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.model.beans.Functions; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.request.VodUploadMediaRequest; import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse; import java.util.ArrayList; import java.util.List; public class VodUploadMediaDemo { public static void main(String[] args) { IVodService vodService = VodServiceImpl.getInstance(); // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65641. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); String space = "your space name"; String filePath = "your file path"; List<Functions> functionsList = new ArrayList<>(); Functions getMetaFunc = Functions.GetMetaFunction(); functionsList.add(getMetaFunc); Functions snapShotFunc = Functions.SnapShotFunction(2.3); functionsList.add(snapShotFunc); VodUploadMediaRequest vodUploadMediaRequest = VodUploadMediaRequest.newBuilder() .setSpaceName(space) .setFilePath(filePath) .setFileName("hello/vod/video") .setFunctions(JSON.toJSONString(functionsList)) .build(); try { VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMedia(vodUploadMediaRequest); if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); System.exit(-1); } System.out.println(vodCommitUploadInfoResponse.toString()); System.out.println(vodCommitUploadInfoResponse.getResult().getData().getVid()); System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); } catch (Exception e) { e.printStackTrace(); } } }
接口请求参数和返回参数说明详见 ApplyUploadInfo 及 CommitUploadInfo。
package com.volcengine.example.vod.upload; import com.alibaba.fastjson.JSON; import com.volcengine.service.vod.Const; import com.volcengine.model.beans.Functions; import com.volcengine.service.vod.IVodService; import com.volcengine.service.vod.impl.VodServiceImpl; import com.volcengine.service.vod.model.request.VodUploadMaterialRequest; import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse; import java.util.ArrayList; import java.util.List; public class VodUploadMaterialDemo { public static void main(String[] args) { IVodService vodService = VodServiceImpl.getInstance(); // call below method if you dont set ak and sk in ~/.vcloud/config vodService.setAccessKey("your ak"); vodService.setSecretKey("your sk"); String space = "your space name"; String filePath = "your file path"; List<Functions> functionsList = new ArrayList<>(); Functions getMetaFunc = Functions.GetMetaFunction(); functionsList.add(getMetaFunc); Functions snapShotFunc = Functions.SnapShotFunction(2.3); functionsList.add(snapShotFunc); Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试视频", "test", "素材测试,视频文件", Const.CategoryVideo, "mp4"); functionsList.add(addOptionInfo); VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() .setSpaceName(space) .setFilePath(filePath) .setFileType(Const.FileTypeMedia) .setFunctions(JSON.toJSONString(functionsList)) .build(); try { VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest); if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); System.exit(-1); } System.out.println(vodCommitUploadInfoResponse.toString()); System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); } catch (Exception e) { e.printStackTrace(); } } //// 上传图片素材 // public static void main(String[] args) { // IVodService vodService = VodServiceImpl.getInstance(); // // // call below method if you dont set ak and sk in ~/.vcloud/config // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); // // String space = "your space name"; // String filePath = "your file path"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试图片", "test", "素材测试,图片文件", Const.CategoryImage, "jpg"); // functionsList.add(addOptionInfo); // // VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() // .setSpaceName(space) // .setFilePath(filePath) // .setFileType(Const.FileTypeImage) // .setFileName("hello/vod/image") // .setFunctions(JSON.toJSONString(functionsList)) // .build(); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } //// 上传字幕素材 // public static void main(String[] args) { // IVodService vodService = VodServiceImpl.getInstance(); // // // call below method if you dont set ak and sk in ~/.vcloud/config // vodService.setAccessKey("your ak"); // vodService.setSecretKey("your sk"); // // String space = "your space name"; // String filePath = "your file path"; // // List<Functions> functionsList = new ArrayList<>(); // Functions getMetaFunc = Functions.GetMetaFunction(); // functionsList.add(getMetaFunc); // // Functions snapShotFunc = Functions.SnapShotFunction(2.3); // functionsList.add(snapShotFunc); // // Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试字幕", "test", "素材测试,字幕文件", Const.CategorySubtitle, "vtt"); // functionsList.add(addOptionInfo); // // VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder() // .setSpaceName(space) // .setFilePath(filePath) // .setFileType(Const.FileTypeObject) // .setFileName("hello/vod/object") // .setFunctions(JSON.toJSONString(functionsList)) // .build(); // // try { // VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest); // if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) { // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError()); // System.exit(-1); // } // System.out.println(vodCommitUploadInfoResponse.toString()); // System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid()); // System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId()); // } catch (Exception e) { // e.printStackTrace(); // } // } }