I am creating a app in which i need to record videos and upload it to a server. Now my project has a android version too. To support android version i have to record the videos in mp4 format. I followed this tutorial to set the UIImagePicker media type to movie format imagePicker.mediaTypes = [kUTTypeMovie as String]
The UIImagePickerController
is perfect for my requirement and the only thing that i need to change is its saving format to mp4. I tried kUTTypeMPEG4
in mediaTypes
but it throws error at the run time with no error description.
This is my video Capture function
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
}
I am using Swift 2.2 and Xcode 8 with Use Legacy swift Language version = Yes
Any Alternative Solutions are also appreciated. Thanks in advance.
EDIT: I found out that there is no method to directly record videos in mp4 format in swift. only can be converted to required format from apple's quicktime mov format.
I made some modifications to the following 2 answers to make it compatible with Swift3:
https://stackoverflow.com/a/40354948/2470084
https://stackoverflow.com/a/39329155/2470084
A quick swift 4 update to the previous answers:
Here is some code that you can use to convert the recorded video into MP4:
Source: https://stackoverflow.com/a/39329155/4786204
Minor refactoring of previous examples:
Also:
AVAssetExportPresetHighestQuality
preset works when video is played on Android / Chrome.P.S. Be aware that the completion handler of
exportVideo
method might not be returned on the main thread.Run on iOS11, we will always received the nil value for the AVAssetExportSession. Do we have any solution for this case?