Skip to content

Commit 4a3523b

Browse files
Updated code sample for start_encode2
1) Changed status getting logic (replacing status_url with value from previous call) 2) Added QencodeClientException 3) Added support for Destination permissions and storage_class fields
1 parent 09b228b commit 4a3523b

File tree

7 files changed

+69
-29
lines changed

7 files changed

+69
-29
lines changed

autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
$files = array(
33
"Exceptions/QencodeException.php",
44
"Exceptions/QencodeApiException.php",
5+
"Exceptions/QencodeClientException.php",
56
"Classes/TranscodingTask.php",
67
"Classes/TranscodingTaskCollection.php",
78
"Classes/CustomTranscodingParams.php",

examples/start_encode.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
try {
2020

21-
$task = $q->createTask();
21+
$task = $q->createTask(); // /v1/create_task
2222
log_message("Created task: ".$task->getTaskToken());
2323

2424
//set start time (in seconds) in input video to begin transcoding from
@@ -47,13 +47,16 @@
4747
echo "DONE!";
4848

4949

50+
} catch (QencodeClientException $e) {
51+
// We got some inconsistent state in client application (e.g. task_token not found when requesting status)
52+
log_message('Qencode Client Exception: ' . $e->getCode() . ' ' . $e->getMessage());
5053
} catch (QencodeApiException $e) {
5154
// API response status code was not successful
5255
log_message('Qencode API Exception: ' . $e->getCode() . ' ' . $e->getMessage());
5356
} catch (QencodeException $e) {
5457
// API call failed
5558
log_message('Qencode Exception: ' . $e->getMessage());
56-
var_export($pf->getLastResponseRaw());
59+
var_export($q->getLastResponseRaw());
5760
}
5861

5962
function log_message($msg) {

examples/start_encode2.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once __DIR__ . '/../autoload.php';
33

44
use Qencode\Exceptions\QencodeApiException;
5+
use Qencode\Exceptions\QencodeClientException;
56
use Qencode\Exceptions\QencodeException;
67
use Qencode\Classes\CustomTranscodingParams;
78
use Qencode\Classes\Format;
@@ -11,25 +12,27 @@
1112
use Qencode\QencodeApiClient;
1213

1314
// Replace this with your API key
14-
$apiKey = '5a5db6fa5b4c5';
15+
$apiKey = 'abcdefgh';
16+
$video_url = 'https://qa.qencode.com/static/1.mp4';
1517

16-
$transcodingProfileId = '5a5db6fa5b8ac';
17-
18-
$video_url = 'https://qa.stagevids.com/static/1.mp4';
19-
20-
$q = new QencodeApiClient($apiKey, 'https://api-qa.qencode.com');
18+
$q = new QencodeApiClient($apiKey);
2119

2220
try {
2321

2422
$task = $q->createTask();
23+
log_message("Created task: ".$task->getTaskToken());
24+
2525
$params = new CustomTranscodingParams();
2626
$params->source = $video_url;
2727

2828
$format = new Format();
2929
$format->destination = new Destination();
30-
$format->destination->url = "s3://s3-eu-west-2.amazonaws.com/qencode-test";
31-
$format->destination->key = "AKIAIKZIPSJ7SDAIWK4A";
32-
$format->destination->secret = "h2TGNXeT49OT+DtZ3RGr+94HEhptS6oYsmXCwWuL";
30+
$format->destination->url = "s3://s3-your-region.amazonaws.com/your-bucket/folder";
31+
$format->destination->key = "your-access-key";
32+
$format->destination->secret = "your-secret-key";
33+
$format->destination->permissions = "public-read";
34+
$format->destination->storage_class = "REDUCED_REDUNDANCY";
35+
$format->segment_duration = 4;
3336
$format->output = "advanced_hls";
3437

3538
$stream = new Stream();
@@ -53,21 +56,28 @@
5356
do {
5457
sleep(5);
5558
$response = $task->getStatus();
56-
print_r($response);
57-
echo "<BR><BR>";
59+
if (is_array($response) and array_key_exists('percent', $response)) {
60+
log_message("Completed: {$response['percent']}%");
61+
}
5862
} while ($response['status'] != 'completed');
5963

6064
foreach ($response['videos'] as $video) {
61-
echo $video['user_tag'] . ': ' . $video['url'].'<BR>';
65+
log_message($video['user_tag'] . ': ' . $video['url']);
6266
}
6367
echo "DONE!";
6468

65-
69+
} catch (QencodeClientException $e) {
70+
// We got some inconsistent state in client application (e.g. task_token not found when requesting status)
71+
log_message('Qencode Client Exception: ' . $e->getCode() . ' ' . $e->getMessage());
6672
} catch (QencodeApiException $e) {
6773
// API response status code was not successful
68-
echo 'Qencode API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
74+
log_message('Qencode API Exception: ' . $e->getCode() . ' ' . $e->getMessage());
6975
} catch (QencodeException $e) {
7076
// API call failed
71-
echo 'Qencode Exception: ' . $e->getMessage();
77+
log_message('Qencode Exception: ' . $e->getMessage());
7278
var_export($q->getLastResponseRaw());
7379
}
80+
81+
function log_message($msg) {
82+
echo $msg."\n";
83+
}

src/Classes/Destination.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,18 @@ class Destination {
2020
* @var string
2121
*/
2222
public $secret;
23+
24+
/**
25+
* Permission settings for S3
26+
* For the list of available permissions see: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#permissions
27+
* @var string
28+
*/
29+
public $permissions;
30+
31+
/**
32+
* S3 storage class
33+
* Use REDUCED_REDUNDANCY to reduce storage redundancy
34+
* @var string
35+
*/
36+
public $storage_class;
2337
}

src/Classes/TranscodingTask.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Qencode\Classes;
44

5+
use Qencode\Exceptions\QencodeClientException;
6+
57
class TranscodingTask {
68

79
private $api;
@@ -119,15 +121,15 @@ public function startCustom($task_params, $payload = null) {
119121
public function getStatus() {
120122
$params = array('task_tokens[]' => $this->taskToken);
121123
//TODO: fallback to /v1/status
122-
//echo "Checking status: ".$this->statusUrl."\n";
123124
$response = $this->api->post($this->statusUrl, $params);
124-
$status = $response['statuses'][$this->taskToken];
125-
$this->lastStatus = $status;
126-
if (is_array($status) and array_key_exists('status_url', $status)) {
127-
$this->statusUrl = $status['status_url'];
125+
$this->lastStatus = $response['statuses'][$this->taskToken];
126+
if ($this->lastStatus == null) {
127+
throw new QencodeClientException('Task '. $this->taskToken. ' not found!');
128128
}
129-
130-
return $status;
129+
if (array_key_exists('status_url', $this->lastStatus)) {
130+
$this->statusUrl = $this->lastStatus['status_url'];
131+
}
132+
return $this->lastStatus;
131133
}
132134

133135
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Qencode\Exceptions;
4+
5+
/**
6+
* Qencode exception thrown by API client code
7+
*/
8+
class QencodeClientException extends QencodeException
9+
{
10+
}

src/QencodeApiClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ private function request($method, $path, array $params = [])
131131
if (!empty($params) & is_array($params)) {
132132
$params = http_build_query($params);
133133
}
134-
//echo $url;
135-
//echo "<br>";
136-
//echo $params."<br><br>";
134+
#echo $url;
135+
#echo "\n";
136+
#echo $params."\n\n";
137137
$curl = curl_init($url);
138138

139139
curl_setopt($curl, CURLOPT_USERPWD, $this->key);
@@ -161,8 +161,8 @@ private function request($method, $path, array $params = [])
161161
throw new QencodeException('CURL: ' . $error, $errorNumber);
162162
}
163163

164-
//echo $this->lastResponseRaw;
165-
//echo "<br><br>";
164+
#echo $this->lastResponseRaw;
165+
#echo "\n\n";
166166
$this->lastResponse = $response = json_decode($this->lastResponseRaw, true);
167167
//print_r($response);
168168

0 commit comments

Comments
 (0)