美文网首页
Swift mov转Mp4

Swift mov转Mp4

作者: ZM_微笑向阳 | 来源:发表于2022-06-24 11:41 被阅读0次

先倒入第三方的库

  import AVKit
  import MobileCoreServices

Mov 转 Mp4

   //MARK:视频 转 换
  extension StatisticalPanelWebViewController:UIImagePickerControllerDelegate,UINavigationControllerDelegate{

func openVideo() {
    
    let photoPicker =  UIImagePickerController()
    photoPicker.delegate = self
    photoPicker.allowsEditing = true
    photoPicker.sourceType = .photoLibrary
    //只显示视频类型的文件
    photoPicker.mediaTypes = [kUTTypeMovie as String]

    //在需要的地方present出来
    self.present(photoPicker, animated: true, completion: nil)
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! URL
    let pathString = videoURL.relativePath
    print("视频地址:\(pathString)")
    picker.dismiss(animated: true, completion: {})
    
    movToMp4Url(videoURL)
}

//视频 格式 转换
func movToMp4Url(_ outputFileURL:URL) {
    
    let avAsset = AVURLAsset.init(url: outputFileURL, options: nil)
    let _:NSArray = avAsset.tracks(withMediaType: AVMediaType.video) as NSArray
    let destinationPath = NSTemporaryDirectory() + "fileName.mp4"
    let newVideoPath: NSURL = NSURL(fileURLWithPath: destinationPath as String)
    let exporter = AVAssetExportSession(asset: avAsset,
                                                   presetName:AVAssetExportPresetHighestQuality)!
    exporter.outputURL = newVideoPath as URL
    exporter.outputFileType = AVFileType.mp4
    exporter.shouldOptimizeForNetworkUse = true
    exporter.exportAsynchronously(completionHandler: {
        print("NewPath:\(String(describing: newVideoPath))")
    })
}

}

相关文章

网友评论

      本文标题:Swift mov转Mp4

      本文链接:https://www.haomeiwen.com/subject/fcbvvrtx.html