GAS: Blob upload to YouTube fails for most files

Viewed 408

I'm trying to upload files from Google Drive to YouTube using Google Apps scripts.

I've only had success uploading a video that is ~2MB.

The main reason for doing this is an internet conenction with slow, sometimes unreliable upload, so letting Google Drive handle that is easier, but then getting files > 25MB to YouTube is hard...

The script needs to have "YouTube Data API" enabled from "Resources" > "Advanced Google services..."

Function code:

// Upload Google Drive file to youtube
function uploadFileToYouTube(file) {
  var name = file.getName();
  var size = file.getSize();
  var type = file.getMimeType();
  var blob = file.getBlob();
  var access = file.getSharingAccess();
  var yt_privacy;
  // Attempt to preserve permissions from Drive
  switch(access) {
    case DriveApp.Access.PRIVATE:
      yt_privacy = "private";
      break;
    case DriveApp.Access.ANYONE:
      yt_privacy = "public";
      break;
    case DriveApp.Access.ANYONE_WITH_LINK:
      yt_privacy = "unlisted";
      break;
    default:
      yt_privacy = "private";
  }
  Logger.log("File: "+name+" Size: "+size+" Type: "+type+" Access: "+access+" YT privacy: "+yt_privacy);
  // Actually upload to YouTube....
  var vid_res = {
    snippet: {
      title: name,
      description: "Uploaded from Google Drive\nOriginal size: "+size+"\nOriginal filename: "+name+"\nOriginal mime type: "+type,
    },
    status: {
      privacyStatus: yt_privacy
    }
  }
  try {
    var yt_upload = YouTube.Videos.insert(vid_res,"snippet,status",blob);
    Logger.log(yt_upload);
  } catch (e) {
    Logger.log('Error uploading: '+ name + " Message: " + e.message);
  }
  return yt_upload;
}

// Test function. Needs test.avi on your Google drive
function testUpload() {
  var file = DriveApp.getFilesByName("test.avi").next();
  uploadFileToYouTube(file);
}

Log:

[13-12-30 13:18:34:924 SAST] File: SUNP0017.avi Size: 35258368 Type: video/x-msvideo Access: ANYONE_WITH_LINK YT privacy: unlisted
[13-12-30 13:18:45:924 SAST] Error uploading: SUNP0017.avi Message: We're sorry, a server error occurred. Please wait a bit and try again.

Execution transcript:

[13-12-30 13:18:33:518 SAST] Starting execution
[13-12-30 13:18:33:527 SAST] DriveApp.getFilesByName([SUNP0017.avi]) [0 seconds]
[13-12-30 13:18:34:799 SAST] (class).next() [1.271 seconds]
[13-12-30 13:18:34:799 SAST] File.getName() [0 seconds]
[13-12-30 13:18:34:799 SAST] File.getSize() [0 seconds]
[13-12-30 13:18:34:799 SAST] File.getMimeType() [0 seconds]
[13-12-30 13:18:34:799 SAST] File.getBlob() [0 seconds]
[13-12-30 13:18:34:921 SAST] File.getSharingAccess() [0.121 seconds]
[13-12-30 13:18:34:924 SAST] Logger.log([File: SUNP0017.avi Size: 35258368 Type: video/x-msvideo Access: ANYONE_WITH_LINK YT privacy: unlisted, []]) [0 seconds]
[13-12-30 13:18:40:474 SAST] Blob.getBytes() [5.549 seconds]
[13-12-30 13:18:40:504 SAST] Blob.getContentType() [0 seconds]
[13-12-30 13:18:45:924 SAST] Logger.log([Error uploading: SUNP0017.avi Message: We're sorry, a server error occurred. Please wait a bit and try again., []]) [0 seconds]
[13-12-30 13:18:45:924 SAST] Execution succeeded [12.398 seconds total runtime]

Log for smaller file: (works)

[13-12-30 13:19:59:720 SAST] File: SUNP0024.avi Size: 3964928 Type: video/x-msvideo Access: ANYONE_WITH_LINK YT privacy: unlisted
[13-12-30 13:20:01:579 SAST] {id=<removed>, etag="<removed>", status={publicStatsViewable=true, privacyStatus=unlisted, embeddable=true, uploadStatus=uploaded, license=youtube}, snippet={publishedAt=2013-12-30T11:20:00.000Z, title=SUNP0024.avi, channelId=<removed>, description=Uploaded from Google Drive
Original size: 3964928
Original filename: SUNP0024.avi
Original mime type: video/x-msvideo, categoryId=22, channelTitle=Gert van den Berg, thumbnails={default={url=<removed>}, high={url=<removed>}, medium={url=<removed>}}, liveBroadcastContent=none}, kind=youtube#video}

Transcript for smaller file: (works)

[13-12-30 13:19:58:311 SAST] Starting execution
[13-12-30 13:19:58:320 SAST] DriveApp.getFilesByName([SUNP0024.avi]) [0 seconds]
[13-12-30 13:19:59:549 SAST] (class).next() [1.229 seconds]
[13-12-30 13:19:59:550 SAST] File.getName() [0 seconds]
[13-12-30 13:19:59:554 SAST] File.getSize() [0.002 seconds]
[13-12-30 13:19:59:558 SAST] File.getMimeType() [0.002 seconds]
[13-12-30 13:19:59:559 SAST] File.getBlob() [0 seconds]
[13-12-30 13:19:59:719 SAST] File.getSharingAccess() [0.157 seconds]
[13-12-30 13:19:59:721 SAST] Logger.log([File: SUNP0024.avi Size: 3964928 Type: video/x-msvideo Access: ANYONE_WITH_LINK YT privacy: unlisted, []]) [0 seconds]
[13-12-30 13:20:00:756 SAST] Blob.getBytes() [1.034 seconds]
[13-12-30 13:20:00:759 SAST] Blob.getContentType() [0 seconds]
[13-12-30 13:20:01:579 SAST] Logger.log([{id=<removed>, etag="<removed>", status={publicStatsViewable=true, privacyStatus=unlisted, embeddable=true, uploadStatus=uploaded, license=youtube}, snippet={publishedAt=2013-12-30T11:20:00.000Z, title=SUNP0024.avi, channelId=<removed>, description=Uploaded from Google Drive
Original size: 3964928
Original filename: SUNP0024.avi
Original mime type: video/x-msvideo, categoryId=22, channelTitle=Gert van den Berg, thumbnails={default={url=<removed>}, high={url=<removed>}, medium={url=<removed>}}, liveBroadcastContent=none}, kind=youtube#video}, []]) [0 seconds]
[13-12-30 13:20:01:579 SAST] Execution succeeded [3.26 seconds total runtime]
1 Answers
Related