如何以编程方式获取Google驱动器文件的文件(视频)持续时间? [英] How to get file (video) duration of google drive file programmatically?
问题描述
无论使用哪种技术,都可以使用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.
}
注意:
- 这些是简单的示例脚本.因此,请根据您的情况对其进行修改.
- 我不确定您的视频文件的格式.因此,如果上述脚本无法满足您的情况,我深表歉意.
- 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.
- Files of Drive API
- Class UrlFetchApp
Note:
如果我误解了您的问题,而这不是您想要的结果,我深表歉意.
If I misunderstood your question and this was not the result you want, I apologize.
从2020年1月开始,访问令牌不能与access_token=###
之类的查询参数一起使用.
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()}});
这篇关于如何以编程方式获取Google驱动器文件的文件(视频)持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!