Skip to content

Commit 5f0d762

Browse files
committed
Add metadata
1 parent b0d7970 commit 5f0d762

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed

autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"Classes/Destination.php",
1111
"Classes/Format.php",
1212
"Classes/Stream.php",
13+
"Classes/Metadata.php",
1314
"Classes/VideoCodecParameters.php",
1415
"QencodeApiClient.php",
1516
);

examples/get_metadata.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
require_once __DIR__ . '/../autoload.php';
3+
4+
use Qencode\Exceptions\QencodeApiException;
5+
use Qencode\Exceptions\QencodeClientException;
6+
use Qencode\Exceptions\QencodeException;
7+
use Qencode\Classes\CustomTranscodingParams;
8+
use Qencode\Classes\Format;
9+
use Qencode\Classes\Stream;
10+
use Qencode\Classes\Destination;
11+
use Qencode\Classes\Libx264_VideoCodecParameters;
12+
use Qencode\QencodeApiClient;
13+
14+
// Replace this with your API key
15+
$apiKey = '12345678';
16+
$video_url = 'https://nyc3.s3.qencode.com/qencode/bbb_30s.mp4';
17+
18+
$q = new QencodeApiClient($apiKey);
19+
20+
try {
21+
22+
$metdata = $q->getMetadata($video_url);
23+
24+
log_message($metdata);
25+
26+
echo "DONE!";
27+
28+
} catch (QencodeClientException $e) {
29+
// We got some inconsistent state in client application (e.g. task_token not found when requesting status)
30+
log_message('Qencode Client Exception: ' . $e->getCode() . ' ' . $e->getMessage());
31+
} catch (QencodeApiException $e) {
32+
// API response status code was not successful
33+
log_message('Qencode API Exception: ' . $e->getCode() . ' ' . $e->getMessage());
34+
} catch (QencodeException $e) {
35+
// API call failed
36+
log_message('Qencode Exception: ' . $e->getMessage());
37+
var_export($q->getLastResponseRaw());
38+
}
39+
40+
function log_message($json) {
41+
$msg = json_encode($json);
42+
echo $msg."\n";
43+
}

src/Classes/Format.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
namespace Qencode\Classes;
44

55
class Format {
6+
67
/**
78
* Output video format. Currently supported values are mp4, webm, advanced_hls, advanced_dash. Required.
89
* @var string
910
*/
1011
public $output;
1112

13+
/**
14+
* Output metadata version.
15+
* @var string
16+
*/
17+
public $metadata_version;
1218
/**
1319
* Output video file extension (for MP4 - defaults to '.mp4', for WEBM - defaults to '.webm').
1420
* @var string

src/Classes/Metadata.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Qencode\Classes;
4+
5+
use Qencode\Exceptions\QencodeClientException;
6+
7+
class Metadata extends TranscodingTask {
8+
function __construct($api, $task_token)
9+
{
10+
// вызов конструктора базового класс
11+
parent::__construct($api, $task_token);
12+
}
13+
/**
14+
* Gets Metadata vidoe
15+
* @return object
16+
*/
17+
public function get(string $video_url) {
18+
$params = new CustomTranscodingParams();
19+
$format = new Format();
20+
$format->output = "metadata";
21+
$format->metadata_version = "4.1.5";
22+
$params->source = $video_url;
23+
$params->format = [$format];
24+
parent::startCustom($params);
25+
do {
26+
sleep(5);
27+
$response =parent::getStatus();
28+
if (is_array($response) and array_key_exists('percent', $response)) {
29+
log_message("Completed: {$response['percent']}%");
30+
}
31+
} while ($response['status'] != 'completed');
32+
33+
$urlMet = '';
34+
foreach ($response['videos'] as $video) {
35+
$urlMet = $video['url'];
36+
}
37+
$response = $this->api->get($urlMet);
38+
return $response;
39+
}
40+
}

src/Classes/TranscodingTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TranscodingTask {
88

9-
private $api;
9+
public $api;
1010
private $taskToken;
1111
private $statusUrl;
1212
private $lastStatus;

src/QencodeApiClient.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Qencode\Exceptions\QencodeApiException;
66
use Qencode\Exceptions\QencodeException;
77
use Qencode\Classes\TranscodingTask;
8+
use Qencode\Classes\Metadata;
89

910
/**
1011
* Class QencodeClient
@@ -80,7 +81,7 @@ private function getAccessToken() {
8081
$response = $this->post("access_token", array('api_key' => $this->key));
8182
$this->access_token = $response['token'];
8283
}
83-
84+
8485
/**
8586
* Returns total available item count from the last request if it supports paging (e.g order list) or null otherwise.
8687
*
@@ -105,6 +106,20 @@ public function post($path, $params = [], $arrays = null)
105106
return $this->request('POST', $path, $params, $arrays);
106107
}
107108

109+
110+
/**
111+
* Perform a GET request to the API
112+
* @param string $path Request path (e.g. 'start_encode')
113+
* @param array $data Request body data as an associative array
114+
* @param array $params Additional GET parameters as an associative array
115+
* @return mixed API response
116+
* @throws \Qencode\Exceptions\QencodeApiException if the API call status code is not in the 2xx range
117+
* @throws QencodeException if the API call has failed or the response is invalid
118+
*/
119+
public function get($path, $params = [], $arrays = null)
120+
{
121+
return $this->request('GET', $path, $params, $arrays);
122+
}
108123
/**
109124
* Return raw response data from the last request
110125
* @return string|null Response data
@@ -165,7 +180,8 @@ private function request($method, $path, array $params = [], $arrays = null)
165180
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
166181
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
167182
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
168-
183+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
184+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
169185
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->curlConnectTimeout);
170186
curl_setopt($curl, CURLOPT_TIMEOUT, $this->curlTimeout);
171187

@@ -190,6 +206,9 @@ private function request($method, $path, array $params = [], $arrays = null)
190206
$this->lastResponse = $response = json_decode($this->lastResponseRaw, true);
191207
//print_r($response);
192208

209+
if($method == 'GET')
210+
return $response;
211+
193212
if (!isset($response['error'])) {
194213
$e = new QencodeException('Invalid API response');
195214
$e->rawResponse = $this->lastResponseRaw;
@@ -214,4 +233,15 @@ public function createTask() {
214233
$task = new TranscodingTask($this, $response['task_token']);
215234
return $task;
216235
}
236+
237+
/**
238+
* @param string
239+
*/
240+
public function getMetadata(string $url)
241+
{
242+
$response = $this->post('create_task', array('token' => $this->access_token));
243+
$metadata = new Metadata($this, $response['task_token']);
244+
$videoInfo = $metadata->get($url);
245+
return $videoInfo;
246+
}
217247
}

0 commit comments

Comments
 (0)