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\Coordinate; |
||
13 | |||
14 | use FFMpeg\Exception\InvalidArgumentException; |
||
15 | |||
16 | class TimeCode |
||
17 | { |
||
18 | //see http://www.dropframetimecode.org/ |
||
19 | private $hours; |
||
20 | private $minutes; |
||
21 | private $seconds; |
||
22 | private $frames; |
||
23 | |||
24 | public function __construct($hours, $minutes, $seconds, $frames) |
||
25 | { |
||
26 | $this->hours = $hours; |
||
27 | $this->minutes = $minutes; |
||
28 | $this->seconds = $seconds; |
||
29 | $this->frames = $frames; |
||
30 | } |
||
31 | |||
32 | public function __toString() |
||
33 | { |
||
34 | return sprintf('%02d:%02d:%02d.%02d', $this->hours, $this->minutes, $this->seconds, $this->frames); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Creates timecode from string. |
||
39 | * |
||
40 | * @param string $timecode |
||
41 | * |
||
42 | * @return TimeCode |
||
43 | * |
||
44 | * @throws InvalidArgumentException In case an invalid timecode is supplied |
||
45 | */ |
||
46 | public static function fromString($timecode) |
||
47 | { |
||
48 | $days = 0; |
||
49 | |||
50 | if (preg_match('/^[0-9]+:[0-9]+:[0-9]+:[0-9]+\.[0-9]+$/', $timecode)) { |
||
51 | list($days, $hours, $minutes, $seconds, $frames) = sscanf($timecode, '%d:%d:%d:%d.%d'); |
||
52 | } elseif (preg_match('/^[0-9]+:[0-9]+:[0-9]+:[0-9]+:[0-9]+$/', $timecode)) { |
||
53 | list($days, $hours, $minutes, $seconds, $frames) = sscanf($timecode, '%d:%d:%d:%d:%d'); |
||
54 | } elseif (preg_match('/^[0-9]+:[0-9]+:[0-9]+\.[0-9]+$/', $timecode)) { |
||
55 | list($hours, $minutes, $seconds, $frames) = sscanf($timecode, '%d:%d:%d.%s'); |
||
56 | } elseif (preg_match('/^[0-9]+:[0-9]+:[0-9]+:[0-9]+$/', $timecode)) { |
||
57 | list($hours, $minutes, $seconds, $frames) = sscanf($timecode, '%d:%d:%d:%s'); |
||
58 | } else { |
||
59 | throw new InvalidArgumentException(sprintf('Unable to parse timecode %s', $timecode)); |
||
60 | } |
||
61 | |||
62 | $hours += $days * 24; |
||
63 | |||
64 | return new static($hours, $minutes, $seconds, $frames); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Creates timecode from number of seconds. |
||
69 | * |
||
70 | * @param float $quantity |
||
71 | * |
||
72 | * @return TimeCode |
||
73 | */ |
||
74 | public static function fromSeconds($quantity) |
||
75 | { |
||
76 | $minutes = $hours = $frames = 0; |
||
77 | |||
78 | $frames = round(100 * ($quantity - floor($quantity))); |
||
79 | $seconds = floor($quantity); |
||
80 | |||
81 | if ($seconds > 59) { |
||
82 | $minutes = floor($seconds / 60); |
||
83 | $seconds = $seconds % 60; |
||
84 | } |
||
85 | if ($minutes > 59) { |
||
86 | $hours = floor($minutes / 60); |
||
87 | $minutes = $minutes % 60; |
||
88 | } |
||
89 | |||
90 | return new static($hours, $minutes, $seconds, $frames); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns this timecode in seconds |
||
95 | * @return int |
||
96 | */ |
||
97 | public function toSeconds() { |
||
98 | $seconds = 0; |
||
99 | |||
100 | $seconds += $this->hours * 60 * 60; |
||
101 | $seconds += $this->minutes * 60; |
||
102 | $seconds += $this->seconds; |
||
103 | |||
104 | // TODO: Handle frames? |
||
105 | |||
106 | return (int) $seconds; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Helper function wether `$timecode` is after this one |
||
111 | * |
||
112 | * @param TimeCode $timecode The Timecode to compare |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function isAfter(TimeCode $timecode) { |
||
116 | // convert everything to seconds and compare |
||
117 | return ($this->toSeconds() > $timecode->toSeconds()); |
||
118 | } |
||
119 | |||
120 | } |