How to read first and last 64kb of a video file in Delphi? -
i want use subtitle api. requires md5 hash of first , last 64kb of video file. know how md5 part want know how achieve 128kb of data.
here solution problem in java unable implement in delphi. how read first , last 64kb of video file in java?
my delphi code far:
function tsubdbapi.gethashfromfile(const afilename: string): string; var md5: tidhashmessagedigest5; filestream: tfilestream; buffer: tbytearray; begin md5 := tidhashmessagedigest5.create; filestream := tfilestream.create(afilename, fmopenread, fmsharedenywrite); try if filestream.size > 0 begin filestream.read(buffer, 1024 * 64); filestream.seek(64, sofromend); filestream.read(buffer, 1024 * 64); result := md5.hashstreamashex(filestream); end; md5.free; filestream.free; end; end;
i not getting accurate md5 hash stated official api.api url here. using delphi xe8.
the hash function
used api described as:
our hash composed taking first , last 64kb of video file, putting , generating md5 of resulting data (128kb).
i can see few problems in code. hashing file stream, not buffer
array. except overwriting array subsequent reading file stream. , trying seek 64 bytes, , beyond end of stream (you need use negative value seek end of stream). try instead:
type esubdbexception = class(exception); function tsubdbapi.gethashfromfile(const afilename: string): string; const kilobyte = 1024; datasize = 64 * kilobyte; var digest: tidhashmessagedigest5; filestream: tfilestream; hashstream: tmemorystream; begin filestream := tfilestream.create(afilename, fmopenread, fmsharedenywrite); try if filestream.size < datasize raise esubdbexception.create('file smaller minimum required ' + 'calculating api hash.'); hashstream := tmemorystream.create; try hashstream.copyfrom(filestream, datasize); filestream.seek(-datasize, soend); hashstream.copyfrom(filestream, datasize); digest := tidhashmessagedigest5.create; try hashstream.position := 0; result := digest.hashstreamashex(hashstream); digest.free; end; hashstream.free; end; filestream.free; end; end;
Comments
Post a Comment