Skip to content

iOS Video Preprocessing

Imagine this: you’re on vacation, you shoot an amazing 4K video, and now you want to upload it to the cloud to share with your friends or to store it safely. You hit “upload” and then learn that the file is too large. What’s worse, the app crashes because it’s trying to handle too much data at once. We’ve all been there. This is where Cloudinary’s iOS SDK comes to the rescue.

In this blog post, we’ll show you how to efficiently compress, format, and validate videos before uploading, using a feature from the Cloudinary iOS SDK. This feature ensures your large videos are uploaded quickly and without unnecessary memory overhead.

These days, videos are incredibly detailed. Smartphones can capture high-resolution 4K and even 8K footage. However, higher resolution means larger file sizes, and that can lead to a few problems:

  • Bandwidth. Uploading large video files can consume a lot of data. Not everyone has a fast or unlimited internet connection.
  • Upload time. Large files take longer to upload, leading to frustrating delays.
  • Memory consumption. Handling large files all in memory can cause apps to crash due to limited resources on mobile devices.

To solve these issues, we need to compress videos before uploading. Compression reduces the file size while maintaining quality, making it easier to handle, upload, and store your files.

Cloudinary’s iOS SDK offers a feature called Preprocessing, which allows developers to resize, compress, and validate videos before uploading. The SDK handles these operations in a chain, making it easy to fine-tune each step of the upload process.

Here’s how the preprocessing chain works:

let preprocessChain = CLDVideoPreprocessChain()

            .addStep(CLDVideoPreprocessHelpers.setOutputDimensions(dimensions: CGSize(width: 640, height: 640)))

            .addStep(CLDVideoPreprocessHelpers.setOutputFormat(format: .mov))

            .addStep(CLDVideoPreprocessHelpers.setCompressionPreset(preset: AVAssetExportPresetHighestQuality))

            .addStep(CLDVideoPreprocessHelpers.dimensionsValidator(minWidth: 100, maxWidth: 2000, minHeight: 100, maxHeight: 1000))Code language: JavaScript (javascript)

Let’s break down the different steps in the preprocessing chain:

.addStep(CLDVideoPreprocessHelpers.setOutputDimensions(dimensions: CGSize(width: 640, height: 640)))Code language: CSS (css)

This step resizes the video to the specified dimensions. In this example, we’ve resized the video to 640×640 pixels, which is great for social media or any platform where a smaller size is preferred.

.addStep(CLDVideoPreprocessHelpers.setOutputFormat(format: .mov))Code language: CSS (css)

Videos can be exported in different formats, such as MOV or MP4. This step allows you to change the format to ensure compatibility with the platform where the video will be uploaded.

.addStep(CLDVideoPreprocessHelpers.setCompressionPreset(preset: AVAssetExportPresetHighestQuality))Code language: CSS (css)

Compression presets are used to balance between quality and file size. You can choose different presets like highest quality or medium quality, depending on your use case. In this case, we’re maintaining the highest quality possible while still compressing the file.

.addStep(CLDVideoPreprocessHelpers.dimensionsValidator(minWidth: 100, maxWidth: 2000, minHeight: 100, maxHeight: 1000))Code language: CSS (css)

The dimensions validator ensures that your video falls within a specific range of width and height. For example, this step checks that the video is at least 100×100 pixels and no more than 2000×1000 pixels. This can prevent uploading unreasonably large videos that could cause issues.

If you’re developing an app that allows users to upload videos to a social media platform, the videos need to be compressed for faster uploading and converted to a compatible format. Their size should be within certain limits to avoid slowing down the app or breaking the user experience.

Here’s how you can use the Cloudinary iOS SDK to make this process efficient:

let preprocessChain = CLDVideoPreprocessChain()

            .addStep(CLDVideoPreprocessHelpers.setOutputDimensions(dimensions: CGSize(width: 640, height: 640)))

            .addStep(CLDVideoPreprocessHelpers.setOutputFormat(format: .mov))

            .addStep(CLDVideoPreprocessHelpers.setCompressionPreset(preset: AVAssetExportPresetHighestQuality))

            .addStep(CLDVideoPreprocessHelpers.dimensionsValidator(minWidth: 100, maxWidth: 2000, minHeight: 100, maxHeight: 1000))

let params = CLDUploadRequestParams()

params.setResourceType("video")

cloudinary.createUploader().upload(url: url as URL, uploadPreset: "ios_sample", params: params, preprocessChain: preprocessChain, completionHandler: { response, error in

    if let response = response {

        // Handle success, e.g., save the uploaded video details to the database

    } else if let error = error {

        // Handle the error, e.g., show a message to the user

    }

})Code language: JavaScript (javascript)

This example shows how you can configure the video upload, including setting the resource type, applying the preprocessing chain, and handling the response or error after the upload completes.

Uploading large videos shouldn’t be a hassle. By using Cloudinary’s iOS SDK, you can take advantage of preprocessing features that compress, resize, and validate your videos before uploading. This ensures faster uploads, reduced memory usage, and a smoother user experience.

Whether you’re building a video-sharing app or just want to streamline your app’s upload functionality, this preprocessing chain is a must-have tool. It takes the complexity out of handling large files and gives developers the flexibility to fine-tune their uploads.

If you haven’t already, give the Cloudinary iOS SDK a try and see how it can optimize video uploads in your app!

Ready to make your app’s video uploads smoother? Explore the Cloudinary iOS SDK features or sign up for a free Cloudinary account today.

Back to top

Featured Post