Does anyone have any idea how I'd get an MP4 file from my Google Drive as a blob for the YouTube API in order to upload a video?
The code below uses the Drive API to get a download URL, and the code gets to the point where it will attempt the video upload to YouTube, but the last error I got is that HTML is not a valid content type.
I don't know if the export needs to convert the file to a specific file type, and I have no idea how that might work.
Or maybe there is a better way to get the MP4 as a blob than using the download url.
I tried using the built in DriveApp Service to get the MP4 file, but had no success.
I also thought about maybe needing a Web App that would upload the file from the users hard drive, and somehow convert it to a blob and then send it back to the server.
function uploadToYouTube() {
var blob,downloadUrl,mp4_fileId,part,requestResource,response;
var options = {},snippet = {};
/*
You will need to create a GCP standard project and associate it with this Apps Script project-
In the new code editor click the settings cog wheel and scroll down to:
Google Cloud Platform (GCP) Project -
You may get an error:
In order to change your project, you will need to configure the OAuth consent screen. Configure your OAuth Consent details.
And if you do not have a Google Workspace account then you wont be able to set up the GCP project as "INTERNAL"
You will need to enable the Google Drive API and the YouTube API in the associated GCP project -
*/
/*
This code needs the file ID of the MP4 file in your Google Drive -
To get the file ID of an MP4 video file in Google Drive, right click the MP4 in your Google Drive
and choose, "Get link"
The link will look like this:
In the URL is the file ID
Then you will need to get the webContentLink of the file with the Drive API
The webContentLink is needed to get the MP4 files as a blob
*/
options = {
"method" : "get",
"headers" : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
"muteHttpExceptions":true
}
mp4_fileId = 'PUT THE MP4 FILE ID HERE';
//The download url should look like the following:
//Logger.log('response.getResponseCode(): ' + response.getResponseCode())
if (response.getResponseCode() !== 200) {
return;
}
rspnsContent = JSON.parse(response.getContentText());
downloadUrl = rspnsContent.webContentLink;
Logger.log('downloadUrl: ' + downloadUrl)
options = {
"method" : "get",
"headers" : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
"muteHttpExceptions":true
}
response = UrlFetchApp.fetch(downloadUrl,options);//YouTub accepts: "video/*", "application/octet-stream"
Logger.log('response.getResponseCode(): ' + response.getResponseCode())
if (response.getResponseCode() !== 200) {
return;
}
Logger.log('response.getContentText(): ' + response.getContentText())
blob = response.getBlob();
return;
/*
With a big video the script might time out before the video was uploaded -
*/
/*
{"snippet":{
"playlistId":"YOUR_PLAYLIST_ID",
"position":0,
"resourceId":{
"kind":"youtube#video",
"videoId":"abcdefg"
}
}
}
*/
/*
{
"snippet": {
"title": "Summer vacation in California",
"description": "Had fun surfing in Santa Cruz",
"tags": ["surfing", "Santa Cruz"],
"categoryId": "22"
},
"status": {
"privacyStatus": "private"
}
}
*/
requestResource = {};
snippet.title = "AAA_Put_Title_Here";
snippet.description = "Description of video goes here";
snippet.categoryId = "22";
options.snippet = snippet;
options.status = {
"privacyStatus": "private"
}
part = "snippet,status";//This correlates to the options
//YouTube.Videos.insert(resource: Youtube_v3.Youtube.V3.Schema.Video, part: string[], mediaData: Blob, optionalArgs: Object)
var response = YouTube.Videos.insert(requestResource, part, blob, options);
if (!response || !response.kind) {//There was an error
console.log("Error!")
}
//Logger.log('response: ' + response);
}