feat: compress video before upload
This commit is contained in:
parent
088e6f05ec
commit
0100d8cbab
|
@ -0,0 +1,33 @@
|
||||||
|
//
|
||||||
|
// AttachmentViewModel+Compress.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by MainasuK on 2022/11/11.
|
||||||
|
//
|
||||||
|
|
||||||
|
import UIKit
|
||||||
|
import AVKit
|
||||||
|
|
||||||
|
extension AttachmentViewModel {
|
||||||
|
func comporessVideo(url: URL) async throws -> URL {
|
||||||
|
let task = Task { () -> URL in
|
||||||
|
let urlAsset = AVURLAsset(url: url)
|
||||||
|
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) else {
|
||||||
|
throw AttachmentError.invalidAttachmentType
|
||||||
|
}
|
||||||
|
let outputURL = try FileManager.default.createTemporaryFileURL(
|
||||||
|
filename: UUID().uuidString,
|
||||||
|
pathExtension: url.pathExtension
|
||||||
|
)
|
||||||
|
exportSession.outputURL = outputURL
|
||||||
|
exportSession.outputFileType = AVFileType.mp4
|
||||||
|
exportSession.shouldOptimizeForNetworkUse = true
|
||||||
|
await exportSession.export()
|
||||||
|
return outputURL
|
||||||
|
}
|
||||||
|
|
||||||
|
self.compressVideoTask = task
|
||||||
|
|
||||||
|
return try await task.value
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,6 +43,8 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
|
||||||
@Published var caption = ""
|
@Published var caption = ""
|
||||||
@Published var sizeLimit = SizeLimit()
|
@Published var sizeLimit = SizeLimit()
|
||||||
|
|
||||||
|
var compressVideoTask: Task<URL, Error>?
|
||||||
|
|
||||||
// output
|
// output
|
||||||
@Published public private(set) var output: Output?
|
@Published public private(set) var output: Output?
|
||||||
@Published public private(set) var thumbnail: UIImage? // original size image thumbnail
|
@Published public private(set) var thumbnail: UIImage? // original size image thumbnail
|
||||||
|
@ -120,9 +122,20 @@ final public class AttachmentViewModel: NSObject, ObservableObject, Identifiable
|
||||||
defer {
|
defer {
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
do {
|
do {
|
||||||
let output = try await load(input: input)
|
var output = try await load(input: input)
|
||||||
self.output = output
|
|
||||||
|
switch output {
|
||||||
|
case .video(let fileURL, let mimeType):
|
||||||
|
let compressedFileURL = try await comporessVideo(url: fileURL)
|
||||||
|
output = .video(compressedFileURL, mimeType: mimeType)
|
||||||
|
try? FileManager.default.removeItem(at: fileURL) // remove old file
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
self.outputSizeInByte = output.asAttachment.sizeInByte.flatMap { Int64($0) } ?? 0
|
self.outputSizeInByte = output.asAttachment.sizeInByte.flatMap { Int64($0) } ?? 0
|
||||||
|
self.output = output
|
||||||
|
|
||||||
self.update(uploadState: .ready)
|
self.update(uploadState: .ready)
|
||||||
self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState)
|
self.delegate?.attachmentViewModel(self, uploadStateValueDidChange: self.uploadState)
|
||||||
} catch {
|
} catch {
|
||||||
|
|
Loading…
Reference in New Issue