【问题标题】:How to get video duration of google drive file via API?如何通过 API 获取谷歌驱动文件的视频时长?
【发布时间】:2021-05-19 12:01:30
【问题描述】:

在我的网站上,我通过 Google Drive 托管了一些视频。在我的侧边栏上,有一个视频的缩略图,我想在角落里显示视频的持续时间。我查看了两个类似的问题(herehere)来解决这个问题。这是我通过查看这两个问题得出的结论:

function sample1() {
console.log("running script");
var fileId = "theFileID"; // 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, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
var obj = JSON.parse(res);
console.log("filename: %s, duration: %s seconds", obj.name, obj.videoMediaMetadata.durationMillis / 1000);

}

sample1();

但是,当我在运行此脚本后检查控制台时,“运行脚本”后没有打印任何内容。尝试通过 Google API 解决此问题时,我应该在程序中采用其他方法吗?

【问题讨论】:

  • 如果你想将视频信息上传到youtube,你不能google drive api是一个文件存储api。
  • 你有什么错误吗?
  • @Kessy 我没有收到任何错误,脚本似乎已经运行,因为它实际上打印出了函数的第一行。

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


【解决方案1】:

将 Drive API 方法 Files: get 与 Apps 脚本结合使用,我已经能够从视频中获取 durationMillis

您必须在 Apps 脚本项目中添加 Drive API 高级服务:

function getVideoLength() {
  var fileId = "FILE ID";
  var returnedFile = Drive.Files.get(fileId);
  Logger.log(returnedFile.videoMediaMetadata.durationMillis)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2015-11-30
    • 2011-04-25
    相关资源
    最近更新 更多