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