gpt4 book ai didi

delphi - 如何在Delphi中读取视频文件的第一个和最后一个64kb?

转载 作者:行者123 更新时间:2023-12-03 15:23:20 26 4
gpt4 key购买 nike

我想使用字幕 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 所说的准确的 md5 哈希值。 API url here 。我使用的是Delphi XE8。

最佳答案

hash function该 API 使用的描述如下:

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;

关于delphi - 如何在Delphi中读取视频文件的第一个和最后一个64kb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com