栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Swift-如何使用UIImagePickerController以MP4格式录制视频?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swift-如何使用UIImagePickerController以MP4格式录制视频?

您可以使用以下代码将录制的视频转换为MP4:

func enpreVideo(videoURL: NSURL)  {let avAsset = AVURLAsset(URL: videoURL, options: nil)var startDate = NSDate()//Create Export sessionexportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)// exportSession = AVAssetExportSession(asset: composition, presetName: mp4Quality)//Creating temp path to save the converted videolet documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .UserDomainMask, true)[0]let mydocumentPath = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent("temp.mp4").absoluteStringlet url = NSURL(fileURLWithPath: mydocumentPath)let documentsDirectory2 = NSFileManager.defaultManager().URLsForDirectory(.documentDirectory, inDomains: .UserDomainMask)[0] as NSURLlet filePath = documentsDirectory2.URLByAppendingPathComponent("rendered-Video.mp4")deleteFile(filePath)//Check if the file already exists then remove the previous fileif NSFileManager.defaultManager().fileExistsAtPath(mydocumentPath) {    do {        try NSFileManager.defaultManager().removeItemAtPath(mydocumentPath)    }    catch let error {        print(error)    }} urlexportSession!.outputURL = filePathexportSession!.outputFileType = AVFileTypeMPEG4exportSession!.shouldOptimizeForNetworkUse = truevar start = CMTimeMakeWithSeconds(0.0, 0)var range = CMTimeRangeMake(start, avAsset.duration)exportSession.timeRange = rangeexportSession!.exportAsynchronouslyWithCompletionHandler({() -> Void in    switch self.exportSession!.status {    case .Failed:        print("%@",self.exportSession?.error)    case .Cancelled:        print("Export canceled")    case .Completed:        //Video conversion finished        var endDate = NSDate()        var time = endDate.timeIntervalSinceDate(startDate)        print(time)        print("Successful!")        print(self.exportSession.outputURL)    default:        break    }})}func deleteFile(filePath:NSURL) {guard NSFileManager.defaultManager().fileExistsAtPath(filePath.path!) else {    return}do {    try NSFileManager.defaultManager().removeItemAtPath(filePath.path!)}catch{    fatalError("Unable to delete file: (error) : (__FUNCTION__).")}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636301.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号