Loading...
 
Sublight
searching subtitles has never been this easy
Follow Us on Facebook Follow Us on Twitter Freelance development (ASP.NET, Windows Forms, WPF, Silverlight)

How to calculate Sublight video hash?

Here is simple C# algorithm (used by Sublight client application):

   1:  /// <summary>
   2:  /// C# algorithm which computes hash of video file. Computed hash is then used to find matching subtitles.
   3:  /// </summary>
   4:  /// <param name="filePath">Absolute path to video file.</param>
   5:  /// <returns>Computed hash value formated as hexadecimal string (52 characters). Returns null on failure.</returns>
   6:  public static string Compute(string filePath)
   7:  {
   8:      try
   9:      {
  10:          if (!System.IO.File.Exists(filePath))
  11:          {
  12:              return null;
  13:          }
  14:   
  15:          List<byte> hash = new List<byte>(26);
  16:   
  17:          //0: reserved
  18:          hash.Insert(0, 0x0);
  19:   
  20:          //1-2: video length in seconds
  21:          short runTime = new VideoInfo(filePath).RunTime;
  22:          if (BitConverter.IsLittleEndian)
  23:          {
  24:              hash.InsertRange(1, Invert(BitConverter.GetBytes(runTime)));
  25:          }
  26:          else
  27:          {
  28:              hash.InsertRange(1, BitConverter.GetBytes(runTime));
  29:          }
  30:   
  31:          //3-8: file length in bytes
  32:          long fileLength = new FileInfo(filePath).Length;
  33:          if (BitConverter.IsLittleEndian)
  34:          {
  35:              hash.InsertRange(3, GetLastBytes(Invert(BitConverter.GetBytes(fileLength)), 6));
  36:          }
  37:          else
  38:          {
  39:              hash.InsertRange(3, GetLastBytes(BitConverter.GetBytes(fileLength), 6));
  40:          }
  41:   
  42:          //9-24: MD5 hash for first 5 MB of file
  43:          hash.InsertRange(9, ComputeMD5FromFile(filePath, 5 * 1024 * 1024));
  44:   
  45:          //25: control byte
  46:          int sum = 0;
  47:          for (int i = 0; i < 25; i++)
  48:          {
  49:              sum += hash[i];
  50:          }
  51:          hash.Insert(25, Convert.ToByte(sum % 256));
  52:   
  53:          //convert to hex string
  54:          StringBuilder sbResult = new StringBuilder();
  55:          for (int i = 0; i < hash.Count; i++)
  56:          {
  57:              sbResult.AppendFormat("{0:x2}", hash[i]);
  58:          }
  59:          return sbResult.ToString();
  60:   
  61:      }
  62:      catch
  63:      {
  64:          return null;
  65:      }
  66:  }

Hash example: 000f9500002bbcc00018d5d8c1747b005aaa2800a18e81de9711

Description:

00 .................................. reserved (1 B)
0f95 ................................ video duration: 3989 seconds (2 B)
00002bbcc000 ........................ video length: 733.790.208 B (6 B)
18d5d8c1747b005aaa2800a18e81de97 .... MD5 hash (16 B)
11 .................................. control byte (1 B)

If you have any questions, please contact us.

Comment Icon Comments are disabled
Share this article:

Free Downloads

Download Sublight 3.1.0
Downloads: 952,236
SublightCmd 2.0.0
hosted on CodePlex
Ads