bkbilly / libHikvision

Extract videos/photos from HikVision camera datadirs
https://pypi.org/project/libhikvision/
MIT License
14 stars 2 forks source link

SQLLite #3

Closed surfzoid closed 12 months ago

surfzoid commented 2 years ago

hi, same way as your PHP ui, i'm in the case of record_index and event_index rather bin file and don't see this part ln the lib.

also, befor continue to debug, is the lib display the green, blue or red vector (event, motion area) during playback?

thank

bkbilly commented 2 years ago

I haven't seen a data structure on my installation that have the files record_index and event_index. These are the data structure that my app is using.

It looks like your data is completely different than mine, so I won't be able to support yours. You might want to look into updating the firmware of your cameras. Could you send me the model of your camera to check it?

surfzoid commented 2 years ago

Well in fact you see it here : https://github.com/bkbilly/Hikvision-Site/issues/3

surfzoid commented 2 years ago

My cam have same bin file, but the python script haven't SQLite support like in PHP. From now I have also an hik NVR with another binary format to support multiple cam in same record dit.

bkbilly commented 2 years ago

Oh! you are right! Thanks for pointing it out for me, I forgot about this. I will take a look on it to add support for SQLite on python. I will keep you updated once I fix it.

bkbilly commented 2 years ago

Just to make sure, could you check that this is your data structure?

.
├── datadir0
│        ├── event_db_index00
│        ├── event_db_index01
│        ├── HIKWS
│        ├── logCurFile.bin
│        ├── logMainFile.bin
│        ├── record_db_index00
│        └── record_db_index01
.
.
.
.
└── info.bin
surfzoid commented 2 years ago

You will find cam1 dir who is the cam and cam2 who is the NVR :

ls -laR /media/pi/HddFbx/cams/ | nc termbin.com 9999 https://termbin.com/hlk6

bkbilly commented 2 years ago

I just remembered that I didn't answer to your original question. It's not possible for me to get the vectors from the camera on playback.

bkbilly commented 2 years ago

I've updated the code with the new data structure on the release 0.3.9. You can update it with pip or download it from my repository. Let me know if this works for you.

surfzoid commented 2 years ago

I just remembered that I didn't answer to your original question. It's not possible for me to get the vectors from the camera on playback.

Hi, your Right about camera read, but, vector are also embedded in the video file metadata, VLC seen them as subtitles, vs player (Linux 32) from hik read them, I don't know if phyton lib SDK have the function, but, there is an full hik SDK and also player SDK, I will get a look during the day and give you feedback. Thanks

surfzoid commented 2 years ago

I remember, in the web admin of Cam or NVR, there is an option"vca" who mean embedded IA info in the stream, but also without it, vs player display vector

surfzoid commented 2 years ago

hi, i'm linux user but used MS code to debug, my scipt sample is https://termbin.com/2eex with or without date, script return nothing, getsegments.len return 0 i'm not python expert sql table "segment" is empty, in event db file there is data for intrusion event, perhap's this because my cam is acusense, whit IA? woul dyou like my bin files ?

bkbilly commented 2 years ago

I haven't used the VCA option, so I am not sure how I can get this info. As for your script, you could run it like this to check the response:

from libhikvision import libHikvision
cameradir = '/media/pi/HddFbx/cams/cam1'
hik = libhikvision(cameradir, 'video')
print(hik.getSegments())

If you still get nothing, then send me these two files:

  1. info.bin
  2. record_db_index00
surfzoid commented 2 years ago

Hi if am i correct, i see two modifications you want: first : from libhikvision import libHikvision If i change this kind of import, it is because i got empty response and want to go into your code with Ms Code debugger, i saw segments.len = 0. Second is add print(hik.getSegments()) , this is the only you want ?

so the result is : [] the function NASInfo return : {'serialNumber': b'DS-2CD2746G2-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'MACAddr': b'xxxxxxxxxxx', 'byRes': 244, 'f_bsize': 4096, 'f_blocks': 7176681, 'DataDirs': 1}

surfzoid commented 2 years ago

Here it is files. I added an video sample, this one is extracted with the web ui of the cam, if you open it with VLC you can see there is subtitle hard coded in the the file, i believe, this the Metadata from VCA or Onvif whose display red, green and blue vector. hikpylib.zip

surfzoid commented 2 years ago

At least, i take the opportunity of this thread to give you the NVR files, whose are in Bin format with différent structure to support multiple cam and multiple disk. NVR.zip

Thank you for your wonderful job and idea.

surfzoid commented 2 years ago

hi i come back to py :-D from now sql request is ok, i think i had mixed in my mind the 2 cam i have. the segment array have some missing index, i solved like that:

            for fileNum, startOffset, endOffset, startTime, endTime in cur.execute(statement):
                segment = {}
                segment['cust_fileNum'] = fileNum
                segment['startOffset'] = startOffset
                segment['endOffset'] = endOffset
                segment['cust_indexFileNum'] = indexFileNum
                segment['cust_startTime'] = datetime.utcfromtimestamp(startTime)
                segment['cust_endTime'] = datetime.utcfromtimestamp(endTime)
                segment['duration'] = endTime - startTime
                segment['cust_duration'] = endTime - startTime
                fileExtension = 'mp4'
                if 'p.bin' in self.indexFile:
                    fileExtension = 'pic'
                segment['cust_filePath'] = '{0}datadir{1}/hiv{2:05d}.{3}'.format(self.cameradir, segment['cust_indexFileNum'], segment['cust_fileNum'], fileExtension)
                self.segments.append(segment)

for the NVR it is amazing, some mp4 files are SQL , i can open them with db browser and see sql data :

for f in ./*; do file $f; done
./HIKDISK00.bin: data
./HIKDISK01.bin: data
./hiv00000.mp4: data
./hiv00001.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00002.mp4: data
./hiv00003.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00004.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00005.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00006.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00007.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00008.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00009.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00010.mp4: SQLite 3.x database, last written using SQLite version 3025002
./hiv00011.mp4: data
./hiv00012.mp4: data
./hiv00013.mp4: data
./hiv00014.mp4: MPEG sequence, v2, program multiplex
./hiv00015.mp4: data
./hiv00016.mp4: data
./hiv00017.mp4: MPEG sequence, v2, program multiplex
./hiv00018.mp4: MPEG sequence, v2, program multiplex
./hiv00019.mp4: data
./hiv00020.mp4: MPEG sequence, v2, program multiplex
./hiv00021.mp4: MPEG sequence, v2, program multiplex
./hiv00022.mp4: MPEG sequence, v2, program multiplex
./hiv00023.mp4: MPEG sequence, v2, program multiplex
./hiv00024.mp4: data
./hiv00025.mp4: data
./hiv00026.mp4: MPEG sequence, v2, program multiplex
./INDEX00.bin: firmware 20 v0 (revision 0) \257\005  (region 2097152), 0 bytes or less, UNKNOWN1 0x200000, at 0x0 0 bytes , at 0x0 0 bytes 
./INDEX01.bin: firmware 20 v0 (revision 0) \262\005  (region 2097152), 0 bytes or less, UNKNOWN1 0x200000, at 0x0 0 bytes , at 0x0 0 bytes 
./INDEX10.bin: data
./INDEX11.bin: data
./logCurFile.bin: data
./logMainFile.bin: data

last bin are firmware !!

for the drawing of vector motions, the rules, it should be nice to use the code from here : https://github.com/iafilatov/hikstract

bkbilly commented 2 years ago

Hi again. I am happy that you got it working and thanks for pointing the hikstract. I didn't know about that, I might take a look at it in the future.

surfzoid commented 2 years ago

hi thank you for reformatting my post. do you believe it is possible to write a class to parse NVR recorded files (don't forget zip is available one month), when looking inside i don't see offset but segments number and len colums

surfzoid commented 2 years ago

again for NVR, very interesting doc :+1: https://www.researchgate.net/publication/285429692_Analysis_of_the_HIKVISION_DVR_file_system

surfzoid commented 2 years ago

Hi back After spend amazing time because Hik use different and incompatible format i decided to use ISAPI on local network and then use less web trafic to backup videos : https://github.com/surfzoid/HikNetExtractor

For my second question, playing video, i used Hik SDK player : https://github.com/surfzoid/QtVsPlayer

Are you interested to contribute?