加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 09:21:17 所属栏目:大数据 来源:网络整理
导读:我想使用字幕API.它需要md5哈希的第一个和最后64kb的视频文件.我知道如何做md5部分只是想知道如何获得128kb的数据. 以下是Java中我无法在Delphi中实现的问题的解决方案. How to read first and last 64kb of a video file in Java? 到目前为止我的Delphi代码
我想使用字幕API.它需要md5哈希的第一个和最后64kb的视频文件.我知道如何做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).

我可以在你的代码中看到一些问题.您正在散列文件流,而不是您的缓冲区数组.除非您通过后续读取文件流覆盖该数组.并且您试图仅搜索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,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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读