78.1.2+
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.
Example Usage :
Method 1 :
import Combine
import TruvideoSdkMedia
private var disposeBag = Set<AnyCancellable>()
// Function to upload a video file to the cloud
func uploadFile(videoPath: String) {
// 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")
builder.addMetadata("key","value")
builder.addMetadata("key1","1")
builder.addMetadata("key2",["4", "5", "6"])
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
}
}
Method 2
import Combine
import TruvideoSdkMedia
private var disposeBag = Set<AnyCancellable>()
private func uploadFile(at url: URL) {
// Initialize a builder for tags to be associated with the media file
let tagsBuilder = TruvideoSdkMediaTags
.builder()
.set("key", "value")
.set("color", "red")
// Initialize a nested metadata builder for more complex metadata
let anotherNestedMetadataBuilder = TruvideoSdkMediaMetadata
.builder()
.set("myNestedMetadataString", "myNestedMetadataStringValue") // Set a metadata string value
// Initialize another nested metadata builder that includes the previous nested metadata
let nestedMetadataBuilder = TruvideoSdkMediaMetadata
.builder()
.set("key", "value")
.set("list", anotherNestedMetadataBuilder.build())
// Initialize the main metadata builder that includes both simple and nested metadata
let metadataBuilder = TruvideoSdkMediaMetadata
.builder()
.set("key", "value")
.set(
"list", ["value1", "value1"]
)
.set("nested", nestedMetadataBuilder.build())
// Create a file upload request builder with the file URL, metadata, and tags
let requestBuilder = TruvideoSdkMedia.FileUploadRequestBuilder(
fileURL: url, // The URL of the file to be uploaded
metadata: metadataBuilder.build(), // Build the metadata to be attached to the upload
tags: tagsBuilder.build() // Build the tags to be attached to the upload
)
// Build the request
let request = requestBuilder.build()
// Handle the completion of the upload process, printing an error if it occurs
request.completionHandler.sink { completion in
print("error \(completion)")
} receiveValue: { result in
print("Result received")
}
.store(in: &disposeBag) // Store the subscription in the dispose bag for memory management
// Handle the progress of the upload process, printing the current progress
request.progressHandler.sink { progress in
print("Progress \(progress.percentage)")
}
.store(in: &disposeBag) // Store the subscription in the dispose bag for memory management
// Attempt to start the upload process
try? request.upload() // Use `try?` to handle any potential errors silently
}
Search Media -
The search
function is designed to search for media files within the Truvideo SDK based on specified tags and media file type. The function constructs a tag set using the TruvideoSdkMediaTags
builder and then performs a search for media files of the specified type, within the provided pagination limits.
Tags:
The function builds a set of tags using the
TruvideoSdkMediaTags.builder()
method.
let tagsBuilder = TruvideoSdkMediaTags
.builder()
.set("myTag", "myTagValue")
.set("anotherTag", "anotherTagValue")
Search Query:
The search is performed using the
TruvideoSdkMedia.search
method with the following parameters:tags: TruvideoSdkMediaTags
: The tags to filter the search results.type: TruvideoSdkMediaFileType
: The type of media files to search for (e.g.,Picture
,Video
).pageNumber: Int
: The starting point for the search results (used for pagination).pageSize: Int
: The maximum number of results to return.
Returns:
result: List<TruvideoSdkMediaFile>
The search results, which consist of a list of media files that match the specified tags and file type. The size of the list is limited by the
limit
parameter.
private func search() async throws {
let tagsBuilder = TruvideoSdkMediaTags
.builder()
.set("myTag", "myTagValue")
.set("anotherTag", "anotherTagValue")
let result = try await TruvideoSdkMedia.search(
type: .image, tags: tagsBuilder.build(), pageNumber: 0, size: 10
)
}
Last updated
Was this helpful?