-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtime.class.php
129 lines (117 loc) · 4.12 KB
/
mtime.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
class MTimeException extends Exception{};
/**
* Klasa pomocnicza, do konwersji czasów filu z i na timestampa
* (z milisekundami).
* @access: public
* @author: Wojciech Bajon
* @version: $Id: mtime.class.php,v 1.5 2009/09/11 22:01:25 wojtekb Exp $
*/
if(!defined('MTimeBaseDate'))
define('MTimeBaseDate', '1990-01-01');
define('MTimeBaseTime', strtotime(MTimeBaseDate.' 00:00:00'));
if(!defined('MTimeDefaultFPS'))
define('MTimeDefaultFPS',23.9);
define('MTimeDefaultFPSf', 1/MTimeDefaultFPS);
if(!defined('MTimeEqPrecision'))
define('MTimeEqPrecision',0.9); //sec
class MTime /* throws MTimeException */{
/**
* Only static usage
*/
private function __construct(){ }
//==========================================================================
/**
* Micro DVD (mdvd): {1100}{1199}.
*/
/// Fps factor.
private static $fpsf = MTimeDefaultFPSf; /* 23.9 */
public static function setFps($v){
self::$fpsf = 1.0/$v;
}
public static function getFps(){
return 1.0/self::$fpsf;
}
public static function fromMdvd($v){
$v = preg_replace("/[^\d]/",'',$v);
if($v == '')
return null;
return MTimeBaseTime + floatval($v)*self::$fpsf;
}
public static function toMdvd($t){
return round(($t-MTimeBaseTime)/self::$fpsf);
}
//==========================================================================
/**
* TMPlayer (tmp): 00:01:40.
*/
public static function fromTmp($v){
return strtotime(MTimeBaseDate.' '
.trim(preg_replace("/[^\d:]/",'',$v),':'));
}
public static function toTmp($t){
return date('G:i:s', round($t));
}
//==========================================================================
/**
* Mplayer 2 (mpl): [994][1023].
*/
public static function fromMpl($v){
return MTimeBaseTime + floatval(preg_replace("/[^\d]/",'',$v))/10;
}
public static function toMpl($t){
return round(($t-MTimeBaseTime)*10);
}
//==========================================================================
/**
* SubStation Alpha (SSA) && Advanced SSA (ASS): 0:00:04.95,0:00:05.70
* !! Throws Errors
*/
public static function fromAss($v,$d='.'){
$v = explode($d,preg_replace("/[^\d:".($d=='.'?'\.':$d)."]/",'',$v));
if(count($v)!=2)
throw new MTimeException('Conversion Error.',1);
return strtotime(MTimeBaseDate.' '.$v[0])+floatval('0.'.$v[1]);
}
public static function toAss($t,$d='.'){
list($foo,$tm) = explode('.',$t);
return date($d=='.'?'G:i:s':'H:i:s',floor($t))
.$d.($d=='.'?substr($tm.'0000',0,2):substr($tm.'0000',0,3));
}
//==========================================================================
/**
* SRT (srt): 00:02:09,700 --> 00:02:12,300
* !! Throws Errors
*/
public static function fromSrt($v){
return self::fromAss($v,',');
}
public static function toSrt($t){
return self::toAss($t,',');
}
/**
* Comparison with the reference time accuracy.
* The comparison function return an integer less than, equal to,
* or greater than zero if the first argument is considered to be
* respectively less than, equal (witch precision) to, or greater
* than the second.
* Porównaj czasy z zadaną prezycją (domyślnie MTimeEqPrecision),
* ale może być inna.
*
* @param float $t1: Time 1
* @param float $t2: Time 2
* @param float $pr: Precision.
* @return int -1: t1 less than t2; 0: t1 eq t2; +1 t1 more than t2
*/
public static function eq($t1, $t2, $pr = MTimeEqPrecision){
if((abs($t1-$t2) - $pr) < $pr)
return 0;
if(($t1-$t2) < 0)
return -1;
return 1;
}
public static function getVersion(){
return '$Id: mtime.class.php,v 1.5 2009/09/11 22:01:25 wojtekb Exp $';
}
}
?>