如何以编程方式获取Google驱动器文件的文件(视频)持续时间? [英] How to get file (video) duration of google drive file programmatically?

查看:38
本文介绍了如何以编程方式获取Google驱动器文件的文件(视频)持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论使用哪种技术,都可以使用rest API,Google脚本,Node SDK.

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

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

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

推荐答案

  • 您要在Google云端硬盘上检索视频的时长.
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么该示例脚本如何?在此修改中,我使用了Drive API的files.get和files.list方法.从您的问题来看,我认为端点直接请求的脚本可能对您的情况很有用.因此,我提出了以下脚本.

    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.

    在此示例脚本中,持续时间是从视频文件中检索的.

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

    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方法

    在此示例脚本中,持续时间是从包含视频文件的文件夹中检索的.

    2. Using files.list method

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

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

    注意:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆