Dave
Dave

Reputation: 567

How to get file (video) duration of google drive file programmatically?

Either using rest API, Google Scripts, Node SDK, whatever works.

I'm seeing this in the docs but that doesn't seem to tell me the duration:

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

I found this link but it doesn't seem to help https://apis-nodejs.firebaseapp.com/drive/classes/Resource$Files.html#watch

Upvotes: 3

Views: 3345

Answers (1)

Tanaike
Tanaike

Reputation: 201368

  • You want to retrieve the duration of the video on your Google Drive.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this sample script? In this modification, I used files.get and files.list methods of Drive API. From your question, I thought that the script that the endpoint is directly requests might be useful for your situation. So I proposed the following script.

1. Using files.get method

In this sample script, the duration is retrieved from a video file.

Sample script:

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. Using files.list method

In this sample script, the durations are retrieved from a folder including the video files.

Sample script:

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.
}

Note:

  • These are simple sample scripts. So please modify them for your situation.
  • I'm not sure about the format of your video files. So if above script cannot be used for your situation, I apologize.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Updated: March 19, 2020

From January, 2020, the access token cannot be used with the query parameter like access_token=###. Ref So please use the access token to the request header instead of the query parameter. It's as follows.

var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});

Upvotes: 4

Related Questions