-
Notifications
You must be signed in to change notification settings - Fork 7
/
libHikvision.php
595 lines (513 loc) · 16.9 KB
/
libHikvision.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
/*
* Hikvision CCTV Class, version 1.2
* This class will parse a Hikvision index file (e.g. index00.bin) tha
* typically gets stored on external media such as an SD card or NFS share.
*
* Access to avconv and shell() is required for the creation of thumbnails.
*
* Thanks go to Alexey Ozerov for his C++ hiktools utility:
* https://github.com/aloz77/hiktools
*
*
*/
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
define("HEADER_LEN", 1280); // Length of the header struct in bytes.
define("FILE_LEN", 32); // Length of the file struct in bytes.
define("SEGMENT_LEN", 80); // Length of the segment struct in bytes.
define("NASINFO_LEN", 68); // Length of info.bin used on NAS storage.
class hikvisionCCTV
{
private $configuration;
///
/// __construct( Array of paths to datadir's' )
/// Created a new instance of this class. The path MUST end in a '/'.
///
public function __construct( $_paths )
{
$paths = $_paths;
// Create a configuration array for each datadir we are going to work
// with.
$this->configuration = array();
// If a single path is provided check to see if it' an NAS info file.
if(!is_array($_paths) && pathinfo($_paths, PATHINFO_BASENAME) == "info.bin")
{
// Parse info.bin and populate our configuration array with the
// details.
$dataDirCount = $this->getNASInfo($_paths)['DataDirs'];
$pathRoot = pathinfo($_paths, PATHINFO_DIRNAME );
$paths = array();
// Add list of datadir's to local paths array for iteration.
for($i=0; $i<$dataDirCount;$i++)
{
$paths[] = $this->pathJoin($pathRoot, 'datadir'.$i);
}
}
// Individual paths have been provided, add them to our configutation
// array.
foreach($paths as $path)
{
$indexfile = $this->pathJoin($path ,'index00.bin');
$indextype = 'bin';
if (!file_exists($indexfile)) {
$indexfile = $this->pathJoin($path ,'record_db_index00');
$indextype = 'sqlite';
}
$tmp = array(
'path' => $path,
'indexFile' => $indexfile,
'idxType' => $indextype
);
$this->configuration[] = $tmp;
}
}
///
/// getNASInfo( Path to NAS info.bin )
///
public function getNASInfo( $_infoFile )
{
$fh = fopen($_infoFile, 'rb');
// Read length of file header.
$data = fread($fh, NASINFO_LEN);
$tmp = unpack(
'a48serialNumber/'. // SERIALNO_LEN
'H12MACAddr/'. // MACADDR_LEN
'C2byRes/'.
'If_bsize/'. // create_info_file (f_bsize)
'If_blocks/'. // create_info_file (f_blocks)
'IDataDirs', $data);
fclose($fh);
return $tmp;
}
///
/// getDataDirNum( Path to Index File )
/// Return the index to the specified index File in our configuration array
///
private function getDataDirNum( $_index )
{
$pos = 0;
foreach($this->configuration as $dataDir)
{
if( $dataDir['indexFile'] == $_index )
return $pos;
$pos++;
}
}
///
/// getFileHeaderForIndexFile( Path to Index File )
/// Return array containing the file header from Hikvision "index00.bin".
/// Based on the work of Alex Ozerov. (https://github.com/aloz77/hiktools/)
///
private function getFileHeaderForIndexFile( $_indexFile )
{
$fh = fopen($_indexFile, 'rb');
// Read length of file header.
$data = fread($fh, HEADER_LEN);
$tmp = unpack(
'Q1modifyTimes/'.
'I1version/'.
'I1avFiles/'.
'I1nextFileRecNo/'.
'I1lastFileRecNo/'.
'C1176curFileRec/'.
'C76unknown/'.
'I1checksum', $data);
fclose($fh);
return $tmp;
}
///
/// getFilesForIndexFile( Path to Index File )
/// Return list of files. One video file may contain multiple segments,
/// i.e. multiple events - motion detection, etc.
/// Currently unused as it's more useful to return segments.
/// Based on the work of Alex Ozerov. (https://github.com/aloz77/hiktools/)
///
private function getFilesForIndexFile( $_indexFile )
{
$results = array();
$header = $this->getFileHeaderForIndexFile($_indexFile);
$fh = fopen($_indexFile, 'rb');
// Seek to end of header.
fread($fh, HEADER_LEN);
// Iterate over recordings.
for($i=0; $i<$header['avFiles']; $i++)
{
// Read length of recoridng header.
$data = fread($fh, FILE_LEN);
if( $data === false )
break;
// Unpack data from the file based on C data types.
$tmp = unpack(
'I1fileNo/'.
'S1chan/'.
'S1segRecNums/'.
'I1startTime/'. // time_t. Hikvision is x86 and uses a 4 Byte long.
'I1endTime/'. // time_t - Hikvision is x86 and uses a 5 Byte long.
'C1status/'.
'C1unknownA/'.
'S1lockedSegNum/'.
'C4unknownB/'.
'C8infoTypes/'
,$data);
if( $tmp['chan'] != 65535 )
array_push($results, $tmp);
}
fclose($fh);
return $results;
}
///
/// getSegments()
/// Returns an array of files and segments fror Hikvision data directories
/// by calling getSegmentsForIndexFile().
///
public function getSegments()
{
$results = array();
// Iterate over all datadir's
foreach($this->configuration as $dataDir)
{
// Get the segments for the index file of this datadir.
if ($dataDir['idxType'] == 'bin') {
$segments = $this->getSegmentsForIndexFile($dataDir['indexFile']);
} else if ($dataDir['idxType'] == 'sqlite') {
$segments = $this->getSegmentsForIndexFileSQL($dataDir['indexFile']);
}
// Iterate over this datadir's segments and append the segment to
// the results array.
foreach($segments as $segment)
{
$results[] = $segment;
}
}
return $results;
}
///
/// getSegmentsForIndexFileSQL( Path to Index File )
/// Returns an array of files and segments from a
/// Hikvision "record_db_index00" file.
///
private function getSegmentsForIndexFileSQL( $_indexFile )
{
$db = new SQLite3($_indexFile);
$dbquery = $db->query('SELECT
file_no as "cust_fileNum",
start_offset as "startOffset",
end_offset as "endOffset",
start_time_tv_sec as "cust_startTime",
end_time_tv_sec as "cust_endTime"
FROM record_segment_idx_tb
WHERE record_type != 0;
');
$results = array();
while ($row = $dbquery->fetchArray()) {
$row['cust_dataDirNum'] = $this->getDataDirNum($_indexFile);
array_push($results, $row);
// error_log(json_encode($row));
}
error_log(json_encode($results));
return $results;
}
///
/// getSegmentsForIndexFile( Path to Index File )
/// Returns an array of files and segments from a Hikvision "index00.bin"
/// file.
/// Based on the work of Alex Ozerov. (https://github.com/aloz77/hiktools/)
///
private function getSegmentsForIndexFile( $_indexFile )
{
// Maximum number of segments possible per recording.
$maxSegments = 256;
$results = array();
$fh = fopen($_indexFile, 'rb');
// Seek to the end of the header and recordings.
$header = $this->getFileHeaderForIndexFile($_indexFile);
$offset = HEADER_LEN + ($header['avFiles'] * FILE_LEN);
fread($fh, $offset);
// Iterate over the number of recordings we have.
for($i=0;$i<$header['avFiles'];$i++)
{
for ($j=0;$j<$maxSegments;$j++)
{
// Read length of the segment header.
$data = fread($fh, SEGMENT_LEN);
if($data === false)
break;
$tmp = unpack(
'C1type/'.
'C1status/'.
'C2resA/'.
'C4resolution/'.
'P1startTime/'. // unit64_t
'P1endTime/'. // uint64_t
'P1firstKeyFrame_absTime/'. // unit64_t
'I1firstKeyFrame_stdTime/'.
'I1lastFrame_stdTime/'.
'IstartOffset/'.
'IendOffset/'.
'C4resB/'.
'C4infoNum/'.
'C8infoTypes/'.
'C4infoStartTime/'.
'C2infoEndTime/'.
'C2existByte/'.
'C4infoStartOffset/'.
'C4infoEndOffset'
,$data);
$startTime = $this->convertTimestampTo32($tmp['startTime']);
$endTime = $this->convertTimestampTo32($tmp['endTime']);
$tmp['cust_startTime'] = $startTime;
$tmp['cust_endTime'] = $endTime;
$tmp['cust_fileNum'] = $i;
$tmp['cust_dataDirNum'] = $this->getDataDirNum($_indexFile);
$tmp['cust_indexFile'] = $_indexFile;
$tmp['fileExists'] = False;
if ($tmp['existByte1'] >= 160 && $tmp['existByte2'] >= 94) {
$tmp['fileExists'] = True;
}
// Ignore empty and those which are still recording.
if($tmp['type'] != 0 && $tmp['endTime'] != 0) //&& $tmp['fileExists'] == True)
array_push($results, $tmp);
}
}
fclose($fh);
return $results;
}
///
/// getSegmentsBetweenDates( Start Date , End Date)
/// Returns an array of segments between the specified dates.
///
public function getSegmentsBetweenDates($_start , $_end)
{
$results = array();
$segments = $this->getSegments();
// Iterate over segments associated with this recording.
foreach($segments as $segment)
{
// Check if the segment began recording in the specified window
if( $_start < $segment['cust_startTime'] && $_end > $segment['cust_endTime'] )
array_push($results, $segment);
}
return $results;
}
///
/// getSegmentsByDate( Start Date , End Date)
/// Returns an array of segments between the speficied dates, indexed by
/// day (unix timestamp)
///
public function getSegmentsByDate($_start, $_end)
{
$segments = $this->getSegmentsBetweenDates($_start, $_end);
// Iterate over the list of segments and index them by day.
$segmentsByDay = array();
foreach($segments as $segment)
{
$startTime = $segment['cust_startTime'];
$index = strtotime("midnight", $startTime);
// This day doesn't exist, add it to our list.
if(!isset( $segmentsByDay[$index] ))
{
$segmentsByDay[$index] = array(
'start' => $index,
'end' => strtotime("tomorrow", $startTime) - 1,
'segments' => array()
);
}
// Add segment to day.
$segmentsByDay[$index]['segments'][] = $segment;
}
return $segmentsByDay;
}
///
/// timeFilename( Prefix, Suffix, Start Time, End Time)
/// Generates a file name based on the speificed values. Used to generate an
// output file name for video clips.
///
public function timeFilename($_prefix, $_suffix, $_startTime, $_endTime)
{
$startTime = strftime("%Y-%m-%d_%H.%M.%S",$_startTime);
$endTime = strftime("%H.%M.%S", $_endTime);
return $_prefix."_".$startTime."_to_".$endTime.$_suffix;
}
//
// convertTimestampTo32( 64bit timestamp )
// Converts an unsigned long long (uint_64) to an unsigned long. Useful
// since PHP's 64bit timestamp support is useless.
//
public function convertTimestampTo32( $_in )
{
$mask = 0x00000000ffffffff;
return $_in & $mask;
}
///
/// getSegmentClipHTTP( Index File, File Number , Start Offset, End Offset )
/// Extracts a recording segment from the specified file, chunking the
/// request to 4kb at a time to conserve memory.
///
public function getSegmentClipHTTP( $_dataDirNum, $_file , $_startOffset, $_endOffset )
{
$file = $this->getFileName($_file);
$path = $this->pathJoin(
$this->configuration[$_dataDirNum]['path'],
$file
);
$fh = fopen( $path, 'rb');
if($fh == false)
die("Unable to open $path");
if( fseek($fh, $_startOffset) === false )
die("Unable to seek to position $_startOffset in $path");
header('Content-Disposition: attachment; filename="'.$file.'"');
if (ob_get_level() == 0)
ob_start();
while(ftell($fh) < $_endOffset)
{
print fread($fh, 4096);
}
ob_end_flush();
fclose($fh);
}
///
/// extractSegmentMP4( Index File, File Number , Start Offset, End Offset,
/// Cache Location )
/// Extracts a recording segment (likely x264) and copies the raw video
/// stream into an MP4 container that's more useful.
///
public function extractSegmentMP4( $_dataDirNum, $_file , $_startOffset, $_endOffset , $_cachePath , $_resolution )
{
$file = $this->getFileName($_file);
$path = $this->pathJoin(
$this->configuration[$_dataDirNum]['path'],
$file
);
$tempFileName = $_dataDirNum.'.'. $_file.'.'. $_startOffset.'.'. $_endOffset.'.'. $_resolution;
$pathExtracted = $this->pathJoin( $_cachePath, $tempFileName.'.h264');
$pathTranscoded = $this->pathJoin( $_cachePath, $tempFileName.'.mp4');
// If file already exists, return path to it.
if( file_exists( $pathTranscoded ))
return $pathTranscoded;
// Extract raw h264 footage and store in temp file. Avoiding
// pipes to improve performance. Testing showed just piping dd to
// ffmpeg was _really_ slow.
$fh = fopen( $path, 'rb');
if($fh == false)
die("Unable to open $path");
if( fseek($fh, $_startOffset) === false )
die("Unable to seek to position $_startOffset in $path");
while(ftell($fh) < $_endOffset)
{
file_put_contents($pathExtracted, fread($fh, 4096), FILE_APPEND);
}
fclose($fh);
// Extract footage and pass to avconv.
if( $_resolution != null and $_resolution != "null" )
$cmd = 'ffmpeg -i '.$pathExtracted.' -threads auto -s '.$_resolution.' -c:a none '.$pathTranscoded;
else
$cmd = 'ffmpeg -i '.$pathExtracted.' -threads auto -c:v copy -c:a none '.$pathTranscoded;
system($cmd);
// Transcode complete. Remove original file.
unlink($pathExtracted);
return $pathTranscoded;
}
///
/// streamFileToBrowser (Path to file)
/// Uses HTTP Range to stream a file to a browser. Neeed in Chrome and
/// other browsers to cleanly stream a file.
/// Based on code from:
/// http://www.media-division.com/php-download-script-with-resume-option/
///
function streamFileToBrowser( $_file )
{
/**
* Copyright 2012 Armand Niculescu - media-division.com
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ob_clean();
$fh = @fopen($_file, 'rb');
$file_size = filesize( $_file );
#header('Content-Type: video/mp4');
// Check if this is a HTTP Range request.
$range = '';
if(isset($_SERVER['HTTP_RANGE']))
{
list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if(!$size_unit == 'bytes')
{
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
// Multiple ranges could be specified.
list($range, $extra_ranges) = array_pad( explode(',',$range_orig,2),2,null);
}
// Figure out download chunk from range (if set).
list($seek_start, $seek_end) = array_pad(explode('-', $range,2),2,null);
// Set start and end based on range (if set).
// Also check for invalid ranges.
$seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
// IE Workaround:
// Only send partial content header if downloading a piece of a file
if( $seek_start > 0 || $seek_end < ($file_size -1))
{
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
header('Content-Length: '.($seek_end - $seek_start + 1));
}
else
{
#header('Content-Length: '.$file_size);
}
#header('Accept-Ranges: bytes');
set_time_limit(0);
fseek($fh, $seek_start);
ob_implicit_flush(true);
while(!feof($fh))
{
print(@fread($fh, 4096));
if (connection_status()!=0)
{
@fclose($fh);
exit;
}
}
@flose($fh);
ob_end_flush();
exit;
}
///
/// getFileName( File Number )
/// Returns the full path to the specified recording file.
///
public function getFileName( $_file )
{
$file = sprintf('hiv%05u.mp4', $_file);
return $file;
}
///
/// extractThumbnail(Data directory #, File Number, offset, Path to output file)
/// Extracts a thumbnail from a recording file based on the offset provided
///
public function extractThumbnail($_dataDirNum, $_file, $_offset, $_output)
{
$path = $this->pathJoin(
$this->configuration[$_dataDirNum]['path'],
$this->getFileName($_file)
);
if(!file_exists($_output))
{
$cmd = 'dd if='.$path.' skip='.$_offset.' ibs=1 | ffmpeg -i pipe:0 -vframes 1 -an '.$_output.' >/dev/null 2>&1';
system($cmd);
}
}
///
/// pathJoin (paths)
/// Joins two or more strings together to produce a valid file path.
///
private function pathJoin()
{
return preg_replace('~[/\\\]+~', DIRECTORY_SEPARATOR, implode(DIRECTORY_SEPARATOR, func_get_args()));
}
}
?>