0

On my website, I am hosting a few videos via Google Drive. On my sidebar, there is a thumbnail of the videos and I'd like to show the duration of the video in the corner. I have looked at two similar questions (here and here) to solve this problem. This is what I produced from looking at these two problems:

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();

However, when I check the console after running this script, nothing is printed after "running script". Is there a different approach I should be taking in my program when attempting to solve this problem via Google APIs?

bimmui
  • 131
  • 1
  • 11

1 Answers1

0

Using the Drive API method Files: get with Apps Script I have been able to get the durationMillis from the video.

You have to add the Drive API Advanced Services on the Apps Script project:

function getVideoLength() {
  var fileId = "FILE ID";
  var returnedFile = Drive.Files.get(fileId);
  Logger.log(returnedFile.videoMediaMetadata.durationMillis)
}
Kessy
  • 1,894
  • 1
  • 8
  • 15