新公司最近在在我寫美顏相機
就是把自己用演算法變成美女帥哥
大家都長得很像外星人
不用化妝就可以騙得了大家
但網頁端想要我回傳mp4
大家都知道iPhone存得是MOV檔
今天就來研究轉檔
首先先引入AVFoundation
import AVFoundation
然後取個名字
let date = Date()
let fileName = "testVideo.mp4"
將他儲存於本地端
let docPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let videoSandBoxPath = (docPath as String) + "/ablumVideo" + fileName
轉碼用
let avAsset = AVURLAsset.init(url: sourceUrl, options: nil)
並且取得影片時間
let time = avAsset.duration
let number = Float(CMTimeGetSeconds(time)) - Float(Int(CMTimeGetSeconds(time)))
let totalSecond = number > 0.5 ? Int(CMTimeGetSeconds(time)) + 1 : Int(CMTimeGetSeconds(time))
let photoId = String(totalSecond)
轉檔用
let exportSession = AVAssetExportSession.init(asset: avAsset, presetName: AVAssetExportPresetMediumQuality)
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputURL = URL.init(fileURLWithPath: videoSandBoxPath)
exportSession?.outputFileType = AVFileType.mp4 //AVFileTypeMPEG4
exportSession?.exportAsynchronously(completionHandler: {
if exportSession?.status == AVAssetExportSessionStatus.failed {
print("Fail")
}
if exportSession?.status == AVAssetExportSessionStatus.completed {
print("Success")
let dataurl = URL.init(fileURLWithPath: videoSandBoxPath)
let image = getVideoCropPicture(videoUrl: sourceUrl)
}
})
大約是這項練習