0

I want to use a subtitle API. It requires a md5 hash of first and last 64kb of the video file. I know how to do the md5 part just want to know how will I achieve to get the 128kb of data.

Here is the solution to the problem in Java which I am unable to implement in Swift. Stack

I have a video URL, How would I get the first and last 64kb from it? Get on AlamoFire then what?

below is how it's done in Java,

FileInputStream in = new FileInputStream("d:/1.avi");
byte[] a = new byte[64 * 1024];
in.read(a);   //head
long p = in.getChannel().size() - 64 * 1024;
in.getChannel().position(p);
in.read(a);   //tail
Community
  • 1
  • 1
alemac852
  • 99
  • 1
  • 11

1 Answers1

1

Here is how to do it correctly:

let data = try! Data(contentsOf: URL(string: <#Insert your URL#>)!) // should do some unwrapping precautions here

// first 64 bytes
let first = data.subdata(in: 0 ..< 65336) // 65336 bytes = 1kb (if 1kb = 1024 bytes)

// last 64 bytes
let last = data.subdata(in: (data.count - 65336)..<data.count) // data.count - 65366 = last 64 bytes of the file

So first you download the file (eg with Alamofire). Once that is done, place the URL into the string: parameter of the URL initialiser.

Then, use the variables first and last to get the md5.

Papershine
  • 4,995
  • 2
  • 24
  • 48
  • thanks! very helpful! Why do I need to download the file "http://thesubdb.com/api/samples/dexter.mp4" with Alamofire when there is this "Data(contentsOf: URL(string: <#Insert your URL#>)!)" and I could just put that url string in there. – alemac852 May 04 '17 at 05:27
  • Well actually if you just put the link there you will have to do some threading, because your app will freeze while it downloads the file. It will get killed by iOS if it hangs for a long time. – Papershine May 04 '17 at 05:49
  • ahh yes, I'm having this problem right now. since I am putting 2hr video urls into the "Data(contentsOf: URL(string: <#Insert your URL#>)!)" Humm is there any way to just load the first and last bits of the video? then get the first and last bits of that data (64 bytes). then find the hash code, (saving the device to loading the entire video just for a subtitle api get request?)trying to think of the best approach. – alemac852 May 04 '17 at 06:06
  • using alamo fire meow – alemac852 May 04 '17 at 06:26
  • Shouldn't 65336 - be 64000? or is it because the api said 64 * 1024 – alemac852 May 04 '17 at 06:40
  • The api states 64*1024, so just use 64*1024 – Papershine May 04 '17 at 08:32
  • And I don't think that you can only download parts of the video – Papershine May 04 '17 at 08:33