以下方法需要開啟相應開關才能使用。包括文檔預覽,音視頻轉碼播放,打包下載。
請求參數中的 options: IPDSRequestConfig 基於 axios 的 request config 封裝,詳情請看:AxiosRequestConfig。
getOfficePreviewUrl 方法
文檔預覽,需要開通對應的服務才可以使用。
const result = await client.getOfficePreviewUrl(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IOfficePreviewReq | 是 | 要預覽的檔案 |
options | IPDSRequestConfig | 其它配置項 |
IOfficePreviewReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 雲端硬碟id。(drive_id和share_id 二選一) | |
share_id | string | 分享id。(drive_id和share_id 二選一) | |
file_id | string | 檔案id | |
allow_copy | boolean | 是 | 是否允許複製 |
referer | string | 地址,location.origin 或者 undefined |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | object | 是 | 返回結果 |
result
欄位 | 類型 | 必選 | 說明 |
access_token | string | 是 | token資訊 |
preview_url | string | 是 | 預覽URL |
附: 文檔預覽方法舉例
PDS文檔的預覽對接的是阿里雲IMM的文檔預覽功能。
下面是具體步驟:
(1) html 修改
在 html 首先需要引入 sdk.js。
<script src="https://g.alicdn.com/IMM/office-js/1.1.15/aliyun-web-office-sdk.min.js"></script>
<!-- iframe mounted 元素 -->
<div id="preview-div"></div>(2) 主要邏輯
調用 getOfficePreviewUrl 介面擷取 access_token 和 preview_url。
//擷取協作地址和AccessToken。
var tokenInfo = await client.getOfficePreviewUrl({
drive_id: '1',
file_id: 'xxxxxx'
})
let instance = aliyun.config({
mount: document.querySelector('#preview-div'), // 預覽的 iframe 會渲染在這個元素裡
url: tokenInfo.preview_url, // 設定文檔協作URL地址。
refreshToken, // 配置重新整理token方法
})
//設定AccessToken。
instance.setToken({
token: tokenInfo.access_token,
timeout: 10*60*1000
})
// 重新整理token 方法
// 重新調用 getOfficePreviewUrl 介面可以再次擷取 access_token 和 preview_url
async function refreshToken(){
var tokenInfo = await client.getOfficePreviewUrl({
drive_id: '1',
file_id: 'xxxxxx'
})
return {
token: tokenInfo.access_token,
timeout: 10*60*1000
}
}(3) iframe 樣式
根據實際情況自行調整。
#preview-div iframe{
width: 100%;
height: 100%;
}getOfficeEditUrl 方法
文檔編輯,需要開通對應的服務才可以使用。
const result = await client.getOfficeEditUrl(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IOfficeEditReq | 是 | 要編輯的檔案等參數 |
options | IPDSRequestConfig | 否 | 其它配置項 |
IOfficeEditReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 雲端硬碟id。(drive_id和share_id 二選一) | |
share_id | string | 分享id。(drive_id和share_id 二選一) | |
file_id | string | 是 | 檔案id |
option | object | 包含如下欄位 { readonly: boolean, copy: boolean } | |
referer | string | 地址,location.origin 或者 undefined |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | IOfficeEditRes | 是 | 返回結果 |
IOfficeEditRes
欄位 | 類型 | 必選 | 說明 |
office_access_token | string | 是 | token |
office_refresh_token | string | 是 | token |
edit_url | string | 是 | URL資訊 |
refreshOfficeEditToken 方法
文檔編輯的重新整理,需要開通對應的服務才可以使用。
const result = await client.refreshOfficeEditToken(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IOfficeRefreshReq | 是 | 要編輯的檔案等參數 |
options | IPDSRequestConfig | 否 | 其它配置項 |
IOfficeRefreshReq
欄位 | 類型 | 必選 | 說明 |
office_access_token | string | 是 | token |
office_refresh_token | string | 是 | token |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | IOfficeRefreshReq | 是 | 返回結果 |
getVideoPreviewPlayMeta 方法
擷取視頻或者音頻轉碼的清晰度列表,需要開通對應的服務才可以使用。
const result = await client.getVideoPreviewPlayMeta(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IGetVideoPreviewPlayMetaReq | 否 | 要播放的視頻資訊,繼承自 IFileKey |
options | IPDSRequestConfig | 否 | 其它配置項 |
IGetVideoPreviewPlayMetaReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 雲端硬碟id。(drive_id和share_id二選一) | |
share_id | string | 分享id。(drive_id和share_id二選一) | |
file_id | string | 是 | 檔案id |
category | string | 否 | 類別。根據開通情況,可選值可能為: live_transcoding | quick_video | offline_audio | offline_video。 |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | IGetVideoPreviewPlayMetaRes | 是 | 返回結果 |
IGetVideoPreviewPlayMetaRes
欄位 | 類型 | 必選 | 說明 |
video_preview_play_meta | IVideoPreviewPlayMeta | 是 | 播放相關的資訊 |
... 其它欄位 |
IVideoPreviewPlayMeta
欄位 | 類型 | 必選 | 說明 |
category | string | 是 | 類別。 |
meta | {duration:number, width:number, height:number} | 是 | 播放相關的資訊。 |
live_transcoding_task_list | ILiveTranscodingTask[] | 轉碼工作清單。 |
ILiveTranscodingTask
欄位 | 類型 | 必選 | 說明 |
template_id | string | 是 | 模板id。 |
template_name | string | 是 | 模板名稱。 |
status | string | 是 | 轉碼任務狀態。取值範圍:'finished' | 'running' | 'failed' |
...其他欄位 |
舉例:
(1) 擷取轉碼後支援的各種清晰度
const previewInfo = await client.getVideoPreviewPlayMeta({
drive_id: "xxx",
file_id: "xxx",
category: "live_transcoding",
});
const definitions = previewInfo?.video_preview_play_meta?.live_transcoding_task_list?.map(n=>{
return {
template_id: n.template_id,
template_name: n.template_name
}
})definitions 格式如:
[
{
template_id: "264_480p",
template_name: "pdsSD"
},
{
template_id: "264_720p",
template_name: "pdsHD"
}
]附錄:轉碼後的視頻各參數均不會超過轉碼模板的對應值
轉碼模板 | 264_480p | 264_720p | 264_1080p | 265_4K |
視頻編碼器 | h264 | h264 | h264 | h265 |
視頻解析度 | 720x480 | 1280x720 | 1920x1080 | 3840x2160 |
視頻碼率(kbps) | 600 | 1500 | 3000 | 6000 |
視訊框架率(fps) | 25 | 25 | 25 | 25 |
音頻編碼器 | aac | aac | aac | aac |
音頻採樣率(Hz) | 44100 | 44100 | 44100 | 44100 |
音頻聲道數 | 2 | 2 | 2 | 2 |
音頻碼率(kbps) | 72 | 128 | 160 | 224 |
(2) 異常情況
如果沒有轉碼完成(正在轉碼),返回 status code 為 202,會返回以下格式json:(這個時候需要輪詢 getVideoPreviewPlayMeta 方法,直到 live_transcoding_task_list 中的狀態為finished)
{
code: "VideoPreviewWaitAndRetry",
message: "video is transcoding, please wait and retry. offline transcode when play, wait transcoding",
};轉碼增值服務開關沒有開啟, 返回 status code: 400:
{
code: "VideoPreviewDisabled.OnlineVideoTranscode",
message: "video preview online_video_transcode is disabled",
};getVideoPreviewPlayInfo 方法
根據清晰度擷取音視頻轉碼後的播放URL,需要開通對應的服務才可以使用。
const result = await client.getVideoPreviewPlayInfo(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IVideoPreviewPlayInfoReq | 否 | 要播放的視頻資訊,繼承自 IFileKey |
options | IPDSRequestConfig | 否 | 其它配置項 |
IVideoPreviewPlayInfoReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 是 | 雲端硬碟id |
file_id | string | 是 | 檔案id |
category | string | 否 | 類別,根據開通情況,可選值可能為: live_transcoding | quick_video | offline_audio | offline_video。 |
template_id | string | 否 | 模板id。如果不傳則拉取所有template對應的轉碼資訊 |
get_without_url | boolean | 否 | 是否拉取視頻流。預設 false。設定此參數true後, 不會擷取任何URL,以減少不必要的轉碼成本 |
url_expire_sec | number | 否 | URL 逾時時間。單位秒(s),最大4小時 |
referer | string | 否 | 地址,location.origin 或者 undefined |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | IVideoPreviewPlayInfoRes | 是 | 返回結果 |
IVideoPreviewUrlRes
欄位 | 類型 | 必選 | 說明 |
video_preview_play_info | IVideoPreviewPlayMeta | 是 | 播放相關的資訊 |
... 其它欄位 |
舉例:
(1) 擷取轉碼後某個清晰度的URL
const previewInfo = await client.getVideoPreviewPlayInfo({
drive_id: "xxx",
file_id: "xxx",
category: "live_transcoding",
template_id: "264_480p",
});正常返回:
{
"domain_id": "xxx",
"drive_id": "xxxx",
"file_id": "xxxx",
"video_preview_play_info": {
"category": "live_transcoding",
"meta": {
"duration": 2.484688,
"width": 1280,
"height": 720,
"live_transcoding_meta": {
"ts_segment": 10,
"ts_total_count": 1
}
},
"live_transcoding_task_list": [
{
"template_id": "264_480p",
"template_name": "pdsSD",
"status": "finished",
"stage": "stage_all",
"url": "https://xxxxxf1ec8c36" // 播放url
}
]
}
}(2) 異常情況
如果沒有轉碼完成(正在轉碼),返回 status code 為 202,會返回以下格式json:(這個時候需要輪詢 getVideoUrlFromDefinition 方法,直到 live_transcoding_task_list 中的狀態為finished)
{
code: "VideoPreviewWaitAndRetry",
message: "video is transcoding, please wait and retry. offline transcode when play, wait transcoding",
};轉碼增值服務開關沒有開啟, 返回 status code: 400:
{
code: "VideoPreviewDisabled.OnlineVideoTranscode",
message: "video preview online_video_transcode is disabled",
};沒有對應的轉碼資訊(一般是template_id傳錯了)
{
"domain_id": "x x x",
"drive_id": "xxxx",
"file_id": "61c5729c722d8f86d9c848508b8c5f6dc6604248",
"video_preview_play_info": {
"category": "live_transcoding",
"meta": {
"duration": 14.433333,
"width": 960,
"height": 544,
"live_transcoding_meta": {
"ts_segment": 10,
"ts_total_count": 2
}
}
}
}(3) 轉碼後的視頻如何播放
轉碼後的視頻一般為.m3u8格式,可以用一些開源的視頻播放器播放,推薦使用videojs。
下面是個基於Vue封裝的例子:
<template>
<video class="video-js" ref="videoPlayer"></video>
</template>
<script>
import videojs from 'video.js';
import { getToken } from 'token.js';
import client from 'client.js'
export default {
data() {
return {
drive_id: '1',
file_id: '612cxxxxxxxxxxxxxxxxxxxxxxxxxxxec7d',
url: '',
status: 'loading', // 視頻狀態,可根據狀態進行其他動作
// video.js配置項
playerOptions: {
controls: false, // 是否擁有控制條
autoplay: true, // 是否自動播放
muted: false, // 是否靜音
html5: {
vhs: {
// 支援m3u8視頻播放
withCredentials: false,
},
},
},
};
},
mounted() {
this.init();
this.getVideoUrlFromDefinition();
},
method: {
init() {
//headers中添加Authorization,處理401報錯
videojs.Vhs.xhr.beforeRequest = function (options) {
const { access_token } = getToken()
options.headers = { Authorization: `Bearer ${access_token}` };
return options;
};
this.player = videojs(this.$refs.videoPlayer, this.playerOptions, function () {
this.on('seeking', () => {
// console.log('seeking---視頻跳轉中---')
this.status = 'loading';
// 跳轉載入逾時15s,進行處理
if (!this.waitingTimer) {
let second = 0;
this.waitingTimer = setInterval(() => {
second++;
if (second > 15) {
this.status = 'transcoding';
}
}, 1000);
}
});
this.on('seeked', () => {
// console.log('seeked---視頻跳轉結束---')
this.status = 'normal';
this.waitingTimer = null;
clearInterval(this.waitingTimer);
});
});
},
async getVideoUrlFromDefinition() {
try {
// 第一步拉模數板
const { video_preview_play_info } = await client.getVideoUrlFromDefinition({
drive_id: this.drive_id,
file_id: this.file_id,
template_id: '',
get_without_url: true,
});
// 取轉碼成功的模板
const live_transcoding_task_list =
video_preview_play_info.live_transcoding_task_list.filter((item) => {
return item.status === 'finished';
});
if (live_transcoding_task_list.length) {
// 第二步擷取url
const videoInfo = await client.getVideoUrlFromDefinition({
drive_id: this.drive_id,
file_id: this.file_id,
template_id: live_transcoding_task_list[0].template_id,
url_expire_sec: 3600,
});
if (
videoInfo.video_preview_play_info.live_transcoding_task_list.length &&
videoInfo.video_preview_play_info.live_transcoding_task_list[0].url
) {
// 第三步擷取url
this.url = videoInfo.video_preview_play_info.live_transcoding_task_list[0].url;
}
}
} catch (error) {
console.log(error);
}
},
},
watch: {
url(value) {
if (value) {
this.player.pause();
// 修改url
this.player.src({
src: value,
type: 'application/x-mpegURL',
});
this.player.load();
this.player.play();
}
},
},
beforeDestroy() {
this.waitingTimer && clearInterval(this.waitingTimer);
},
};
</script>archiveFiles 方法
服務端打包檔案,擷取async_task_id。
const result = await client.archiveFiles(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IArchiveFileReq | 是 | 打包檔案資訊 |
options | IPDSRequestConfig | 否 | 其它配置項 |
IArchiveFileReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 是 | 雲端硬碟id |
name | string | 是 | 要打包的任務名稱 |
files | array | 是 | 要打包的檔案清單,每一項格式 { file_id: string } |
傳回值
欄位 | 類型 | 必選 | 說明 |
result | object | 是 | 返回 async_task_id, { async_task_id: string } |
pollingArchiveFiles 方法
服務端打包檔案,直接返回結果,此方法是在 archiveFiles 方法上的進一步封裝處理,可根據需要選擇調用。
const result = await client.pollingArchiveFiles(params, options)參數說明
欄位 | 類型 | 必選 | 說明 |
params | IArchiveFileReq | 是 | 打包檔案資訊 |
options | IPDSRequestConfig | 否 | 其它配置項 |
IArchiveFileReq
欄位 | 類型 | 必選 | 說明 |
drive_id | string | 是 | 雲端硬碟id |
name | string | 是 | 要打包的任務名稱 |
files | array | 是 | 要打包的檔案清單,每一項格式 { file_id: string } |
返回
欄位 | 類型 | 必選 | 說明 |
result | IAsyncTaskRes | 是 | 打包進度結果 |
const downloadArchiveFiles = async () => {
const archiveName = "download.zip";
try {
const params = {
drive_id: "xxxxx",
files: ["xxxxx", "xxxx"],
name: archiveName,
};
// pollingArchiveFiles 只會返回 Failed 或者 Succeed,對於Running狀態會自動輪詢
const { state, url, message } = await client.pollingArchiveFiles(params);
if (state === "Failed") {
throw Error("Failed to archive files, cause: ", message);
} else {
// 下載壓縮檔
const link = document.createElement("a");
link.href = url;
link.download = archiveName;
link.click();
}
} catch (err) {
console.log(err);
}
};