Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created February 3, 2021 12:48
Show Gist options
  • Save mhawksey/e71ea281eecefe72226a99dee3ffd165 to your computer and use it in GitHub Desktop.
Save mhawksey/e71ea281eecefe72226a99dee3ffd165 to your computer and use it in GitHub Desktop.
/**
* @OnlyCurrentDoc
*/
YouTube.setTokenService(function(){ return getYouTubeService().getAccessToken(); });
// Read data from current sheet and create live events returning details back to sheet
function schedule_events(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YT");
new cUseful.Fiddler(sheet)
.mapRows (function (row) {
if (row.start_time && !row.youtube_url && !row.stream_key){ // if not already scheduled
// format datetime to ISO 8601 format as per docs https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#snippet.scheduledStartTime
row.start_time = Utilities.formatDate(row.start_time, Session.getScriptTimeZone(), "YYYY-MM-dd'T'HH:mm:ss")+"+0100";
row.end_time = Utilities.formatDate(row.end_time, Session.getScriptTimeZone(), "YYYY-MM-dd'T'HH:mm:ss")+"+0100";
// step 1 - create a broadcast
var broadcast_resource = insert_broadcast(row);
// step 2 - create a stream
// var stream_resource = insert_stream(row);
// step 3 - bind broadcast to stream
var bind_result = bind_broadcast(broadcast_resource.id, "STREAM_RESOURCE_ID");
// step 4 - prep data to add back to the sheet
row.youtube_url = "https://www.youtube.com/watch?v="+broadcast_resource.id;
row.vid = broadcast_resource.id;
setThumbnail(row.vid);
var ingestionInfo = {"streamName": "STREAM_NAME",
"ingestionAddress": "rtmp://a.rtmp.youtube.com/live2",
"backupIngestionAddress": "rtmp://b.rtmp.youtube.com/live2?backup=1"
};
row.stream_key = ingestionInfo.streamName;
row.ingestion_address = ingestionInfo.ingestionAddress;
row.start_time = new Date(row.start_time);
row.end_time = new Date(row.end_time);
}
return row;
}).dumpValues();
}
// The following is based on
// https://developers.google.com/youtube/v3/live/code_samples/python#create_a_broadcast_and_stream
// LICENCE Copyright Google http://www.apache.org/licenses/LICENSE-2.0
// Create a liveBroadcast resource and set its title, scheduled start time,
// scheduled end time, and privacy status.
function insert_broadcast(options){
var insert_broadcast_response = YouTube.liveBroadcastsInsert("snippet,status",
{"snippet": {
"title": options.title,
"description": options.desc,
"scheduledStartTime": options.start_time,
"scheduledEndTime": options.end_time,
},
"status": {
"privacyStatus": options.privacy_status,
"selfDeclaredMadeForKids": false
}
},{});
Logger.log(insert_broadcast_response);
return insert_broadcast_response;
}
function getLiveReusableStream(){
var stream = YouTube.liveStreamsList("id,snippet,cdn", {mine:true});
Logger.log(stream);
}
function setThumbnail(video_id){
return YouTube.thumbnailsSet(video_id, {payload: DriveApp.getFileById("FILE_ID").getBlob()})
}
// Create a liveStream resource and set its title, format, and ingestion type.
// This resource describes the content that you are transmitting to YouTube.
function insert_stream(options){
var insert_stream_response = YouTube.liveStreamsInsert("snippet,cdn",
{"snippet": {
"title":options.title,
"description": options.description,
},
"cdn": {
"ingestionType":"rtmp"
}
},{});
Logger.log(insert_stream_response);
return insert_stream_response;
}
// Bind the broadcast to the video stream. By doing so, you link the video that
// you will transmit to YouTube to the broadcast that the video is for.
function bind_broadcast(broadcast_id, stream_id){
var bind_broadcast_response = YouTube.liveBroadcastsBind(broadcast_id, "id,contentDetails", {"streamId":stream_id});
Logger.log(bind_broadcast_response);
return bind_broadcast_response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment