Skip to content

Instantly share code, notes, and snippets.

@nag-cheedella
Created October 4, 2017 19:32
Show Gist options
  • Save nag-cheedella/3c7254979c3319c9e9ed6e3e9180e2b2 to your computer and use it in GitHub Desktop.
Save nag-cheedella/3c7254979c3319c9e9ed6e3e9180e2b2 to your computer and use it in GitHub Desktop.
Ooyala Backlot API based Video Asset Content Replacement
<?php
#This is a standalone script for doing Content Replacement in Ooyala Backlot
#This script requires Ooyala API SDK available at https://github.com/ooyala/php-v2-sdk
#1. Clone the SDK Repo from https://github.com/ooyala/php-v2-sdk
#2. Keep the file in the repo
#3. Update API Key, Secret, Embed Code, FileName
#4. Run "php content_replacement.php"
set_time_limit(36000);
ini_set('memory_limit', '50G');
ini_set('display_errors', 'On');
date_default_timezone_set('America/Los_Angeles');
//From https://github.com/ooyala/php-v2-sdk
require_once("OoyalaApi.php");
$api_key = "xxxx12345"; // Put Ooyala API Key
$api_secret = "yyyy23456"; // Put Ooyala API Secret
$embedCode = "abc1234"; // Put Embedcode to be replaced
$fileName = "Sample.mp4"; // File location
//Create API instance.
$OoApi = new OoyalaApi($api_key, $api_secret);
$file = "./$fileName";
$handle = fopen($file, "r");
$content = fread($handle, filesize($file));
$fileSize=filesize($file);
echo "Staring Content Repalcement for embed_code:".$embedCode;
echo "\nReplacement fileSize:".$fileSize;
$chunkSize=10*1024*1024; //Max recommended chunk size is 100 MB for large files
echo "\nChunk Size:".$chunkSize;
$count=0;
$params = array(
"user_permission" => "admin",
"limit" => "500",
"file_size" => $fileSize,
"chunk_size" => (string)$chunkSize
);
//Set the asset on "replacement" state. (Required prior to get the upload URLs)
echo "\nCalling POST /assets/:embed_code/repalcement";
$replacement_response=$OoApi->post("assets/".$embedCode."/"."replacement",$params); //Execute the change
//Get uploading URLs from Ooyala API.
echo "\nGenerate Uploading URLs. Calling GET /assets/:embed_code/repalcement/uploading_urls";
$urls=$OoApi->get("assets/".$embedCode."/"."replacement/uploading_urls");
$i =0;
echo "\nUploading Chunks";
foreach ($urls as $url) {
#echo "\n"."url/chunk number: ".$i." ".date("h:i:sa");;
echo "\n"."Uploading url/chunk#: ".$i." of ".count($urls)." Timestamp: ".date("h:i:sa");;
$i=$i+1;
$first_attempt = true;
while($need_retry || $first_attempt) {
$need_retry = false;
$chunk = file_get_contents($file, NULL, NULL, $count, (string)$chunkSize);
//--------------------------------------------------------upload file
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: multipart/mixed"));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $chunk);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$postResult = curl_exec($ch);
echo $postResult;
//--------------------------------------------------------
$first_attempt = false;
// Check if any error occured
if(!curl_errno($ch))
{
$count = $count+$chunkSize;
echo "\n"."Total file Uploaded so far is: ".$count;
$info = curl_getinfo($ch);
echo "\n"."Chunk Upload HTTP response: ".$info['http_code']."\n" ;
}
else
{
$need_retry = true;
echo 'Curl error: ' . curl_error($ch). $info['http_code'] ;
}
}
} //foreach ends.
$empty_queryParams = array();
echo "\nAll chunks uploaded successfully";
echo "\nSetting Asset Status to uploaded. Calling PUT /assets/:embed_code/upload_status";
//Change status on API to uploaded.
$requestBody = array(
"status" => "uploaded"
);
$OoApi->put("assets/".$embedCode."/"."replacement/upload_status",$requestBody,$empty_queryParams);
echo "\nContent to be replaced is uploaded. Transcoding will be triggered as per the standard process. Check Backlot UI for progress \n"
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment