Should selected videos be compressed in iOS (Swift)?

214 views Asked by At

my app allows users to pick media from their library. With videos, as they can be quite heavy in size, compression seems to become interesting.

However, while picking, it is shown that the video is already being compressed:

Compression Screen

At the bottom of the image you're able to see the blue progress bar saying "Compressing Video"

I'm making use of Apple's default UIImagePicker and therefore get a URL once the user has finished picking.

This is the code I'm using to handle picks:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard info[UIImagePickerController.InfoKey.mediaType] != nil else { return }
        let mediaType = info[UIImagePickerController.InfoKey.mediaType] as! CFString

        switch mediaType {

        case kUTTypeImage:
            // code removed for brevity

        case kUTTypeMovie:
            print("Movie!") // it's a movie!

            if let mediaUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL{
                print("Picked video with ending:", mediaUrl.path?.suffix(4)) // print out the video suffix
            }

            break

        default:
            print("Unknown media!")
        }

        viewController.dismiss(animated: true, completion: nil) // dismiss the picker

    }

Until now, I've always compressed the file further, but I'm not sure if this makes any sense at all as iOS seems to already compress the file.

I compressed it like this:

let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + NSUUID().uuidString + ".mp4")
                var compressedFileData : Data? =  nil

                compressVideo(inputURL: m.mediaUrl, outputURL: compressedURL, handler: { (_ exportSession: AVAssetExportSession?) -> Void in

                    switch exportSession!.status {
                    case .completed:

                        print("Video compressed successfully")
                        do {
                            compressedFileData = try Data(contentsOf: exportSession!.outputURL!)

                            // Call upload function here using compressedFileData


                        } catch _ {
                            print ("Error converting compressed file to Data")
                        }

                    default:
                        print("Could not compress video")
                    }
                } )

            func compressVideo(inputURL: URL, outputURL: URL, handler:@escaping (_ exportSession: AVAssetExportSession?)-> Void) {
                let urlAsset = AVURLAsset(url: inputURL, options: nil)
                guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetMediumQuality) else {
                    handler(nil)
                    return
                }

                exportSession.outputURL = outputURL
                exportSession.outputFileType = AVFileType.mp4 //AVFileTypeQuickTimeMovie (m4v)
                exportSession.shouldOptimizeForNetworkUse = true
                exportSession.exportAsynchronously { () -> Void in
                    handler(exportSession)
                }
            }

Any thoughts on this topic? Any help would be greatly appreciated!

0

There are 0 answers