1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @see https://github.com/soluble-io/soluble-mediatools for the canonical repository |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude) |
9
|
|
|
* @license https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Soluble\MediaTools\Video; |
13
|
|
|
|
14
|
|
|
use Soluble\MediaTools\Common\Exception\InvalidArgumentException; |
15
|
|
|
use Soluble\MediaTools\Video\Adapter\FFMpegCLIValueInterface; |
16
|
|
|
|
17
|
|
|
final class SeekTime implements FFMpegCLIValueInterface |
18
|
|
|
{ |
19
|
|
|
/** @var float */ |
20
|
|
|
private $time; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param float $seconds seconds and optional milliseconds as decimals |
24
|
|
|
*/ |
25
|
23 |
|
public function __construct(float $seconds) |
26
|
|
|
{ |
27
|
23 |
|
$this->time = $seconds; |
28
|
23 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $hmsmTime 'HOURS:MM:SS.MILLISECONDS' like in '01:23:45.678' |
32
|
|
|
* |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
2 |
|
public static function createFromHMS(string $hmsmTime): self |
36
|
|
|
{ |
37
|
2 |
|
return new self(self::convertHMSmToSeconds($hmsmTime)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Convert 'HOURS:MM:SS.MILLISECONDS' format to seconds with milli |
42
|
|
|
* Note: FFMpeg refer to this format as 'sexagesimal'. |
43
|
|
|
* |
44
|
|
|
* @param string $hmsmTime 'HOURS:MM:SS.MILLISECONDS' like in '01:23:45.678' |
45
|
|
|
* |
46
|
|
|
* @return float i.e 123.642 |
47
|
|
|
* |
48
|
|
|
* @throws InvalidArgumentException |
49
|
|
|
*/ |
50
|
4 |
|
public static function convertHMSmToSeconds(string $hmsmTime): float |
51
|
|
|
{ |
52
|
|
|
[ $secondsWithMilli, |
53
|
|
|
$minutes, |
54
|
|
|
$hours, |
55
|
4 |
|
] = array_merge(array_reverse(explode(':', $hmsmTime)), [0, 0, 0]); |
56
|
|
|
|
57
|
4 |
|
if (!is_numeric($secondsWithMilli) || $secondsWithMilli < 0 || $secondsWithMilli >= 60.0) { |
58
|
1 |
|
throw new InvalidArgumentException(sprintf( |
59
|
1 |
|
'Seconds \'%s\' are incorrect in \'%s\'', |
60
|
|
|
$secondsWithMilli, |
61
|
|
|
$hmsmTime |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
if (!is_numeric($minutes) || $minutes < 0 || $minutes >= 60.0) { |
66
|
1 |
|
throw new InvalidArgumentException(sprintf( |
67
|
1 |
|
'Minutes \'%s\' are incorrect in \'%s\'', |
68
|
|
|
$minutes, |
69
|
|
|
$hmsmTime |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
if (!is_numeric($hours) || $hours < 0) { |
74
|
1 |
|
throw new InvalidArgumentException(sprintf( |
75
|
1 |
|
'Hours \'%s\' are incorrect in \'%s\'', |
76
|
|
|
$hours, |
77
|
|
|
$hmsmTime |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return (float) $secondsWithMilli + ((int) $minutes) * 60 + ((int) $hours) * 3600; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws InvalidArgumentException |
86
|
|
|
*/ |
87
|
18 |
|
public static function convertSecondsToHMSs(float $secondsWithMilli): string |
88
|
|
|
{ |
89
|
18 |
|
if ($secondsWithMilli < 0) { |
90
|
1 |
|
throw new InvalidArgumentException(sprintf( |
91
|
1 |
|
"Cannot convert negative time to HMSs: \'%s\'", |
92
|
1 |
|
(string) $secondsWithMilli |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
17 |
|
[$time, $milli] = array_merge(explode('.', (string) $secondsWithMilli), [0, 0]); |
97
|
|
|
|
98
|
17 |
|
return sprintf( |
99
|
17 |
|
'%d:%02d:%02d.%d', |
100
|
17 |
|
((int) $time / 3600), |
101
|
17 |
|
((int) $time % 3600 / 60), |
102
|
17 |
|
((int) $time % 3600 % 60), |
103
|
|
|
$milli |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return time in seconds with milli. |
109
|
|
|
* |
110
|
|
|
* @return float |
111
|
|
|
*/ |
112
|
2 |
|
public function getTime(): float |
113
|
|
|
{ |
114
|
2 |
|
return $this->time; |
115
|
|
|
} |
116
|
|
|
|
117
|
16 |
|
public function getFFmpegCLIValue(): string |
118
|
|
|
{ |
119
|
16 |
|
return self::convertSecondsToHMSs($this->time); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|