73.1.1

Upload Media

The uploadFile function facilitates the uploading of photos or videos by accepting the context and the URI of the local file path as parameters.

Our transcriptions module simplifies video transcription. Upload videos effortlessly, initiate transcription, and retrieve results seamlessly, it's ideal for content management, media monitoring, and educational platforms.

import Combine
import TruvideoSdkMedia

private var disposeBag = Set<AnyCancellable>()
    
// Function to upload a video file to the cloud
func uploadFile(videoPath: String, viewController: UIViewController) {
    // Media Uploading
    // Create a file upload request using TruvideoSdkMedia uploader
    let builder = TruvideoSdkMedia.FileUploadRequestBuilder(fileURL: URL(string: videoPath)!)
    
    // Tags
    builder.addTag("key", "value")
    builder.addTag("color", "red")
    builder.addTag("order-number", "123")
    
    // Metadata
    var metadata = Metadata()
    metadata["key"] = "value"
    metadata["key1"] = 1
    metadata["key2"] = [4, 5, 6]
    builder.setMetadata(metadata)
    
    let request = builder.build()
    // Print the file upload request for debugging
    print("fileUploadRequest: ", request)
    // Completion of request
    // Handle the completion of the file upload request
    let completeCancellable = request.completionHandler
        .receive(on: DispatchQueue.main)
        .sink(receiveCompletion: { receiveCompletion in
            // Handle different completion cases
            switch receiveCompletion {
            case .finished:
                print("finished")
            case .failure(let error):
                // Print any errors that occur during the upload process
                print("failure:", error)
            }
        }, receiveValue: { uploadedResult in
            // Upon successful upload, retrieve the uploaded file URL
            let url = uploadedResult.uploadedFileURL
            print("uploadedResult: ", uploadedResult)
        })
    
    // Store the completion handler in the dispose bag to avoid premature deallocation
    completeCancellable.store(in: &disposeBag)
    // Progress of request
    // Track the progress of the file upload request
    let progress = request.progressHandler
        .receive(on: DispatchQueue.main)
        .sink(receiveValue: { progress in
            // Handle progress updates if needed
        })
    
    // Store the progress handler in the dispose bag to avoid premature deallocation
    progress.store(in: &disposeBag)
    do {
        try request.upload()
    } catch {
        // Handle error
    }
}

Last updated

Was this helpful?