如何以编程方式获取Google驱动器文件的文件(视频)持续时间?

时间:2019-05-05 07:24:07

标签: google-apps-script google-drive-api

无论使用哪种REST API,Google脚本,Node SDK,都可以。

我在文档中看到了这一点,但这似乎并没有告诉我持续时间:

function watchFile(fileId, channelId, channelType, channelAddress) {
  var resource = {
    'id': channelId,
    'type': channelType,
    'address': channelAddress
  };
  var request = gapi.client.drive.files.watch({
    'fileId': fileId,
    'resource': resource
  });
  request.execute(function(channel){console.log(channel);});
}

我找到了此链接,但似乎没有帮助https://apis-nodejs.firebaseapp.com/drive/classes/Resource $ Files.html#watch

1 个答案:

答案 0 :(得分:1)

  • 您要在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.
}

注意:

  • 这些是简单的示例脚本。因此,请根据您的情况对其进行修改。
  • 我不确定您的视频文件的格式。因此,如果上述脚本无法满足您的情况,我深表歉意。

参考:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。