2

TransferUtility works well when I upload the captured video file with its original format (.MOV) but when upload the converted file it becomes 0 a byte file.

After converting the file from .MOV to .MP4 I check that the file size isn't 0. Also I have to change the content type from movie/quicktime to video/mp4.

Is this the correct process?

Calling the uploading function

  uploadToAWS(path: filePath, contentType: "video/mp4", key: videoname)

Function to convert the file

 func exportVideo(inputurl: URL,
                 presetName: String = AVAssetExportPresetHighestQuality,
                 outputFileType: AVFileType = .mp4,
                 fileExtension: String = "mp4",
                 then completion: @escaping (URL?) -> Void)
{
        let asset = AVAsset(url: inputurl)


    let filename = filePath.deletingPathExtension().appendingPathExtension(fileExtension).lastPathComponent
    outputURL = FileManager.default.temporaryDirectory.appendingPathComponent(filename)

    if let session = AVAssetExportSession(asset: asset, presetName: presetName) {
        session.outputURL = outputURL
        session.outputFileType = outputFileType

        session.shouldOptimizeForNetworkUse = true
        session.exportAsynchronously {
            switch session.status {
            case .completed:
                completion(self.outputURL)
            case .cancelled:
                debugPrint("Video export cancelled.")
                completion(nil)
            case .failed:
                let errorMessage = session.error?.localizedDescription ?? "n/a"
                debugPrint("Video export failed with error: \(errorMessage)")
                completion(nil)
            default:
                break
            }
        }
    } else {
        completion(nil)
    }
}

Function for the uploading

func uploadToAWS(path: URL, contentType: String, key: String) {

        exportVideo(inputurl: path, presetName: AVAssetExportPresetHighestQuality, outputFileType: .mp4, fileExtension: "mp4") { (outputURL) in

           //here i checked that the file has not 0 bytes
            do {
                let resources = try outputURL?.resourceValues(forKeys:[.fileSizeKey])
                let fileSize = resources?.fileSize!
                print ("size of this video is \(fileSize)")
            } catch {
                print("Error: \(error)")
            }

        }

        let expression = AWSS3TransferUtilityUploadExpression()
        expression.progressBlock = progressBlock

        transferUtility.uploadFile(outputURL, bucket: bucket, key: key, contentType: contentType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
                if let error = task.error {
                    print("Error: \(error.localizedDescription)")
                    DispatchQueue.main.async {
                        print("failed")
                    }
                }
                if let _ = task.result {
                    DispatchQueue.main.async {
                        print("Upload Starting!")
                    }
                    // Do something with uploadTask.
                }
                return nil;
        }
    }
Maruta
  • 1,063
  • 11
  • 24

1 Answers1

3

Your upload (transferUtility.uploadFile(...)) currently starts as soon as exportVideo returns, which only guarantees that the AVAssetExportSession has been created, rather than waiting until it has finished. Move the upload logic inside the export completion and you should find that the upload operates on the completed export:

func uploadToAWS(path: URL, contentType: String, key: String) {

    exportVideo(inputurl: path, presetName: AVAssetExportPresetHighestQuality, outputFileType: .mp4, fileExtension: "mp4") { (outputURL) in

        //here i checked that the file has not 0 bytes
        do {
            let resources = try outputURL?.resourceValues(forKeys:[.fileSizeKey])
            let fileSize = resources?.fileSize!
            print ("size of this video is \(fileSize)")

            let expression = AWSS3TransferUtilityUploadExpression()
            expression.progressBlock = progressBlock

            transferUtility.uploadFile(outputURL, bucket: bucket, key: key, contentType: contentType, expression: expression, completionHandler: completionHandler).continueWith { (task) -> AnyObject? in
                if let error = task.error {
                    print("Error: \(error.localizedDescription)")
                    DispatchQueue.main.async {
                        print("failed")
                    }
                }
                if let _ = task.result {
                    DispatchQueue.main.async {
                        print("Upload Starting!")
                    }
                    // Do something with uploadTask.
                }
                return nil
            }
        } catch {
            print("Error: \(error)")
        }
    }
}
Chris
  • 3,445
  • 3
  • 22
  • 28