-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgyro.php
171 lines (135 loc) · 3.39 KB
/
gyro.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
$videoFile = 'video.mp4';
echo 'Extracting gyro data from video file'.PHP_EOL;
if(!file_exists($videoFile)){
die('Video not found');
}
// Extract gyro data
exec('./exiftool/exiftool -api largefilesupport=1 -ee '.$videoFile.' > raw_data.txt');
echo 'Generating CSV file'.PHP_EOL;
$data = file_get_contents('raw_data.txt');
$data = explode(PHP_EOL, $data);
// Extract only required lines from the metadata
$data = array_values(array_filter($data, function($row){
$row = explode(':', $row);
$key = trim($row[0]);
return in_array($key, [
'Time Code',
'Angular Velocity',
//'Accelerometer'
]);
}));
$array = [];
$array_line = [];
foreach($data as $row){
$items = explode(':', $row);
$key = trim($items[0]);
$value = trim($items[1]);
if(in_array($key, ['Angular Velocity','Accelerometer'])){
$key = str_replace('Angular Velocity', 'gyroADC', $key);
$value = explode(' ', $value);
$array_line[ $key .'[0]' ] = round($value[2] * 100 / pi());
$array_line[ $key .'[1]' ] = round($value[0] * 100 / pi());
$array_line[ $key .'[2]' ] = round($value[1] * 100 / pi());
} else {
$array_line[ 'time' ] = $value;
}
if ( count( $array_line ) === 4 ) {
$array[] = $array_line;
$array_line = [];
}
}
$class = new Array2Csv();
$class->data($array)->save('gyro_data.csv');
echo 'Done!';
class Array2Csv {
public $title = 'Export';
public $headings;
public $data = [];
public $seperator = ',';
public function __construct() {
}
/**
* Set the file title/name
*
* @param string $value
*/
public function title(string $value){
$this->title = $value;
}
/**
* Create headings
*
* @param array $data
*
* @return $this
*/
public function headings(array $data)
{
$this->headings = $data;
return $this;
}
/**
* Add the data
*
* @param array $data
*
* @return $this
*/
public function data(array $data)
{
$this->data = $data;
return $this;
}
/**
* Column seperator
*
* @param string $value
*
* @return $this
*/
public function seperator(string $value)
{
$this->seperator = $value;
return $this;
}
/**
* Generate the CSV
*/
public function convert()
{
$data = [];
// Add titles
$data[] = $this->headings ?? array_keys($this->data[0]);
// Add the data and remove the keys
$data = array_merge($data, array_map(function($row){
return array_values($row);
}, $this->data));
$text = '';
foreach($data as $row){
$row = array_map(function($column){
return '"' . $column . '"';
}, $row);
$text .= implode($this->seperator, $row) . PHP_EOL;
}
return $text;
}
/**
* Generate the CSV and download it
*/
public function download()
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' .$this->title.'.csv"');
echo $this->convert();
die;
}
/**
* Generate the CSV and save it
*/
public function save(string $name)
{
$data = $this->convert();
file_put_contents($name, $data);
}
}