如何读取 Delphi 中视频文件的第一个和最后一个 64kb?
How to read first and last 64kb of a video file in Delphi?
我想使用字幕API。它需要视频文件第一个和最后一个 64kb 的 md5 散列。我知道如何做 md5 部分只是想知道我将如何获得 128kb 的数据。
这是 Java 中我无法在 Delphi 中实现的问题的解决方案。 How to read first and last 64kb of a video file in Java?
到目前为止我的 Delphi 代码:
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 then begin
Filestream.Read(Buffer, 1024 * 64);
Filestream.Seek(64, soFromEnd);
Filestream.Read(Buffer, 1024 * 64);
Result := Md5.HashStreamAsHex(Filestream);
end;
finally
Md5.Free;
Filestream.Free;
end;
end;
我没有得到官方 API.API url here 所说的准确的 md5 哈希值。我正在使用 Delphi XE8。
那个API使用的hash function
描述为:
Our hash is composed by taking the first and the last 64kb of the
video file, putting all together and generating a md5 of the resulting
data (128kb).
我可以看出您的代码中存在一些问题。您正在散列文件流,而不是 Buffer
数组。除了您通过随后从文件流中读取来覆盖该数组。而且您试图仅查找 64 个字节,并且超出了流的末尾(您需要使用负值从流的末尾开始查找)。尝试这样的事情:
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 then
raise ESubDBException.Create('File is smaller than the minimum required for ' +
'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);
finally
Digest.Free;
end;
finally
HashStream.Free;
end;
finally
FileStream.Free;
end;
end;
我想使用字幕API。它需要视频文件第一个和最后一个 64kb 的 md5 散列。我知道如何做 md5 部分只是想知道我将如何获得 128kb 的数据。
这是 Java 中我无法在 Delphi 中实现的问题的解决方案。 How to read first and last 64kb of a video file in Java?
到目前为止我的 Delphi 代码:
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 then begin
Filestream.Read(Buffer, 1024 * 64);
Filestream.Seek(64, soFromEnd);
Filestream.Read(Buffer, 1024 * 64);
Result := Md5.HashStreamAsHex(Filestream);
end;
finally
Md5.Free;
Filestream.Free;
end;
end;
我没有得到官方 API.API url here 所说的准确的 md5 哈希值。我正在使用 Delphi XE8。
那个API使用的hash function
描述为:
Our hash is composed by taking the first and the last 64kb of the video file, putting all together and generating a md5 of the resulting data (128kb).
我可以看出您的代码中存在一些问题。您正在散列文件流,而不是 Buffer
数组。除了您通过随后从文件流中读取来覆盖该数组。而且您试图仅查找 64 个字节,并且超出了流的末尾(您需要使用负值从流的末尾开始查找)。尝试这样的事情:
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 then
raise ESubDBException.Create('File is smaller than the minimum required for ' +
'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);
finally
Digest.Free;
end;
finally
HashStream.Free;
end;
finally
FileStream.Free;
end;
end;