reduce video size in swift/iOS to upload to server

alionthego picture alionthego · May 31, 2016 · Viewed 13.8k times · Source

I am picking a video from my swift iOS app using the UIImagePickerController. I am saving this URL and now would like to convert it to data to send to my server for storage using:

let messageVideoData = NSData(contentsOfURL: chosenVideoURL)

The problem is that the file size is very large. For a 7 second video shot on my iPhone 6s the resolution is 1280, 720, the frame rate is 30 and the file size is over 4 MB. I have noticed the same image sent with whatsapp and other chat apps is reduced to a few hundred KB.

What is the best method to reduce the file size for external storage? The video is primarily targeted for phones so reducing the resolution to 800 or less is fine.

I tried setting the UIImagePickerController quality to:

picker.videoQuality = UIImagePickerControllerQualityType.Type640x480

but this only reduced the file size to 3.5 MB.

Using:

picker.videoQuality = UIImagePickerControllerQualityType.TypeLow

reduced the resolution to a value far lower than is desirable.

Is there another approach I should be taking to reduce my video file size for storing on my server?

Answer

onkar picture onkar · May 31, 2016

Try this answer for compress video. According to jojaba's answer:

If you are wanting to compress the video for remote sharing and to keep the original quality for local storage on the iPhone, you should look into AVAssetExportSession or AVAssetWriter.

Compress Video Without Low Quality

This approach is as per Objective-C though.

You should also consider reading on how iOS manages Assets.