86 lines
2.5 KiB
Swift
86 lines
2.5 KiB
Swift
import Foundation
|
|
import Cocoa
|
|
import AVKit
|
|
import AVFoundation
|
|
import ZIPFoundation
|
|
|
|
func radians(_ x: CGFloat) -> CGFloat {
|
|
return .pi * x / 180.0
|
|
}
|
|
|
|
class TimelapseViewController: NSViewController {
|
|
var document: Document?
|
|
|
|
private let rootLayer = CALayer()
|
|
let player: AVQueuePlayer =
|
|
AVQueuePlayer()
|
|
var isPlaying = true
|
|
|
|
@IBAction func clickAction(_ sender: Any) {
|
|
if isPlaying {
|
|
player.pause()
|
|
isPlaying = false
|
|
} else {
|
|
player.play()
|
|
isPlaying = true
|
|
}
|
|
}
|
|
|
|
func addVideo(entryPath: String, directory: String) {
|
|
guard let archive = Archive(data: (document?.data)!, accessMode: Archive.AccessMode.read) else {
|
|
return
|
|
}
|
|
|
|
let fileName = NSUUID().uuidString + ".mp4"
|
|
|
|
// This returns a URL? even though it is an NSURL class method
|
|
let fullURL = NSURL.fileURL(withPathComponents: [directory, fileName])!
|
|
|
|
let _ = try? archive.extract(archive[entryPath]!, to: fullURL)
|
|
|
|
OperationQueue.main.addOperation {
|
|
self.player.insert(AVPlayerItem(url: fullURL), after: nil)
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear() {
|
|
super.viewDidAppear()
|
|
|
|
guard let archive = Archive(data: (document?.data)!, accessMode: Archive.AccessMode.read) else {
|
|
return
|
|
}
|
|
|
|
let directory = NSTemporaryDirectory()
|
|
|
|
let queue = OperationQueue()
|
|
for entry in archive.makeIterator() {
|
|
if entry.path.contains(VideoPath) {
|
|
queue.addOperation() {
|
|
self.addVideo(entryPath: entry.path, directory: directory)
|
|
}
|
|
}
|
|
}
|
|
|
|
let playerLayer = AVPlayerLayer.init(player: self.player)
|
|
playerLayer.videoGravity = .resizeAspect
|
|
playerLayer.autoresizingMask = [.layerHeightSizable, .layerWidthSizable]
|
|
playerLayer.frame = view.bounds
|
|
|
|
var rotationDegrees: CGFloat = 0
|
|
if document?.info.orientation == 3 {
|
|
rotationDegrees = 90
|
|
} else if document?.info.orientation == 4 {
|
|
rotationDegrees = -90
|
|
}
|
|
|
|
let affineTransform = CGAffineTransform(rotationAngle: radians(rotationDegrees))
|
|
playerLayer.setAffineTransform(affineTransform)
|
|
|
|
view.wantsLayer = true
|
|
view.layer = rootLayer
|
|
|
|
rootLayer.addSublayer(playerLayer)
|
|
|
|
player.play()
|
|
}
|
|
}
|