Check on the minimum duration of the video on iOS

Archana Kumari
3 min readApr 3, 2023
Videos and iOS

At present, there is no check in the UIImagePicker for the minimum duration of the video we can apply in swift, as we have for the maximum duration of the video.

How to present Camera to capture Video in swift?

Here is the most simplest code with which you can simply present camera to capture video in your app.

import UIKit
import Foundation
import AVFoundation
import PhotosUI
import MobileCoreServices

func presentCamera() {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.allowsEditing = true
imagePicker.videoMaximumDuration = 30 // in seconds
self.present(imagePicker, animated: true, completion: nil)
} else {
let alert = UIAlertController(title: "Camera Not Available",
message: "Sorry, but it looks like your device doesn't have a camera.",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

// This is the delagte which you have to confirm
extension YourControllerName: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
}
}

Make sure to add below two permissions in your plist.

As we can see in the above code we have videoMaximumDuration check available in the UIImagePickerController but we don’t have any such check available from iOS to put validation on the minimum duration of the video.

There is only one place where you can get the video duration and put check on that. In the delegate didFinishPickingMediaWithInfo . This Delegate triggers when you click on Use Video in the below image state of the video capturing.

The screen which comes when you stop capturing video

Now, let’s put check of minimum duration which can be uplaoded through your app.

extension ViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// Check if the selected media is a video
if let mediaType = info[.mediaType] as? String, mediaType == "public.movie" {
// Get the URL of the selected video
guard let videoURL = info[.mediaURL] as? URL else {
return
}
// Create an AVURLAsset with the selected video URL
let asset = AVURLAsset(url: videoURL)
// Get the duration of the video
let duration = asset.duration.seconds
if duration < 5 {
//let vc = UIViewController()
let alertVc = UIAlertController(title: "Minimum Duration of Video should be 5 seconds", message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay",
style: .cancel,
handler: nil)
alertVc.addAction(cancelAction)
picker.pushViewController(alertVc, animated: false)
}
}
}
}
This is the popup when you will try to upload video less than minimum duration

Thank you for reading!

Please leave comments if you have any suggestion(s) or would like to add point(s) or if you noticed any mistakes or typos or any queries!

P.S. If you found this article helpful, clap! 👏👏👏 [feels rewarding and gives the motivation to continue my writing].

--

--