78.1.1+

TruvideoSdkVideoFile

The TruvideoSdkVideoFile class is used to reference an existing file in the iOS file system. It can be initialized using either a URL or a file path.

Initializing with a URL

You can reference a video file using its URL with the following initializer:

let input: TruvideoSdkVideoFile = .init(url: URL(string: "dir/subdir/video_result.mp4")!)

Initializing with a Path

Alternatively, you can use the file path directly to point to a video file:

let input: TruvideoSdkVideoFile = .init(path: "dir/subdir/video_result.mp4")

Methods

  • init(url: URL): Initializes the video file using its URL.

  • init(path: String) throws: Initializes the video file using its path. This method can throw an error if the file path is invalid or inaccessible.

TruvideoSdkVideoFileDescriptor

The TruvideoSdkVideoFileDescriptor class is used when you want the SDK to create a video file for you. You can specify the file's location using various methods, such as within the cache directory, the files directory, or by providing a custom path.

Using Cache Directory

This method creates or references a file in the app’s cache directory. It's useful for temporary files that can be cleared.

let output1: TruvideoSdkVideoFileDescriptor = .cache(fileName: "video_result")
let output2: TruvideoSdkVideoFileDescriptor = .cache(fileName: "dir/subdir/video_result")

Using Files Directory

This method references a file in the app's files directory, which is persistent and typically used for files the app might need long-term.

let output1: TruvideoSdkVideoFileDescriptor = .files(fileName: "video_result")
let output2: TruvideoSdkVideoFileDescriptor = .files(fileName: "dir/subdir/video_result")

Using Custom Path

This method allows you to provide a custom file path anywhere in the file system that the app has access to.

let output1: TruvideoSdkVideoFileDescriptor = .custom(rawPath: "video_result")
let output2: TruvideoSdkVideoFileDescriptor = .custom(rawPath: "dir/subdir/video_result")

Methods

  • cache(fileName: String): Specifies a file to be created in the app’s cache directory.

  • files(fileName: String): Specifies a file to be created in the app’s files directory.

  • custom(rawPath: String): Specifies a file to be created at a custom location in the file system.

Video Information

This function gives you all the important details about a video, like its resolution, duration, codec, and other useful metadata.

import TruvideoSdkVideo

// Function to fetch information about a video.
 func getVideoInfo(
        input: TruvideoSdkVideoFile
    ) async throws -> TruvideoSdkVideoInformation {
        let result = try await TruvideoSdkVideo.getVideosInformation(input: [input])
        // handle videoInfo
        guard let videoInfo = result.first else {
            throw NSError(domain: "VideoInfoError", code: -1, userInfo: [NSLocalizedDescriptionKey: "No video information found"])
        }
        return videoInfo
    }

Compare Video

This function checks if multiple videos are compatible for concatenation, returning true if they have matching characteristics and false if they need merging.

import TruvideoSdkVideo

// Function to determine if videos can be concatenated
func compare(
    input: [TruvideoSdkVideoFile]
) async throws -> Bool {
        // Check if the videos can be concatenated using TruvideoSdkVideo
    let result = try await TruvideoSdkVideo.canConcat(input: input)
    return result
}

Edit Video

This function initiates video editing by creating a preset with the input video URL and the desired output URL. It then presents the TruvideoSdkVideoEditorView with the preset, allowing users to perform editing actions. Upon completion, it provides the result of the edited video.

import TruvideoSdkVideo

func edit(video: URL, output: URL) throws {
            guard FileManager.default.fileExists(atPath: video.path) else {
                throw NSError(domain: "VideoEditorError", code: -1, userInfo: [NSLocalizedDescriptionKey: "Input video file does not exist"])
            }
        let preset : TruvideoSdkVideoFile = .init(url: video)
            let outputDescriptor = TruvideoSdkVideoFileDescriptor.custom(rawPath: output.path)
            self.presentTruvideoSdkVideoEditorView(input: preset, output: outputDescriptor, onComplete: { result in
                if let editedVideoURL = result.editedVideoURL {
                    print("Video editing successful. Edited video URL: \(editedVideoURL)")
                } else {
                    print("Video editing failed or was canceled.")
                }
            })
        }

Thumbnail Generation

This function asynchronously generates a thumbnail for a given video using the TruvideoSdkVideo's thumbnailGenerator. It accepts a TruvideoSdkVideoThumbnailInputVideo object as input, which encapsulates the necessary information about the video.

import TruvideoSdkVideo
// Function to generate a thumbnail for a video asynchronously
func generateThumbnail(
        video: TruvideoSdkVideoFile,
        output: TruvideoSdkVideoFileDescriptor,
        width: Int?,
        height: Int?,
        position: TimeInterval
    ) async throws -> URL {
        // Call generateThumbnail with individual parameters
        let result = try await TruvideoSdkVideo.generateThumbnail(input: video, output: output, position: position, width: width, height: height)
        return result.generatedThumbnailURL
    }

Clean noise

This function asynchronously utilizes TruvideoSdkVideo's engine to clean noise from a specified video file located at the given input URL. The cleaned video is then saved to the provided output URL.

import TruvideoSdkVideo
// Function to asynchronously clean noise from a video file
func clearNoise(
    input: TruvideoSdkVideoFile,
    output: TruvideoSdkVideoFileDescriptor
) async throws -> URL {
    // Attempt to clean noise from the input video file using TruvideoSdkVideo's engine
    let result = try await TruvideoSdkVideo.engine.clearNoiseForFile(input: input, output: output)
    return result.fileURL
}

Concat Videos

This function concatenates multiple videos into a single video file. First, it checks if the videos can be concatenated by calling the canConcatVideos function. If the videos are compatible, it proceeds to concatenate them using the ConcatBuilder. Finally, it prints the output path of the concatenated video.

Note:
1. Concatenation necessitates uniform characteristics across all provided videos, including resolution, audio codec, video codec, and pixel format.
2. Any discrepancies in these characteristics may lead to exceptions during the concatenation process.
3. To verify compatibility, it's advisable to use the compare method mentioned earlier to ensure the videos meet the necessary criteria before concatenation.
import TruvideoSdkVideo
func concatVideos(
    input: [TruvideoSdkVideoFile],
    output: TruvideoSdkVideoFileDescriptor,
    width: CGFloat?,
    height: CGFloat?,
    frameRate: TruvideoSdkVideoFrameRate
) async throws -> URL {
    // Concatenate the videos using ConcatBuilder
    let builder = TruvideoSdkVideo.ConcatBuilder(input: input, output: output)
    let request = builder.build()
    let result = try await request.process()
    return result.videoURL
}

Merge Videos

The mergeVideos function combines several videos into one file. Unlike just sticking them together, this method reprocesses the videos, which might take longer. The merged video could end up with different qualities to fit all the originals. Unlike a simple join, this method lets you mix videos without any specific limitations, giving you more options.

Note:
1. Any video resolution can be merged without causing errors.
2. Unlike the concatenate method, this process involves reprocessing the videos, which might slow things down, especially on slower devices.
3 The resulting video's resolution is automatically adjusted to make sure it plays properly.
import TruvideoSdkVideo
// Function to merge multiple videos into a single video file
func mergeVideos(
    input: [TruvideoSdkVideoFile],
    output: TruvideoSdkVideoFileDescriptor,
    width: CGFloat?,
    height: CGFloat?,
    frameRate: TruvideoSdkVideoFrameRate
) async throws -> URL {
   // Create a MergeBuilder instance with specified parameters. 
    let builder = TruvideoSdkVideo.MergeBuilder(input: input, output: output)
    builder.width = width
    builder.height = height
    builder.framesRate = frameRate
    let request = builder.build()
    let result = try await request.process()
    return result.videoURL
}

Encode Video

This function is used to build a TruvideoSdkVideoRequest that performs an encoding operation. An encoding operation allows to perform changes over a single video by changing one or many of its attributes, such as:

  1. Resolution through the width and height parameters

  2. Video codec (.h264 and .h265)

  3. Audio codec (.aac, .mp3, .ac3)

  4. Frame rate (24fps, 25fps, 30fps, 50fps, 60fps)

import TruvideoSdkVideo
// Function to encode a video with multiple parameters
func encodeVideo(
    input: TruvideoSdkVideoFile,
    output: TruvideoSdkVideoFileDescriptor,
    width: CGFloat?,
    height: CGFloat?,
    frameRate: TruvideoSdkVideoFrameRate
) async throws -> URL {
    let builder = TruvideoSdkVideo.EncodingBuilder(input: input, output: output)
    let request = builder.build()
    let result = try await request.process()
    return result.videoURL
}

Last updated

Was this helpful?