Source of file AwsVideoFile.php
Size: 12,638 Bytes - Last Modified: 2022-02-21T10:01:10+00:00
/var/www/docs.ssmods.com/process/src/code/models/AwsVideoFile.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 | <?php use Aws\Common\Credentials\Credentials; use Aws\S3\S3Client; use Aws\Common\Exception\MultipartUploadException; use Aws\S3\Model\MultipartUpload\UploadBuilder; use Aws\ElasticTranscoder\ElasticTranscoderClient; class AwsVideoFile extends VideoFile { private static $aws_credentials = null; private static $aws_s3client = null; private static $aws_etclient = null; private static $key = null; private static $secret = null; private static $region = null; private static $input_bucket = null; private static $output_bucket = null; private static $cloudfront_domain = null; private static $pipeline_id = null; private static $preset_1080p = "1351620000001-000001"; // generic 1080p mp4 preset private static $preset_720p = "1351620000001-000010"; // generic 720p mp4 preset private static $preset_480p = "1351620000001-000020"; // generic 480p mp4 preset private static $api_request_time = 900; private static $db = [ 'AwsProcessingStatus' => "Enum(array('unprocessed','uploading','updating','processing','processingerror','error','finished'))", // uploading, processing, finished 'AwsInputURI' => 'Varchar(255)', 'AwsJobId' => 'Varchar(255)', 'AwsOutputURI1080p' => 'Varchar(255)', 'AwsOutputURI720p' => 'Varchar(255)', 'AwsOutputURI480p' => 'Varchar(255)' ]; private static $defaults = [ 'AwsProcessingStatus' => 'unprocessed' ]; protected function creds(){ if(!self::$aws_credentials) self::$aws_credentials = new Credentials(Config::inst()->get('AwsVideoFile', 'key'), Config::inst()->get('AwsVideoFile', 'secret')); return self::$aws_credentials; } protected function s3client(){ // Instantiate the S3 client with your AWS credentials if(!self::$aws_s3client) self::$aws_s3client = S3Client::factory([ 'credentials' => self::creds(), 'region' => Config::inst()->get('AwsVideoFile', 'region') ]); return self::$aws_s3client; } protected function etclient(){ // Instantiate the S3 client with your AWS credentials if(!self::$aws_etclient) self::$aws_etclient = ElasticTranscoderClient::factory([ 'credentials' => self::creds(), 'region' => Config::inst()->get('AwsVideoFile', 'region') ]); return self::$aws_etclient; } protected static function videoDomain(){ return Config::inst()->get('AwsVideoFile', 'cloudfront_domain'); } protected function jobStatus(){ $etClient = self::etclient(); $job = $etClient->readJob(array('Id' => $this->AwsJobId)); $jobData = $job->get('Job'); return $jobData['Status']; } protected function getLogFile(){ if(!$this->log_file){ $this->log_file = TEMP_FOLDER.'/AwsVideoFileProcessing-ID-'.$this->ID.'.log'; } return $this->log_file; } public function process($LogFile = false, $runAfterProcess = true){ if(!$LogFile) $LogFile = $this->getLogFile(); switch($this->ProcessingStatus){ case 'new': if(parent::process($LogFile, $runAfterProcess)){ $this->awsProcess($LogFile, $runAfterProcess); }else{ // Something went wrong } break; case 'finished': $this->awsProcess($LogFile, $runAfterProcess); break; case 'processing': // just do nothing break; case 'error': // just do nothing break; } } protected function awsProcess($LogFile, $runAfterProcess = true){ $this->appendLog($LogFile, "awsProcess() started"); switch($this->AwsProcessingStatus){ case 'processingerror': case 'unprocessed': // upload the Video $this->awsUpload($LogFile); if($this->AwsProcessingStatus == 'finished' && $runAfterProcess) $this->onAfterProcess(); break; case 'uploading': // just do nothing break; case 'processing': // just do nothing break; case 'updating': // just do nothing break; case 'error': // just do nothing break; case 'finished': // just do nothing break; } } protected function awsUpload($LogFile){ $this->AwsProcessingStatus = 'uploading'; $this->write(); try { $this->appendLog($LogFile, "awsUpload() started"); // Instantiate the S3 client with your AWS credentials $s3Client = self::s3client(); // upload the file to the input bucket $upload = $s3Client->upload(Config::inst()->get('AwsVideoFile', 'input_bucket'), $this->ID.'/'.basename($this->getFullPath()), file_get_contents($this->getFullPath())); $this->appendLog($LogFile, "Aws Video Upload Data returned", print_r($upload, true)); if(isset($upload['Key'])){ // Upload was successfull $this->appendLog($LogFile, "File uploaded to input bucket"); $this->AwsInputURI = $upload['Key']; $this->write(); // initiate transcoding jobs $etClient = self::etclient(); // prevent Job errors by deleting existing files previously $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->ID.'/'.'1080p/'.basename($this->getFullPath())]); $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->ID.'/'.'720p/'.basename($this->getFullPath())]); $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->ID.'/'.'480p/'.basename($this->getFullPath())]); // start processing $job = $etClient->createJob([ 'PipelineId' => Config::inst()->get('AwsVideoFile', 'pipeline_id'), 'OutputKeyPrefix' => $this->ID.'/', 'Input' => [ 'Key' => $this->AwsInputURI, 'FrameRate' => 'auto', 'Resolution' => 'auto', 'AspectRatio' => 'auto', 'Interlaced' => 'auto', 'Container' => 'auto', ], 'Outputs' => [ [ 'Key' => '1080p/'.basename($this->getFullPath()), 'Rotate' => 'auto', 'PresetId' => Config::inst()->get('AwsVideoFile', 'preset_1080p') ], [ 'Key' => '720p/'.basename($this->getFullPath()), 'Rotate' => 'auto', 'PresetId' => Config::inst()->get('AwsVideoFile', 'preset_720p') ], [ 'Key' => '480p/'.basename($this->getFullPath()), 'Rotate' => 'auto', 'PresetId' => Config::inst()->get('AwsVideoFile', 'preset_480p') ] ] ]); // get the job data as array $jobData = $job->get('Job'); $this->AwsJobId = $jobData['Id']; $this->write(); return $this->finish(); }else{ $this->appendLog($LogFile, "Error on Upload", print_r($upload, true)); $this->AwsProcessingStatus = 'unprocessed'; $this->write(); return false; } } catch(Exception $e) { $this->AwsProcessingStatus = 'error'; $this->write(); $this->appendLog($LogFile, "AwsUploadException:\n".$e->getMessage()); return false; } } protected function finish(){ $s3Client = self::s3client(); switch($this->jobStatus()){ case 'Submitted': case 'Progressing': $this->AwsProcessingStatus = 'processing'; $this->write(); return false; break; case 'Complete': // Delete origin from input bucket $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'input_bucket'), 'Key' => $this->AwsInputURI]); $this->AwsOutputURI1080p = $this->ID.'/'.'1080p/'.basename($this->getFullPath()); $this->AwsOutputURI720p = $this->ID.'/'.'720p/'.basename($this->getFullPath()); $this->AwsOutputURI480p = $this->ID.'/'.'480p/'.basename($this->getFullPath()); $this->AwsProcessingStatus = 'finished'; $this->write(); return true; break; default: $this->AwsProcessingStatus = 'processingerror'; $this->write(); return false; break; } } public function IsProcessed(){ if($this->AwsProcessingStatus == 'finished'){ return true; }else{ $cache = SS_Cache::factory('AwsVideoFile_ApiRequest'); SS_Cache::set_cache_lifetime('AwsVideoFile_ApiRequest', Config::inst()->get('AwsVideoFile', 'api_request_time')); // set the waiting time if(!($result = $cache->load($this->ID))){ switch($this->AwsProcessingStatus){ case 'unprocessed': $this->process(); break; case 'updating': case 'processing': $this->appendLog($this->getLogFile(), 'IsProcessed - processing'); // update transcoding job info $this->finish(); break; } $result = $this->AwsProcessingStatus; $cache->save($result, $this->ID); } return ($result == 'finished'); } } public function getFullHDUrl() { if($this->AwsProcessingStatus == 'finished') return 'http://'.self::videoDomain().'/'.$this->AwsOutputURI1080p; else return false; } public function getHDUrl() { if($this->AwsProcessingStatus == 'finished') return 'http://'.self::videoDomain().'/'.$this->AwsOutputURI720p; else return false; } public function getSDUrl() { if($this->AwsProcessingStatus == 'finished') return 'http://'.self::videoDomain().'/'.$this->AwsOutputURI480p; else return false; } public function getFullHDUrlSecure() { if($this->AwsProcessingStatus == 'finished') return 'https://'.self::videoDomain().'/'.$this->AwsOutputURI1080p; else return false; } public function getHDUrlSecure() { if($this->AwsProcessingStatus == 'finished') return 'https://'.self::videoDomain().'/'.$this->AwsOutputURI720p; else return false; } public function getSDUrlSecure() { if($this->AwsProcessingStatus == 'finished') return 'https://'.self::videoDomain().'/'.$this->AwsOutputURI480p; else return false; } protected function onBeforeDelete() { parent::onBeforeDelete(); $s3Client = self::s3client(); // Delete origin from input bucket $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'input_bucket'), 'Key' => $this->AwsInputURI]); // delete encoded files from output bucket $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->AwsOutputURI1080p]); $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->AwsOutputURI720p]); $s3Client->deleteObject(['Bucket' => Config::inst()->get('AwsVideoFile', 'output_bucket'), 'Key' => $this->AwsOutputURI480p]); } protected function onAfterProcess() { parent::onAfterProcess(); } } |