Skip to content

Instantly share code, notes, and snippets.

@Instagraeme
Last active July 23, 2023 16:25
Show Gist options
  • Save Instagraeme/19557ece2db0e1cdb74ec5d8e3bb1618 to your computer and use it in GitHub Desktop.
Save Instagraeme/19557ece2db0e1cdb74ec5d8e3bb1618 to your computer and use it in GitHub Desktop.
Python Script to retrieve JPEG images from Hikvision PIC file format
#!/usr/bin/env python3
"""
Copyright (c) 2017 Graeme Smith
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
--------------------------------------------------------------------------------
The purpose of this script is to retreive the JPEG images embedded in a PIC
file created by a Hikvision CCTV.
Usage:
Required flags:
-i or --input filename(s) : list of PIC files to be decompressed
-d or --directory directory : directory to output the files, if the
directory doesn't exist it will be
created
Optional flags:
-p or --prefix name : filenames will start with this prefix
and end in '-nnn.jpg' where nnn is the
number of the image in the sequence.
Defaults to 'picture'
-e or --every n : only output every nth image. Defaults
to 1
If the script detects existing image files with the same prefix it will try to
continue the sequence.
"""
import argparse
import binascii
import mmap
import re
from pathlib import Path
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Decompress Hikvision PIC Files')
optional = parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
required.add_argument('-i', '--input', metavar="Filenames", nargs='+', required=True)
required.add_argument('-d', '--directory', metavar="Output Directory", required=True)
optional.add_argument('-p', '--prefix', metavar="Prefix", help="Filename Prefix (Default: Picture)", required=False, default="Picture")
optional.add_argument('-e', '--every', metavar="Number", help="Output every nth picture (Default: 1)", required=False, type=int, default=1)
parser._action_groups.append(optional) # added this line
args = parser.parse_args()
print ("\nHikvision PIC File Decompressor")
print ("-------------------------------")
count = 1
picture = 0
output_dir = Path(args.directory)
output_dir.mkdir(parents=True, exist_ok=True)
print("\nWriting files to: " + str(output_dir.absolute()))
filename = args.prefix + "-" + str(count) + ".jpg"
file_output = output_dir.joinpath(Path(filename).name)
if file_output.exists():
filelist = output_dir.glob(args.prefix + "-*" + ".jpg")
for filename in filelist:
number = int(str(filename.name)[len(args.prefix)+1:-4])
if number > count:
count = number
count += 1
print("\nExisting Files Found - Starting at number:" + str(count))
for file_input in args.input:
if Path(file_input).is_file():
print ("\nDecompressing: %s" % file_input)
else:
print ("\nError: %s not found" % file_input)
start = 0
end = 0
with open(file_input, 'rb') as f:
f.seek(0, 2)
num_bytes = f.tell()
print ("File Size: %.2f MB" % float(num_bytes / 1024 / 1024))
print()
i = 0
status = "No files written"
t = tqdm(total=num_bytes, unit='B', unit_scale=True, unit_divisor=1024, desc="Progress")
mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
while 1:
f.seek(end)
t.set_description(status)
start = mm.find(b'\xFF\xD8', end)
if start == -1:
break
end = mm.find(b'\xFF\xD9', start)
if (picture % args.every == 0):
filename = args.prefix + "-" + str(count) + ".jpg"
filename_output = output_dir.joinpath(Path(filename).name)
file_output = open(filename_output, 'wb')
f.seek(start)
jpeg_data = f.read(end - start)
file_output.write(jpeg_data)
file_output.close()
status = "File " + filename + " written"
count += 1
picture += 1
t.update(end - start)
t.update(num_bytes - t.n)
t.close()
f.closed
@GGeog
Copy link

GGeog commented May 22, 2018

Doesn't work for Win7(x64).
Any help?

@RomanJos
Copy link

RomanJos commented Aug 7, 2018

i don't know why but for the first .pic it stop at 130M and the last it go to 100% but i still can't open the .jpg files it says its corrupted or too big idk do you have an idea ? and also i have change post=mmap.POST_READ) to access=mmap.ACCESS_READ)
image

@matthewgream
Copy link

matthewgream commented Jul 22, 2023

You need 'end = mm.find(b'\xFF\xD9', start) + 2' so the jpeg end of file marker is written out, otherwise you'll get annoying messages from tools re. truncated jpeg files.

UPDATED: if you have the SD card you can extract the events from the eventdb on the card and work from that, including the actual timestamps. here's some code that works for me but you need to modify to work for you. Nodejs on debian.


const fs = require ('fs');
const path = require ('path');

// data.txt
// sqlite3 ./event_db_index00 "select trigger_time,file_no,info_start_offset,info_len from TimingCaptureTB" > data.txt

const records = fs.readFileSync ('data.txt', 'utf-8').split ('\n').map (line => {
        const [timestamp, filenumber, offset, header] = line.split ('|').map (Number);
        return { timestamp, filenumber, offset, header };
});
for (let i = 0; i < records.length - 1; i++)
        records [i].length = (records [i].filenumber == records [i+1].filenumber) ? records [i+1].offset - records [i].offset : -1;

for (let i = 0; i < records.length - 1; i++) {
        const record = records [i];

        const filename = path.join ('/mnt/raw', 'hiv' + String (record.filenumber).padStart (5, '0') + '.pic');
        const timestamp = (new Date (record.timestamp * 1000)).toISOString ().replace (/[:\-T]/g, '').substring (0, 14);

        console.log ([i, records.length, record.filenumber, record.offset, record.length, timestamp]);

        const fileData = fs.readFileSync (filename);
        const jpegData = fileData.slice (record.offset + record.header, record.length > 0 ? (record.offset + record.length) : fileData.length);

        if (jpegData.readUInt16BE (0) === 0xFFD8 && jpegData.readUInt16BE (jpegData.length - 2) === 0xFFD9)
                console.log (' --> ' + timestamp + '.jpg') + fs.writeFileSync (path.join ('/mnt/hgfs/VMWARE_SHARED/pics_two', timestamp + '.jpg'), jpegData);
        else
                console.log ('NOT JPEG');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment