Skip to content

Commit f454fcd

Browse files
committed
Metadata updates
static functions: get_video_dimensions, get_bitrate, get_framerate, get_duration
1 parent 41233ca commit f454fcd

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

examples/get_metadata.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44
use Qencode\Exceptions\QencodeApiException;
55
use Qencode\Exceptions\QencodeClientException;
66
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;
127
use Qencode\QencodeApiClient;
8+
use Qencode\Classes\Metadata;
139

1410
// Replace this with your API key
15-
$apiKey = '12345678';
11+
$apiKey = 'abcde12345';
1612
$video_url = 'https://nyc3.s3.qencode.com/qencode/bbb_30s.mp4';
1713

1814
$q = new QencodeApiClient($apiKey);
1915

2016
try {
17+
log_message('Getting metadata for: '.$video_url.' ...');
18+
$video_info = $q->getMetadata($video_url);
2119

22-
$metdata = $q->getMetadata($video_url);
20+
list($width, $height) = Metadata::get_video_dimensions($video_info);
21+
log_message('width, px: '.$width);
22+
log_message('height, px: '.$height);
2323

24-
log_message($metdata);
24+
$bitrate = Metadata::get_bitrate($video_info);
25+
log_message('bitrate, b/s: '.$bitrate);
26+
27+
$framerate = Metadata::get_framerate($video_info);
28+
log_message('framerate, fps: '.$framerate);
29+
30+
$duration = Metadata::get_duration($video_info);
31+
log_message('duration, sec: '.$duration);
2532

2633
echo "DONE!";
2734

@@ -37,7 +44,6 @@
3744
var_export($q->getLastResponseRaw());
3845
}
3946

40-
function log_message($json) {
41-
$msg = json_encode($json);
47+
function log_message($msg) {
4248
echo $msg."\n";
4349
}

src/Classes/Metadata.php

+62-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class Metadata extends TranscodingTask {
88
function __construct($api, $task_token)
99
{
10-
// вызов конструктора базового класс
1110
parent::__construct($api, $task_token);
1211
}
1312
/**
@@ -25,9 +24,6 @@ public function get(string $video_url) {
2524
do {
2625
sleep(5);
2726
$response =parent::getStatus();
28-
if (is_array($response) and array_key_exists('percent', $response)) {
29-
log_message("Completed: {$response['percent']}%");
30-
}
3127
} while ($response['status'] != 'completed');
3228

3329
$urlMet = '';
@@ -37,4 +33,66 @@ public function get(string $video_url) {
3733
$response = $this->api->get($urlMet);
3834
return $response;
3935
}
36+
37+
public static function get_a_stream_by_codec_type($video_info, $codec_type) {
38+
if (! array_key_exists('streams', $video_info)) {
39+
return null;
40+
}
41+
foreach ($video_info['streams'] as $stream) {
42+
if ($stream['codec_type'] == $codec_type) {
43+
return $stream;
44+
}
45+
}
46+
return null;
47+
}
48+
49+
public static function get_video_stream_info($video_info) {
50+
return self::get_a_stream_by_codec_type($video_info, 'video');
51+
}
52+
53+
public static function get_audio_stream_info($video_info) {
54+
return self::get_a_stream_by_codec_type($video_info, 'audio');
55+
}
56+
57+
public static function get_video_dimensions($video_info) {
58+
$video_stream = self::get_video_stream_info($video_info);
59+
if (! $video_stream)
60+
return null;
61+
return [$video_stream['width'], $video_stream['height']];
62+
}
63+
64+
public static function get_bitrate($video_info, $stream_type = 'video') {
65+
$stream = null;
66+
if ($stream_type == 'video') {
67+
$stream = self::get_video_stream_info($video_info);
68+
}
69+
if ($stream_type == 'audio') {
70+
$stream = self::get_audio_stream_info($video_info);
71+
}
72+
if (is_array($stream) and array_key_exists('bit_rate', $stream)) {
73+
$bitrate = $stream['bit_rate'];
74+
}
75+
else {
76+
$bitrate = null;
77+
}
78+
return $bitrate;
79+
}
80+
81+
public static function get_framerate($video_info) {
82+
$stream = self::get_video_stream_info($video_info);
83+
$avg_frame_rate = $stream['avg_frame_rate'];
84+
if (strpos($avg_frame_rate, '/') !== FALSE) {
85+
$buf = explode('/', $avg_frame_rate);
86+
$framerate = floatval($buf[0]) / floatval($buf[1]);
87+
}
88+
else {
89+
$framerate = floatval($avg_frame_rate);
90+
}
91+
return round($framerate, 2);
92+
}
93+
94+
public static function get_duration($video_info) {
95+
return $video_info['format']['duration'];
96+
}
97+
4098
}

0 commit comments

Comments
 (0)