2

Am using below code for decrypting the VideoFile.mp4(CTR/Nopadding encrypted file) and playing it in Avplayer. This code works for small files. But its crashing for big video files due to memory issues.

How to solve this issue. Please help

guard let content = Bundle.main.url(forResource: "videoFile", withExtension: "mp4") else{
                return
            }
 
  let vidoeData = NSData(contentsOf: content)!
            
   let vidoeDatadencrypted = vidoeData.decryptData(vidoeData as Data, withKey: aeskey.data(using: .utf8))
    
    let tmpFileURL1 = URL(fileURLWithPath:NSTemporaryDirectory()).appendingPathComponent("video").appendingPathExtension("mp4")
            let wasFileWritten1 = (try? vidoeDatadencrypted!.write(to: tmpFileURL1, options: [.atomic])) != nil
    
   if wasFileWritten1{
    let player = AVPlayer(url: tmpFileURL1)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
   }

update:

Able to achive this for smaller files using the above shared code.

how to load avplayer with NSStream?

Angel
  • 112
  • 15

1 Answers1

0

you need to open your file using InputStream, pass the data chunk to CommonCrypto lib, write the output chunk to the OutputStream and repeat until file ends.

I haven't seen any guide to do so, but as a reference I've found a couple of examples for you: first, second, third.

Related questions for you to check out: first, second

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • could you please share code sample - to decrypt a very large file without crashing memory. – Angel Apr 08 '21 at 07:51
  • It can't be done in a couple lines. I don't have such code, and writing it will take me at least 3-4 hours, but examples I've mentioned in the answer should be enough to implement it – Phil Dukhov Apr 11 '21 at 05:41