scratch – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of PHP-FFmpeg. |
||
5 | * |
||
6 | * (c) Alchemy <info@alchemy.fr> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace FFMpeg\FFProbe\DataMapping; |
||
13 | |||
14 | use FFMpeg\Exception\LogicException; |
||
15 | use FFMpeg\Exception\RuntimeException; |
||
16 | use FFMpeg\Coordinate\Dimension; |
||
17 | |||
18 | class Stream extends AbstractData |
||
19 | { |
||
20 | /** |
||
21 | * Returns true if the stream is an audio stream. |
||
22 | * |
||
23 | * @return Boolean |
||
24 | */ |
||
25 | public function isAudio() |
||
26 | { |
||
27 | return $this->get('codec_type') === 'audio'; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Returns true if the stream is a video stream. |
||
32 | * |
||
33 | * @return Boolean |
||
34 | */ |
||
35 | public function isVideo() |
||
36 | { |
||
37 | return $this->get('codec_type') === 'video'; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Returns the dimension of the video stream. |
||
42 | * |
||
43 | * @return Dimension |
||
44 | * |
||
45 | * @throws LogicException In case the stream is not a video stream. |
||
46 | * @throws RuntimeException In case the dimensions can not be extracted. |
||
47 | */ |
||
48 | public function getDimensions() |
||
49 | { |
||
50 | if (!$this->isVideo()) { |
||
51 | throw new LogicException('Dimensions can only be retrieved from video streams.'); |
||
52 | } |
||
53 | |||
54 | $sampleRatio = $displayRatio = null; |
||
55 | |||
56 | $width = $this->get('width'); |
||
57 | $height = $this->get('height'); |
||
58 | |||
59 | if (null !== $ratio = $this->extractRatio($this, 'sample_aspect_ratio')) { |
||
60 | $sampleRatio = $ratio; |
||
61 | } |
||
62 | if (null !== $ratio = $this->extractRatio($this, 'display_aspect_ratio')) { |
||
63 | $displayRatio = $ratio; |
||
64 | } |
||
65 | |||
66 | if (null === $height || null === $width) { |
||
67 | throw new RuntimeException('Unable to extract dimensions.'); |
||
68 | } |
||
69 | |||
70 | if (null !== $displayRatio && null !== $sampleRatio) { |
||
71 | if ($sampleRatio[0] !== 1 && $sampleRatio[1] !== 1) { |
||
72 | if (null !== $width && null !== $height) { |
||
73 | // stretch video according to pixel sample aspect ratio |
||
74 | $width = round($width * ($sampleRatio[0] / $sampleRatio[1])); |
||
75 | // set height according to display aspect ratio |
||
76 | $height = round($width * ($displayRatio[1] / $displayRatio[0])); |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | |||
81 | return new Dimension($width, $height); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Extracts a ratio from a string in a \d+:\d+ format given a key name. |
||
86 | * |
||
87 | * @param Stream $stream The stream where to look for the ratio. |
||
88 | * @param string $name the name of the key. |
||
89 | * @return null|array An array containing the width and the height, null if not found. |
||
90 | */ |
||
91 | private function extractRatio(Stream $stream, $name) |
||
92 | { |
||
93 | if (!$stream->has($name)) { |
||
94 | return; |
||
95 | } |
||
96 | |||
97 | $ratio = $stream->get($name); |
||
98 | if (preg_match('/\d+:\d+/', $ratio)) { |
||
99 | $data = array_filter(explode(':', $ratio), function ($int) { |
||
100 | return $int > 0; |
||
101 | }); |
||
102 | if (2 === count($data)) { |
||
103 | return array_map(function ($int) { return (int) $int; }, $data); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |