2016-11-01 64 views
8

我正在創建一個應用程序,我需要記錄視頻並將其上傳到服務器。現在我的項目也有一個android版本。爲了支持Android版本,我必須以mp4格式錄製視頻。我跟着this教程設置UIImagePicker媒體類型電影格式imagePicker.mediaTypes = [kUTTypeMovie as String]Swift - 如何使用UIImagePickerController以MP4格式錄製視頻?

UIImagePickerController非常適合我的要求,我需要改變是它保存格式爲MP4的唯一的事情。我在mediaTypes中試過kUTTypeMPEG4,但它在運行時拋出錯誤,沒有錯誤描述。

這是我的視頻拍攝功能

func startCameraFromViewController() { 

     if UIImagePickerController.isSourceTypeAvailable(.Camera) == false { 
      return 
     } 
     viewBlack.hidden = false 
     presentViewController(cameraController, animated: false, completion: nil) 

     cameraController.sourceType = .Camera 

     cameraController.mediaTypes = [kUTTypeMovie as String] 
     //cameraController.mediaTypes = [kUTTypeMPEG4 as String] 
     cameraController.cameraCaptureMode = .Video 
     cameraController.videoQuality = .TypeMedium 
     if(getPurchaseId() as! Int == 0) 
     { 
      if(txtBenchMark.text?.isEmpty == false) 
      { 
       cameraController.videoMaximumDuration = NSTimeInterval(300.0) 
      }else{ 
       cameraController.videoMaximumDuration = NSTimeInterval(60.0) 
      } 
     }else{ 
      cameraController.videoMaximumDuration = NSTimeInterval(600.0) 
     } 
     cameraController.allowsEditing = false 
    } 

我使用雨燕2.2和Xcode中8 Use Legacy swift Language version = Yes

任何替代方案也很受青睞。提前致謝。

編輯: 我發現,沒有辦法直接錄製mp4格式的視頻在swift中。只能從蘋果的quicktime mov格式轉換爲所需的格式。

回答

3

下面是一些代碼,你可以用它來錄製的視頻轉換成MP4:

func encodeVideo(videoURL: NSURL) { 
let avAsset = AVURLAsset(URL: videoURL, options: nil) 

var startDate = NSDate() 

//Create Export session 
exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) 

// exportSession = AVAssetExportSession(asset: composition, presetName: mp4Quality) 
//Creating temp path to save the converted video 


let documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 
let myDocumentPath = NSURL(fileURLWithPath: documentsDirectory).URLByAppendingPathComponent("temp.mp4").absoluteString 
let url = NSURL(fileURLWithPath: myDocumentPath) 

let documentsDirectory2 = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL 

let filePath = documentsDirectory2.URLByAppendingPathComponent("rendered-Video.mp4") 
deleteFile(filePath) 

//Check if the file already exists then remove the previous file 
if NSFileManager.defaultManager().fileExistsAtPath(myDocumentPath) { 
    do { 
     try NSFileManager.defaultManager().removeItemAtPath(myDocumentPath) 
    } 
    catch let error { 
     print(error) 
    } 
} 

url 

exportSession!.outputURL = filePath 
exportSession!.outputFileType = AVFileTypeMPEG4 
exportSession!.shouldOptimizeForNetworkUse = true 
var start = CMTimeMakeWithSeconds(0.0, 0) 
var range = CMTimeRangeMake(start, avAsset.duration) 
exportSession.timeRange = range 

exportSession!.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__).") 
} 
} 

來源:https://stackoverflow.com/a/39329155/4786204

+0

感謝您的代碼。我會嘗試。但你知道我可以直接以mp​​4格式從相機錄製視頻嗎? –

+0

我不相信用戶界面圖像選擇器是可能的。 –

+0

感謝您的幫助朋友。我會嘗試上面的代碼。 –

10

我做了一些修改,下面的2個答案,使其與Swift3兼容
https://stackoverflow.com/a/40354948/2470084
https://stackoverflow.com/a/39329155/2470084

import AVFoundation 

func encodeVideo(videoURL: URL){ 
    let avAsset = AVURLAsset(url: videoURL) 
    let startDate = Date() 
    let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) 

    let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] 
    let myDocPath = NSURL(fileURLWithPath: docDir).appendingPathComponent("temp.mp4")?.absoluteString 

    let docDir2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL 

    let filePath = docDir2.appendingPathComponent("rendered-Video.mp4") 
    deleteFile(filePath!) 

    if FileManager.default.fileExists(atPath: myDocPath!){ 
     do{ 
      try FileManager.default.removeItem(atPath: myDocPath!) 
     }catch let error{ 
      print(error) 
     } 
    } 

    exportSession?.outputURL = filePath 
    exportSession?.outputFileType = AVFileTypeMPEG4 
    exportSession?.shouldOptimizeForNetworkUse = true 

    let start = CMTimeMakeWithSeconds(0.0, 0) 
    let range = CMTimeRange(start: start, duration: avAsset.duration) 
    exportSession?.timeRange = range 

    exportSession!.exportAsynchronously{() -> Void in 
     switch exportSession!.status{ 
     case .failed: 
      print("\(exportSession!.error!)") 
     case .cancelled: 
      print("Export cancelled") 
     case .completed: 
      let endDate = Date() 
      let time = endDate.timeIntervalSince(startDate) 
      print(time) 
      print("Successful") 
      print(exportSession?.outputURL ?? "") 
     default: 
      break 
     } 

    } 
} 

func deleteFile(_ filePath:URL) { 
    guard FileManager.default.fileExists(atPath: filePath.path) else{ 
     return 
    } 
    do { 
     try FileManager.default.removeItem(atPath: filePath.path) 
    }catch{ 
     fatalError("Unable to delete file: \(error) : \(#function).") 
    } 
} 
+0

Thanks Mate很高興有一個Swift 3版本 –

0

在iOS11上運行,我們將始終收到AVAssetExportSession的零值。我們有這個案子的解決方案嗎?

if let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) { 
    //work on iOS 9 and 10 
} else { 
    //always on iOS 11 
} 
相關問題