七牛雲上傳音視頻時進行切片處理

    現在視頻播放太慢,上傳的視頻進行切片處理,修改常用的七牛雲上傳方法,增加切片處理方法,記錄一下

public class QiniuCloudService{
 private UploadManager uploadManager;
 private String token;
 private Auth auth;
 private OperationManager operater;
 public QiniuCloudService(CloudConfig config){
 this.config = config;
 //初始化
 init();
 }
 private void init(){
 uploadManager = new UploadManager(new Configuration(Zone.autoZone()));
 auth = Auth.create(config.getQiniuAccessKey(), config.getQiniuSecretKey());
 token = auth.uploadToken(config.getQiniuBucketName());
 }
 /**
 * 上傳
 * @param data
 * @param path
 * @return
 * @throws QiniuException
 */
 public String upload(byte[] data, String path) {
 try {
 Response res = uploadManager.put(data, path, token);
 if (!res.isOK()) {
 throw new RuntimeException("上傳七牛雲出錯:" + res.toString());
 }
 } catch (Exception e) {
 throw new RRException("上傳文件失敗", e);
 }
 return config.getQiniuDomain() + "/" + path;
 }
 /**
 * 切片處理
 * @param data
 * @param path
 * @return
 * @throws QiniuException
 */
 public String uploadFop(byte[] data, String path) throws QiniuException {
 String resultUrl = this.upload(data, path);
 operater = new OperationManager(auth, new Configuration(Zone.autoZone()));
 //設置當前要轉碼的視頻的所在空間,即path所在空間
 String bucket = "sp-tv";
 //設置轉碼操作參數
 String fops = "選擇合適的轉碼參數";
 //設置轉碼的隊列
 String pipeline = "sp";
 //可以對轉碼後的文件進行使用saveas參數自定義命名,當然也可以不指定文件會默認命名並保存在當前空間。
//目標空間:文件路徑
 String urlbase64 = UrlSafeBase64.encodeToString("sp-hls-tv:" + path + ".m3u8");
 String pfops = fops + "|saveas/" + urlbase64;
 //設置pipeline參數
 StringMap params = new StringMap().putWhen("force", 1, true).putNotEmpty("pipeline", pipeline);
 try {
 String persistid = operater.pfop(bucket, path, pfops, params);
 } catch (QiniuException e) {
 //捕獲異常信息
 Response r = e.response;
 // 請求失敗時簡單狀態信息
 System.out.println(r.toString());
 try {
 // 響應的文本信息
 System.out.println(r.bodyString());
 } catch (QiniuException e1) {
 //ignore
 }
 }
 return resultUrl;
 }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章