所有文档

          音视频点播 VOD

          快速入门

          一次完整的 iOS 客户端 VOD 应用场景的主要步骤包括:

          1. 完成客户端鉴权,初始化Client
          2. 生成MediaID
          3. 上传媒体资源文件
          4. 上传成功,处理媒体资源文件
          5. 查询媒资状态

          鉴权初始化BOSClient和VODClient对象

          本文档默认了读者已经对BOS服务有一定的了解。如果对BOS仍有疑问,请参阅BOS产品文档

          百度智能云采用统一的API鉴权认证机制,在使用百度智能云服务前需要先进行鉴权认证。(详情请参阅BOS 访问控制

          鉴权方式

          首先确定鉴权方式,以临时授权方式(STS或者AKSK方式,二者只需选择其一即可)举例。 VODClient是与VOD服务交互的客户端。VOD相关操作都是通过VOD Client完成的。

          基本流程:

          1. 初始化Credentials对象,传入用户的鉴权key;
          2. 初始化对应的Client对象。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          void example (void) {
              //STS方式获得鉴权credentials对象,推荐采用
              BCESTSCredentials* credentials = [BCESTSCredentials new];
              credentials.accessKey = @"<your ak>";
              credentials.secretKey = @"<your sk>";
              credentials.sessionToken = @"<your session token>";
              
              //或者AKSK方式鉴权credentials对象,两种方式任选其一即可
              //BCECredentials* credentials = [BCECredentials new];
              //credentials.accessKey = @"<your ak>";
              //credentials.secretKey = @"<your sk>";
              
              
              //初始化鉴权后的BOSClient对象
              BOSClientConfiguration* bosConfig = [BOSClientConfiguration new];
              bosConfig.credentials = credentials;
              BosClient *bosClient = [[BOSClient alloc] initWithConfiguration:bosConfig];
              
              //初始化鉴权后的VODClient对象
              VODClientConfiguration* vodConfig = [VODClientConfiguration new];
              vodConfig.credentials = credentials;
              VodClient *vodClient = [[VODClient alloc] initWithConfiguration:vodConfig];
          }

          生成媒资并获取MediaID

          生成媒资接口用于:

          • 生成媒资的唯一标识mediaId、
          • 生成用于存储源视频文件的sourceBucket,sourceBucket对用户开放写权限,对于用户的BOS空间不可见。
          • 生成源媒资文件在BOS存储中的key。

          基本流程:

          1. 通过完成鉴权初始化的vodClient生成用于请求媒资接口的BCETask;
          2. 执行该Task,在任务成功的回调中获取媒资接口返回的相应数据。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example (void) {
          VODGenerateMediaIDRequest* request = [[VODGenerateMediaIDRequest alloc] init];
          request.mode = @"<mode>";
          BCETask *task = [vodClient generateMediaID:request];//此处的vodClient指向之前的步骤中已经鉴权并初始化的VodClient对象
          
          task.then(^(BCEOutput* output) {
              if (output.response) {
                  //任务执行成功,通过返回的response获取mediaId等相关字段
                  VODGenerateMediaIDResponse *res = (VODGenerateMediaIDResponse*)output.response;
                  NSString* mediaID = res.mediaID;
                  NSString* sourceBucket = res.sourceBucket;
                  NSString* sourceKey = res.sourceKey;
          
                  //执行任务成功相关逻辑代码
              }
              
              if (output.error) {
                  //执行任务失败相关逻辑代码
              }
          });
          [task waitUtilFinished];
          }

          上传媒体资源

          基本流程:

          1. 通过完成鉴权初始化的vodClient初始化的vodClient生成用于上传媒体资源的BCETask(注意需要将步骤2获取到的媒资数据传入BOSPutObjectRequest对象);
          2. 执行该Task,在任务回调中执行相应的业务逻辑。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example (void) {
          NSString *uploadFile = [[NSBundle mainBundle] pathForResource:@"<your file path>" ofType:nil];
          
          BOSObjectContent* content = [[BOSObjectContent alloc] init];
          content.objectData.file = uploadFile;
          
          VODGenerateMediaIDResponse *mediaIdResponse = ……;//此处mediaIdResponse指向步骤2中请求到的媒资数据
          
          BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
          request.bucket = mediaIdResponse.sourceBucket;
          request.key = mediaIdResponse.sourceKey;
          request.objectContent = content;
          
          BCETask* task = [bosClient putObject:request];//此处的bosClient指向之前的步骤中已经鉴权并初始化的VodClient对象
          task.then(^(BCEOutput* output) {
          
              if (output.progress) {//上传中
                  //处理相关逻辑
                  //可以通过 output.progress.floatValue 获取当前上传进度
              }
              
              if (output.response) {//上传成功
                  //处理相关逻辑
              }
              
              if (output.error) {//上传错误
                  //处理相关逻辑
              }
          
          });
          [task waitUtilFinished];
          }

          处理媒体资源

          处理媒资接口用于:

          对已完成mediaID申请和视频上传的媒资进行管理。

          基本流程:

          1. 初始化VODProcessMediaRequest,并在对应的字段中指定处理参数;
          2. 通过完成鉴权初始化的vodClient根据构建好的request对象生成BCETask对象,在任务回调中执行相应的业务逻辑。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example (void) {
          VODProcessMediaRequest* request = [VODProcessMediaRequest new];
          VODGenerateMediaIDResponse* mediaIdResponse = <……>;//此处mediaIdResponse指向步骤2中请求到的媒资数据
          request.mediaId = mediaIdResponse.mediaID;
          request.attributes.mediaTitle = @"mediaTitle";
          request.attributes.mediaDescription = @"mv";
          request.sourceExtension = @"mp4";
          request.transcodingPresetGroupName = @"notranscoding";
          
          BCETask *task = [vodClient processMedia:request];
          task.then(^(BCEOutput* output) {
              if (output.response) {//处理媒资请求成功
                  //处理相关业务逻辑
              }
              
              if (output.error) {//处理媒资请求错误
                  //处理相关业务逻辑
              }
          });
          	[task waitUtilFinished];
          }

          查询媒资状态

          基本流程:

          1. 通过完成鉴权初始化的vodClient已经获取到的媒资数据来生成用于上传媒体资源的BCETask;
          2. 执行task,在任务回调中执行相应的业务逻辑。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example(void) {
          
          __block VODGetMediaResponse* response = nil;
          VODGenerateMediaIDResponse* mediaIdResponse = <……>;//此处mediaIdResponse指向步骤2中请求到的媒资数据
          BCETask *task = [vodClient getMedia:mediaIdResponse.mediaID];
          task.then(^(BCEOutput* output) {
              
              if (output.response) {//处理媒资请求成功
                  //通过返回的response获取mediaId等相关字段
                  response = (VODGetMediaResponse*)output.response;
                  
                  //处理相关业务逻辑
              }
              
              if (output.error) {//处理媒资请求错误
                  //处理相关业务逻辑
          
              }
          });
          [task waitUtilFinished];
          }

          分页方式筛选媒资

          对用户的所有媒资进行条件筛选并分页显示。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example(void) {
              VODFilterMediaByPageRequest* request = [VODFilterMediaByPageRequest new];
              request.pageSize = 10;
              
              __block VODFilterMediaByPageResponse* response = nil;
              BCETask* task = [vodClient getFilteredMediaByPage:request];
              task.then(^(BCEOutput* output) {
                  if (output.response) { // 分页方式筛选媒资成功
                      response = (VODFilterMediaByPageResponse*)output.response;
          			// 处理相关业务逻辑
                  }
          
                  if (output.error) { // 分页方式筛选媒资失败
                      //处理相关业务逻辑
                  }
          
              });
          	[task waitUtilFinished];
          }

          Marker方式筛选媒资

          对用户的所有媒资进行条件筛选并用marker机制显示。

          示例代码如下:

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example(void) {
          	VODFilterMediaByMarkerRequest* request = [VODFilterMediaByMarkerRequest new];
          	request.maxSize = 1;
          	
          	__block VODFilterMediaByMarkerResponse* response = nil;
          	BCETask* task = [self.vodClient getFilteredMediaByMarker:request];
          	task.then(^(BCEOutput* output) {
          	    if (output.response) { //Marker方式筛选媒资成功
          	        response = (VODFilterMediaByMarkerResponse *)output.response;
          	        //处理相关业务逻辑
          	    }
          	    
          	    if (output.error) { //Marker方式筛选媒资失败
          	    	//处理相关业务逻辑
          	    }
          	});
          	[task waitUtilFinished];
          }

          完整示例

          #import <BaiduBCEBasic/BaiduBCEBasic.h>
          #import <BaiduBCEBOS/BaiduBCEBOS.h>
          #import <BaiduBCEVOD/BaiduBCEVOD.h>
          
          void example(void) {
              BCETask* task;
              VODGenerateMediaIDResponse* mediaIdResponse;
              NSString* uploadFile = [[NSBundle mainBundle] pathForResource:@"input.mp4" ofType:nil];
          
              
              //1.设置AK、SK,以及BOS、VOD的endpoint
              // STS验证方式,推荐采用
              BCESTSCredentials* credentials = [BCESTSCredentials new];
              credentials.accessKey = @"<your ak>";
              credentials.secretKey = @"<your sk>";
              credentials.sessionToken = @"<your session token>";
              
              // 或者AKSK验证方式
              // BCECredentials* credentials = [BCECredentials new];
              // credentials.accessKey = @"<your ak>";
              // credentials.secretKey = @"<your sk>";
              
              BOSClientConfiguration* bosConfig = [BOSClientConfiguration new];
              bosConfig.credentials = credentials;
              BOSClient* bosClient = [[BOSClient alloc] initWithConfiguration:bosConfig];
              
              VODClientConfiguration* vodConfig = [VODClientConfiguration new];
              vodConfig.credentials = credentials;
              VODClient* vodClient = [[VODClient alloc] initWithConfiguration:vodConfig];
          
          
              //2.生成一个mediaID并将信息存储下来
              
              VODGenerateMediaIDRequest* request = [[VODGenerateMediaIDRequest alloc] init];
              request.mode = @"<mode>";
              __block VODGenerateMediaIDResponse* mediaIdResponse = nil;
          
              task = [vodClient generateMediaID:request];
              task.then(^(BCEOutput* output) {
                  if (output.response) {//上传成功
                      //处理相关业务逻辑
                      mediaIdResponse = (VODGenerateMediaIDResponse*)output.response;
          
                  }
                  
                  if (output.error) {//上传成功
                      //处理相关业务逻辑
          
                  }
              });
              [task waitUtilFinished];
          
              //3.通过BOS上传媒资
              if (!mediaIdResponse) {
                  return;
              }
              
              BOSObjectContent* content = [[BOSObjectContent alloc] init];
              content.objectData.file = uploadFile;
              
              BOSPutObjectRequest* request = [[BOSPutObjectRequest alloc] init];
              request.bucket = mediaIdResponse.sourceBucket;
              request.key = mediaIdResponse.sourceKey;
              request.objectContent = content;
              
              BCETask* task = [bosClient putObject:request];
              task.then(^(BCEOutput* output) {
                  if (output.progress) {//正在上传
                      //处理相关业务逻辑
          
                  }
                  
                  if (output.response) {//上传成功
                      //处理相关业务逻辑
          
                  }
                  
                  if (output.error) {//上传错误
                      //处理相关业务逻辑
          
                  }
              });
              [task waitUtilFinished];
          
          
              //4.处理媒资
              
              VODProcessMediaRequest* request = [VODProcessMediaRequest new];
              request.mediaId = mediaIdResponse.mediaID;
              request.attributes.mediaTitle = @"mediaTitle";
              request.attributes.mediaDescription = @"mv";
              request.sourceExtension = @"mp4";
              request.transcodingPresetGroupName = @"notranscoding";
              
              task = [vodClient processMedia:request];
              task.then(^(BCEOutput* output) {
                  if (output.response) {//处理媒资请求成功
                      //处理相关业务逻辑
          
                  }
                  
                  if (output.error) {//处理媒资请求错误
                      //处理相关业务逻辑
          
                  }
              });
              [task waitUtilFinished];
          
              //5.查询媒资状态
          
              __block VODGetMediaResponse* response = nil;
              task = [vodClient getMedia:mediaIdResponse.mediaID];
              task.then(^(BCEOutput* output) {
                  
                  if (output.response) {//查询媒资请求成功
                      //处理相关业务逻辑
                      response = (VODGetMediaResponse*)output.response;
          
                  }
                  
                  if (output.error) {//查询媒资请求错误
                      //处理相关业务逻辑
          
                  }
              });
              [task waitUtilFinished];
          
          }
          
          	//6.分页方式筛选媒资
          	
              VODFilterMediaByPageRequest* request = [VODFilterMediaByPageRequest new];
              request.pageSize = 10;
              
              __block VODFilterMediaByPageResponse* response = nil;
              task = [vodClient getFilteredMediaByPage:request];
              task.then(^(BCEOutput* output) {
              	if (output.response) { // 分页方式筛选媒资成功
              		response = (VODFilterMediaByPageResponse*)output.response;
          			// 处理相关业务逻辑
                 }
          
                 if (output.error) { // 分页方式筛选媒资失败
                     //处理相关业务逻辑
                 }
             });
             [task waitUtilFinished];
             
             //7.Marker方式筛选媒资
             
             VODFilterMediaByMarkerRequest* request = [VODFilterMediaByMarkerRequest new];
             request.maxSize = 1;
          	
          	__block VODFilterMediaByMarkerResponse* response = nil;
          	task = [self.vodClient getFilteredMediaByMarker:request];
          	task.then(^(BCEOutput* output) {
          	    if (output.response) { //Marker方式筛选媒资成功
          	        response = (VODFilterMediaByMarkerResponse *)output.response;
          	        //处理相关业务逻辑
          	    }
          	    
          	    if (output.error) { //Marker方式筛选媒资失败
          	    	//处理相关业务逻辑
          	    }
          	});
          	[task waitUtilFinished];
          }
          上一篇
          SDK集成
          下一篇
          接口速查