- 您想在 Google 云端硬盘上检索视频的时长。
- 您希望使用 Google Apps 脚本实现此目的。
如果我的理解是正确的,那么这个示例脚本怎么样?在这个修改中,我使用了 Drive API 的 files.get 和 files.list 方法。根据您的问题,我认为端点直接请求的脚本可能对您的情况有用。所以我提出了以下脚本。
1。使用 files.get 方法
在此示例脚本中,时长是从视频文件中检索的。
示例脚本:
function sample1() {
var fileId = "###"; // Please set the file ID of the video file.
var fields = "mimeType,name,videoMediaMetadata"; // duration is included in "videoMediaMetadata"
var url = "https://www.googleapis.com/drive/v3/files/" + fileId + "?fields=" + encodeURIComponent(fields) + "&access_token=" + ScriptApp.getOAuthToken();
var res = UrlFetchApp.fetch(url);
var obj = JSON.parse(res);
Logger.log("filename: %s, duration: %s seconds", obj.name, obj.videoMediaMetadata.durationMillis / 1000);
// DriveApp.getFiles() // This line is put for automatically detecting the scope (https://www.googleapis.com/auth/drive.readonly) for this script.
}
2。使用 files.list 方法
在此示例脚本中,持续时间是从包含视频文件的文件夹中检索的。
示例脚本:
function sample2() {
var folderId = "###"; // Please set the folder ID including the video files.
var q = "'" + folderId + "' in parents and trashed=false";
var fields = "files(mimeType,name,videoMediaMetadata)"; // duration is included in "videoMediaMetadata"
var url = "https://www.googleapis.com/drive/v3/files?q=" + encodeURIComponent(q) + "&fields=" + encodeURIComponent(fields) + "&access_token=" + ScriptApp.getOAuthToken();
var res = UrlFetchApp.fetch(url);
var obj = JSON.parse(res);
for (var i = 0; i < obj.files.length; i++) {
Logger.log("filename: %s, duration: %s seconds", obj.files[i].name, obj.files[i].videoMediaMetadata.durationMillis / 1000);
}
// DriveApp.getFiles() // This line is put for automatically detecting the scope (https://www.googleapis.com/auth/drive.readonly) for this script.
}
注意:
- 这些是简单的示例脚本。所以请根据您的情况修改它们。
- 我不确定您的视频文件的格式。因此,如果上述脚本不能用于您的情况,我深表歉意。
参考资料:
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
更新日期:2020 年 3 月 19 日
自 2020 年 1 月起,访问令牌不能与 access_token=### 等查询参数一起使用。 Ref 所以请使用请求头的访问令牌而不是查询参数。如下。
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});