-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdvr-recover.py
executable file
·1163 lines (936 loc) · 38 KB
/
dvr-recover.py
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Stefan Haller <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
dvr-recover - extract MPEG2 files of digital video recorder hdd
===============================================================
Version: 0.6
Copyright (C) 2010 Stefan Haller <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Sqlite database file
--------------------
The script will create a sqlite3 database in the current directory with the
name "dvr-recover.sqlite". All settings and data will be stored in this
database.
Setup
-----
Before you can use dvr-recover you will have to change the default settings.
You can do this by simply calling the script with the parameter "setup"
followed by the setting to change and the new value. To change the value of
the blocksize setting call:
python dvr-recover.py setup blocksize 2048
Here is a full listing of all available settings:
The square brackets in this listing are representing the type of the value.
Replace them with an integer for "[integer]". Don't copy this square
brackets, so write
blocksize 2048
instead of
blocksize [2048]
input clear This option sets the path of the hard disk drive
input add [filename] file used as input. You can either use a copy of the
input del [filename] block device (created with something like dd or so) or
the block device directly (required root privileges).
The file must be readable. It's possible to specify
multiple hdd-files by calling the add parameter
multiply times. The script will threat the single files
as one big file. That way you can split the hdd into
smaller pieces.
parameter clear: clear list of input file
parameter add: add one file to list of input files
parameter del: delete one file from list of input
files
export_dir [string] Defines where the output should be written to. Must
match an existing path. Both relative and absolute
paths are accepted. Current directory is "./".
blocksize [integer] The blocksize of the filesystem can be configured
with this option. Set this to a value in bytes.
The default value is 2048 bytes. Probably this value
should work, but if not, you're free to tune it.
min_chunk_size [integer]
If the script finds chunks smaller than this size
(value must be given in blocks!), it will ignore them
silently. If this value is too small, the script
will find chunks that were deleted or can't be used.
Otherwise, if the value is too big, valuable chunks
will be ignored. The default value is 25600 blocks
(50 MiB by blocksize of 2048 bytes).
max_create_gap [integer]
The script will split the stream into two chunks if
it finds two frames where the timecode differs more
than this value. MPEG uses a clock of 90 kHz.
So the default value of 90,000 ticks equals one second.
max_sort_gap [integer]
See maxcreategap. This value is used to concatenate
two chunks if the difference of the timecode is smaller
than this value. The default value of 90,000 ticks
equals one second.
Input hdd file
--------------
You can create either a copy of the hdd or use the hdd directly as input.
Linux:
If you want to copy the hdd (assuming the hdd is /dev/sdb) then you can use
this dd command: (setting the blocksize to 10MB should increase performance.)
dd if=/dev/sdb if=hddfile bs=10MB
If you want to use the hdd directly as input, use this as value for hdd-file:
(Assuming that the hdd is /dev/sdb.)
hdd-file=/dev/sdb
Windows:
Use a tool to create a copy of the hdd. Direct access to Windows device
files (\\.\PhysicalDrive0, ...) is not supported at the moment.
Steps:
------
Step 1: Export information of chunks
The hdd file will be analyzed and all necessary information are collected.
This step may take quite a long time, be patient! This step is only necessary
once. All other steps will use the stored chunk info to save time.
(The script tries to find mpeg headers and extract the timecode. Depending on
the timecode it's possible to split the stream into separate chunks.)
Parameter: create
Step 2: Analyze and sort chunks
This step will analyze the stored chunk info and sort the chunks. The tools
tries to find parts of the same recording (by analyzing the timecode
informationof the chunks) and bring them into the right order.
Parameter: sort
Step 3: Show chunks
You can list all chunks to make sure that the program did the job properly.
Parameter: show
Step 4: Export chunks
This step will use the conditioned chunk data and export the chunks. You can
either export all chunks at once or select chunks. The tool will assembly all
parts of the same recording into one file.
Use paramater "show" to get the id of the chunk you want to extract. If you
call export without any additional parameter, all chunks will be exported.
Parameter: export
Additional Parameters:
----------------------
setup [setup-args] Manages all settings necessary for a working script.
setup show Show all settings.
setup reset Reset all settings to default values.
setup input clear
setup input add [FILE]
setup input remove [FILE]
setup blocksize [INTEGER]
setup exportdir [STRING]
setup minchunksize [INTEGER]
setup maxcreategap [INTEGER]
setup maxsortgap [INTEGER]
Usage:
------
usage
setup [setup-args]
create
sort
reset
clear
show
export [chunk-id]
Tested devices:
---------------
* Panasonic DMR-EH55
* Panasonic DMR-EH56
* Panasonic DMR-EH57
* Panasonic DMR-EX77
* Panasonic DMR-EX85
* Panasonic DMR-XW300
* Panasonic DVM-E80H
'''
import os
import os.path
import sqlite3
import sys
import time
class DvrRecoverError(Exception):
'''Base class for all Exceptions in this module'''
__slots__ = ('msg',)
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class SqlManagerError(DvrRecoverError):
pass
class CreateError(DvrRecoverError):
'''Error while creating chunk list'''
pass
class ExportError(DvrRecoverError):
'''Error while exporting chunks'''
pass
class UnexpectedResultError(DvrRecoverError):
'''Unexpected result encountered'''
pass
class FileReaderError(DvrRecoverError):
'''Exception class for FileReader class'''
pass
class Chunk(object):
'''Object to save information about one chunk'''
__slots__ = ('id',
'block_start',
'block_size',
'clock_start',
'clock_end',
'concat',
'new')
def __init__(self, new = True):
for i in self.__slots__:
setattr(self, i, None)
self.new = new
class Timer(object):
'''Time measurement'''
__slots__ = ('timecode')
def __init__(self):
self.reset()
def reset(self):
self.timecode = time.time()
def elapsed(self, reset = False):
result = time.time() - self.timecode
if reset:
self.reset()
return result
class FileReader(object):
'''Handle multiple input streams as one big file'''
__slots__ = ('parts', 'current_file', 'file')
def __init__(self, filenames):
'''Initialize FileReader'''
self.parts = []
for filename in filenames:
if filename[0:3] == r'\\.':
raise FileReaderError('Direct access to Windows devices files '
'is not supported currently.')
part = {'filename': filename,
'size': os.stat(filename).st_size}
if part['size'] == 0:
# size is most likely not 0, but it might be a special file
# (device file). Try to determine size in another way.
f = open(part['filename'], 'rb')
f.seek(0, os.SEEK_END) # seek end of file
part['size'] = f.tell() # current file position = file size
f.close()
self.parts.append(part)
self.current_file = None
self.file = None
def get_size(self):
'''Return the total size of all input streams'''
size = 0
for part in self.parts:
size += part['size']
return size
def get_index(self, offset):
'''Return the index of the file where offset is located'''
index = 0
start = 0
for part in self.parts:
end = start + part['size']
if ((offset >= start) and
(offset < end)):
return index
start = end
index += 1
return None
def get_offset(self, index):
'''Return the starting offset of a specified file part'''
i = 0
offset = 0
for part in self.parts:
if i < index:
offset += part['size']
else:
break
i += 1
return offset
def open(self, index):
'''Open input stream with the specified index'''
self.close()
if (index >= 0) and (index < len(self.parts)):
self.file = open(self.parts[index]['filename'], 'rb')
self.current_file = index
else:
raise FileReaderError('Index out of range!')
def close(self):
'''Close current input stream'''
if self.file is not None:
self.file.close()
self.current_file = None
self.file = None
def seek(self, offset):
'''Seek to offset (open correct file, seek, ...)'''
index = self.get_index(offset)
delta = offset - self.get_offset(index)
if self.current_file is None:
self.open(index)
else:
if self.current_file != index:
self.open(index)
self.file.seek(delta)
def is_eof(self):
'''Return true if eof of current file part is reached'''
return (self.file.tell() == self.parts[self.current_file]['size'])
def next_file(self):
'''Open next input file'''
if self.current_file + 1 < len(self.parts):
self.open(self.current_file + 1)
else:
self.close()
def read(self, size):
'''Read data from stream, automatically switch stream if necessary'''
if self.file is None:
raise FileReaderError('No files are open!')
buf = self.file.read(size)
delta = size - len(buf)
if delta != 0:
if self.is_eof():
self.next_file()
buf += self.read(delta)
else:
raise FileReaderError('Incomplete filled buffer without '
'reaching end of file!')
return buf
class SqlManager(object):
'''Interface to access data via SQL queries'''
__slots__ = ('conn',)
def __init__(self):
'''Initialize SqlManager'''
self.conn = None
def open(self, filename):
'''Open Sqlite3 database'''
self.conn = sqlite3.connect(filename)
self.init_db()
def close(self, commit=True):
'''Close database connection after optional commit'''
if commit:
self.commit()
self.conn.close()
def commit(self):
'''Commit all changes'''
self.conn.commit()
def init_db(self):
'''Create structure of database'''
self.conn.execute(
"CREATE TABLE IF NOT EXISTS chunk("
"id INTEGER PRIMARY KEY,"
"block_start INTEGER,"
"block_size INTEGER,"
"clock_start INTEGER,"
"clock_end INTEGER,"
"concat INTEGER"
")")
self.conn.execute(
"CREATE TABLE IF NOT EXISTS state("
"key TEXT PRIMARY KEY ON CONFLICT REPLACE,"
"value"
")")
self.conn.execute(
"CREATE TABLE IF NOT EXISTS setting("
"key TEXT PRIMARY KEY ON CONFLICT REPLACE,"
"value"
")")
def chunk_count(self):
'''Return count of rows in chunk table'''
return self.conn.execute("SELECT COUNT(*) FROM chunk").fetchone()[0]
def chunk_load(self, chunk_id):
'''Return chunk object by chunk_id'''
result = self.conn.execute(
"SELECT * FROM chunk "
"WHERE id = ?",
(chunk_id,)).fetchone()
if result is None:
return None
chunk = Chunk(False)
(chunk.id,
chunk.block_start,
chunk.block_size,
chunk.clock_start,
chunk.clock_end,
chunk.concat) = result
return chunk
def chunk_save(self, chunk):
'''Insert or update info in chunk table'''
if chunk.new:
cur = self.conn.execute(
"INSERT INTO chunk "
"VALUES (?, ?, ?, ?, ?, ?)",
(chunk.id,
chunk.block_start,
chunk.block_size,
chunk.clock_start,
chunk.clock_end,
chunk.concat))
chunk.id = cur.lastrowid
chunk.new = False
else:
self.conn.execute(
"UPDATE chunk "
"SET block_start = ?,"
"block_size = ?,"
"clock_start = ?,"
"clock_end = ?,"
"concat = ? "
"WHERE id = ?",
(chunk.block_start,
chunk.block_size,
chunk.clock_start,
chunk.clock_end,
chunk.concat,
chunk.id))
def chunk_delete_id(self, chunk_id):
'''Delete row from chunk table by id'''
self.conn.execute("DELETE FROM chunk WHERE id = ?",
(chunk_id,))
def chunk_delete(self, chunk):
'''Delete row from chunk table by chunk object'''
self.chunk_delete_id(chunk.id)
def chunk_reset(self):
'''Delete all rows from chunk table'''
self.conn.execute("DELETE FROM chunk")
def chunk_reset_concat(self):
'''Set concat to null for all rows in chunk table'''
self.conn.execute(
"UPDATE chunk "
"SET concat = null")
def chunk_query_ids(self):
'''Return iterator for all chunk ids'''
for result in self.conn.execute(
"SELECT id FROM chunk "
"ORDER BY clock_start"):
yield result[0]
def chunk_query(self):
'''Return iterator for all chunk objects'''
for chunk_id in self.chunk_query_ids():
yield self.chunk_load(chunk_id)
def chunk_query_concat(self, chunk):
'''Return chunk which should be concatenated to the current one'''
cur = self.conn.execute(
"SELECT id FROM chunk "
"WHERE concat = ?",
(chunk.id,))
result = cur.fetchone()
if result is None:
return None
if cur.fetchone() is not None:
raise SqlManagerError('Multiple chunks are referencing the same '
'chunk for concatenating!')
return self.chunk_load(result[0])
def chunk_fix_multiple_concats(self):
'''Fix multiple chunks referencing the same chunk in concat field'''
self.conn.execute(
"UPDATE chunk "
"SET concat = null "
"WHERE id IN "
"("
"SELECT a.id FROM chunk a "
"INNER JOIN chunk b ON a.id != b.id AND a.concat = b.concat"
")")
def state_reset(self):
'''Delete all entries of state table'''
self.conn.execute("DELETE FROM state")
def state_query(self, key):
'''Return value of state by key'''
result = self.conn.execute(
"SELECT value FROM state "
"WHERE key = ?",
(key,)).fetchone()
if result is None:
return None
return result[0]
def state_delete(self, key):
'''Delete entry in state table by key'''
self.conn.execute(
"DELETE from state "
"WHERE key = ?",
(key,))
def state_insert(self, key, value):
'''Insert key/value pair into state table'''
self.conn.execute(
"INSERT INTO state "
"VALUES (?, ?)",
(key, value))
def setting_reset(self):
'''Delete all entries of setting table'''
self.conn.execute("DELETE FROM setting")
def setting_query(self, key):
'''Return value of setting by key'''
result = self.conn.execute(
"SELECT value FROM setting "
"WHERE key = ?",
(key,)).fetchone()
if result is None:
return None
return result[0]
def setting_delete(self, key):
'''Delete entry in setting table by key'''
self.conn.execute(
"DELETE from setting "
"WHERE key = ?",
(key,))
def setting_insert(self, key, value):
'''Insert key/value pair into setting table'''
self.conn.execute(
"INSERT INTO setting "
"VALUES (?, ?)",
(key, value))
class ChunkFactory(object):
'''Extract information of all chunks'''
__slots__ = ('current_block', 'clock', 'old_clock', 'timer', 'timer_all',
'timer_blocks', 'blocksize', 'min_chunk_size', 'max_gap',
'db_manager', 'reader', 'input_blocks', 'chunk')
def __init__(self, main, reader):
self.current_block = 0
self.clock = 0
self.old_clock = 0
self.chunk = None
self.timer = Timer()
self.timer_all = Timer()
self.timer_blocks = 0
self.blocksize = main.blocksize
self.min_chunk_size = main.min_chunk_size
self.max_gap = main.max_create_gap
self.db_manager = main.db_manager
self.reader = reader
self.input_blocks = int(self.reader.get_size() / self.blocksize)
def save_state(self):
if self.chunk is None:
block_start = None
clock_start = None
else:
block_start = self.chunk.block_start
clock_start = self.chunk.clock_start
self.db_manager.state_insert(
'current_block',
self.current_block)
self.db_manager.state_insert(
'block_start',
block_start)
self.db_manager.state_insert(
'clock_start',
clock_start)
self.db_manager.state_insert(
'old_clock',
self.old_clock)
self.db_manager.state_insert(
'time_elapsed',
self.timer_all.elapsed())
self.db_manager.commit()
def load_state(self):
current_block = self.db_manager.state_query('current_block')
block_start = self.db_manager.state_query('block_start')
clock_start = self.db_manager.state_query('clock_start')
old_clock = self.db_manager.state_query('old_clock')
time_elapsed = self.db_manager.state_query('time_elapsed')
self.current_block = current_block
if (block_start is not None) and (clock_start is not None):
self.chunk = Chunk()
self.chunk.block_start = block_start
self.chunk.clock_start = clock_start
self.old_clock = old_clock
if time_elapsed is not None:
self.timer_all.timecode -= time_elapsed
def check_timer(self):
'''Print statistics and save state if timer elapses'''
delta = self.timer.elapsed()
if delta > 30:
self.timer.reset()
self.save_state()
chunk_count = self.db_manager.chunk_count()
speed = float(self.current_block - self.timer_blocks) \
/ float(delta)
print '[%5.1f%%] %i/%i blocks (%.1f bl/s; ' \
'%.1f MiB/s): %i chunks' % \
(
float(self.current_block) /
float(self.input_blocks) * 100.0,
self.current_block,
self.input_blocks,
speed,
float(speed * self.blocksize) / float(1024**2),
chunk_count
)
self.timer_blocks = self.current_block
def finished(self):
'''Print statistics and commit changes after finishing'''
self.db_manager.state_reset()
self.db_manager.commit()
delta = self.timer_all.elapsed()
chunk_count = self.db_manager.chunk_count()
speed = float(self.current_block + 1) / float(delta)
print
print 'Finished.'
print 'Read %i of %i blocks.' % (self.current_block ,
self.input_blocks)
print 'Found %i chunks.' % chunk_count
print 'Took %.2f seconds.' % delta
print 'Average speed was %.1f blocks/s (%.1f MiB/s).' % \
(speed, float(speed * self.blocksize) / float(1024**2))
def mpeg_header(self, buf):
'''Check if buffer is mpeg header and return system clock or None'''
# Partial Program Stream Pack header format
# =========================================
#
# Name |Number of bits| Description
# ----------------------|--------------|----------------------------
# sync bytes | 32 | 0x000001BA
# marker bits | 2 | 01b
# System clock [32..30] | 3 | System Clock Reference
# | | (SCR) bits 32 to 30
# marker bit | 1 | Bit always set.
# System clock [29..15] | 15 | System clock bits 29 to 15
# marker bit | 1 | Bit always set.
# System clock [14..0] | 15 | System clock bits 14 to 0
# marker bit | 1 | 1 Bit always set.
#
#
# [4] [5] [6] [7] [8] = buffer[x]
# 01000100|00000000|00000100|00000000|00000100 = marker bits
# ^^^ ^^ ^^^^^^^^ ^^^^^ ^^ ^^^^^^^^ ^^^^^ = SCR
#
# SCR -> 90 kHz Timer
#
# See http://en.wikipedia.org/wiki/MPEG_program_stream#Coding_structure
if ((ord(buf[0]) != 0x00) or
(ord(buf[1]) != 0x00) or
(ord(buf[2]) != 0x01) or
(ord(buf[3]) != 0xBA)):
return None
marker_bit_1 = (ord(buf[4]) >> 6) & 3
marker_bit_2 = (ord(buf[4]) >> 2) & 1
marker_bit_3 = (ord(buf[6]) >> 2) & 1
marker_bit_4 = (ord(buf[8]) >> 2) & 1
if ((marker_bit_1 != 1) or
(marker_bit_2 != 1) or
(marker_bit_3 != 1) or
(marker_bit_4 != 1)):
return None
clock_bits = [None] * 33
clock_bits[32] = (ord(buf[4]) >> 5) & 1;
clock_bits[31] = (ord(buf[4]) >> 4) & 1;
clock_bits[30] = (ord(buf[4]) >> 3) & 1;
clock_bits[29] = (ord(buf[4]) >> 1) & 1;
clock_bits[28] = (ord(buf[4]) >> 0) & 1;
clock_bits[27] = (ord(buf[5]) >> 7) & 1;
clock_bits[26] = (ord(buf[5]) >> 6) & 1;
clock_bits[25] = (ord(buf[5]) >> 5) & 1;
clock_bits[24] = (ord(buf[5]) >> 4) & 1;
clock_bits[23] = (ord(buf[5]) >> 3) & 1;
clock_bits[22] = (ord(buf[5]) >> 2) & 1;
clock_bits[21] = (ord(buf[5]) >> 1) & 1;
clock_bits[20] = (ord(buf[5]) >> 0) & 1;
clock_bits[19] = (ord(buf[6]) >> 7) & 1;
clock_bits[18] = (ord(buf[6]) >> 6) & 1;
clock_bits[17] = (ord(buf[6]) >> 5) & 1;
clock_bits[16] = (ord(buf[6]) >> 4) & 1;
clock_bits[15] = (ord(buf[6]) >> 3) & 1;
clock_bits[14] = (ord(buf[6]) >> 1) & 1;
clock_bits[13] = (ord(buf[6]) >> 0) & 1;
clock_bits[12] = (ord(buf[7]) >> 7) & 1;
clock_bits[11] = (ord(buf[7]) >> 6) & 1;
clock_bits[10] = (ord(buf[7]) >> 5) & 1;
clock_bits[ 9] = (ord(buf[7]) >> 4) & 1;
clock_bits[ 8] = (ord(buf[7]) >> 3) & 1;
clock_bits[ 7] = (ord(buf[7]) >> 2) & 1;
clock_bits[ 6] = (ord(buf[7]) >> 1) & 1;
clock_bits[ 5] = (ord(buf[7]) >> 0) & 1;
clock_bits[ 4] = (ord(buf[8]) >> 7) & 1;
clock_bits[ 3] = (ord(buf[8]) >> 6) & 1;
clock_bits[ 2] = (ord(buf[8]) >> 5) & 1;
clock_bits[ 1] = (ord(buf[8]) >> 4) & 1;
clock_bits[ 0] = (ord(buf[8]) >> 3) & 1;
clock = 0
for i in range(0,33):
clock += clock_bits[i] * 2**i
return clock
def split(self):
'''End current chunk and start a new one'''
if self.chunk is not None:
self.chunk.block_size = self.current_block - \
self.chunk.block_start
self.chunk.clock_end = self.old_clock
if (self.chunk.block_size >= self.min_chunk_size):
self.db_manager.chunk_save(self.chunk)
self.chunk = None
def run(self):
'''Main function for this class'''
self.load_state()
if self.current_block is None:
if self.db_manager.chunk_count() != 0:
raise CreateError('No state information, but chunk '
'count is not 0. Probably the scan '
'finished already. Abort process to '
'avoid loss of data. Use parameter '
'clear to clear database (you will '
'lose all chunk information).')
self.current_block = 0
self.db_manager.state_reset()
self.timer_blocks = self.current_block
self.reader.seek(self.current_block * self.blocksize)
for self.current_block in xrange(self.current_block,
self.input_blocks):
self.check_timer()
buf = self.reader.read(self.blocksize)
if len(buf) != self.blocksize:
raise UnexpectedResultError('len(buf) != '
'self.blocksize')
self.clock = self.mpeg_header(buf)
if self.clock is None:
self.split()
else:
if self.chunk is None:
self.chunk = Chunk()
self.chunk.block_start = self.current_block
self.chunk.clock_start = self.clock
else:
delta = self.clock - self.old_clock
if (delta < 0) or (delta > self.max_gap):
self.split()
if self.chunk is None:
self.chunk = Chunk()
self.chunk.block_start = self.current_block
self.chunk.clock_start = self.clock
self.old_clock = self.clock
self.current_block = self.current_block + 1
self.split()
self.finished()
class Main(object):
'''Main class for this application'''
__slots__ = ('input_filenames', 'db_filename', 'export_dir', 'blocksize',
'min_chunk_size', 'max_create_gap', 'max_sort_gap',
'db_manager')
def __init__(self):
self.input_filenames = None
self.db_filename = 'dvr-recover.sqlite'
self.export_dir = None
self.blocksize = None
self.min_chunk_size = None
self.max_create_gap = None
self.max_sort_gap = None
self.db_manager = SqlManager()
def load_settings(self):
'''Load all settings and set class attributes'''
self.input_filenames = self.db_manager.setting_query('input_filenames')
self.export_dir = self.db_manager.setting_query('export_dir')
self.blocksize = self.db_manager.setting_query('blocksize')
self.min_chunk_size = self.db_manager.setting_query('min_chunk_size')
self.max_create_gap = self.db_manager.setting_query('max_create_gap')
self.max_sort_gap = self.db_manager.setting_query('max_sort_gap')
if self.input_filenames is not None:
self.input_filenames = str(self.input_filenames).split('\0')
else:
self.input_filenames = []
if self.blocksize is None:
self.blocksize = 2048
if self.min_chunk_size is None:
self.min_chunk_size = 25600 # 50 MiB
if self.max_create_gap is None:
self.max_create_gap = 90000 # 1 second
if self.max_sort_gap is None:
self.max_sort_gap = 90000 # 1 second
def usage(self):
'''Print usage message'''
print __doc__
def setup(self):
args = sys.argv[2:]
if len(args) == 0:
args.append('show')
if (args[0] == 'input') and (len(args) > 1):
args[0:2] = (args[0] + ' '+ args[1],)
parameters = {
'show': 0,
'reset': 0,
'input add': 1,
'input del': 1,
'input clear': 0,
'blocksize': 1,
'min_chunk_size': 1,
'max_create_gap': 1,
'max_sort_gap': 1,
'export_dir': 1,
}
if args[0] not in parameters:
print 'Unknown argument: %s' % args[0]
return
if len(args) - 1 != parameters[args[0]]:
print ('Invalid argument count -- parameter "%s" '
'expects %i argument(s).') % (args[0], parameters[args[0]])
return
if args[0] in ('blocksize', 'min_chunk_size', 'max_create_gap',
'max_sort_gap'):
self.db_manager.setting_insert(args[0], int(args[1]))
elif args[0] in ('export_dir'):
self.db_manager.setting_insert(args[0], args[1])
elif args[0] in 'input clear':
self.db_manager.setting_insert('input_filenames', None)
elif args[0] in ('input add', 'input del'):
if args[0] == 'input add':
self.input_filenames.append(args[1])
else:
self.input_filenames.remove(args[1])
if len(self.input_filenames) > 0:
binary = buffer('\0'.join(self.input_filenames))
else:
binary = None
self.db_manager.setting_insert('input_filenames', binary)
elif args[0] == 'show':
for filename in self.input_filenames:
print 'input_file:', filename
if len(self.input_filenames) == 0:
print 'No input files specified!'
print 'export_dir:', self.export_dir