I have a google drive folder with images and videos. Using a google script, I would like to generate a google slides presentation from those files.
Using images works well following this tutorial.
Can't figure out how to insert/embed videos. I've tried using the insertVideo method, but that fails since it expects a Video object which doesn't correspond to the drive's File object type.
I am curious if anyone has found a way to implement this. Any help appreciated, thanks!
var deck = SlidesApp.create('My Slides');
function addAsset(assetFile) {
var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);
if (assetFile.getMimeType().startsWith('image')) {
slide.insertImage(assetFile);
} else {
slide.insertVideo(assetFile);
}
}
function main() {
var folder = DriveApp.getFoldersByName('my assets').next();
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
addAsset(file);
}
}
I think that the reason of your issue is that the file cannot be directly used to insertVideo
. The argument of insertVideo is the URL and the video object of Google Slides. I think that the reason of your issue is due to this.
About the URL of argument, in the current stage, it seems that when the method of insertVideo
is used, the video content is required to be the URL of the publicly shared YouTube.
From above situation, as a workaround for achieving your issue, I would like to propose the following 3 patterns.
In this pattern, at first, a new Google Slides is created including the video contents. In this case, it is required to manually put the video to the new Google Slides. And, this Slides is used as the template Slides. When you want to put the video contents to the Google Slides you want to use, the video contents of the template Slides are used using the script. By this, your goal can be achieved using insertVideo
.
In this pattern, at first, all videos you want to use are manually put to YouTube. And when you want to put the video contents to the Google Slides you want to use, the video contents of YouTube are used using the script. By this, your goal can be achieved using insertVideo
.
In this pattern, Slides API is used. When Slides API is used, the files on Google Drive can be directly put to the Google Slides using the file ID. When your script is modified, it becomes as follows. This is used for this workaround. Before you use this script, please enable Slides API at Advanced Google services.
function main() {
var deck = SlidesApp.create('My Slides');
var slide = deck.appendSlide(SlidesApp.PredefinedLayout.BLANK);
deck.saveAndClose();
deck = SlidesApp.openById(deck.getId());
slide = deck.getSlides()[1];
var pageObjectId = slide.getObjectId();
var folder = DriveApp.getFoldersByName('my assets').next();
var files = folder.getFiles();
var videos = [];
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType().startsWith('image')) {
slide.insertImage(file);
} else if (file.getMimeType().startsWith("video")) {
videos.push({createVideo: {source: "DRIVE", id: file.getId(), elementProperties: {pageObjectId: pageObjectId}}});
}
}
if (videos.length > 0) {
Slides.Presentations.batchUpdate({requests: videos}, deck.getId());
}
}