iOS v-0.0.3 / Android v-0.0.2

Imports

To integrate the Truvideo SDK, include the following platform-specific imports:

#if IOS
using TruvideoVideoiOS;
using Foundation;
using UIKit;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoVideoAndroidBinding;
using TruVideoVideoLib = TruVideoVideoAndroid.DotnetTruvideoVideo;
#endif

Async Callback Wrapper

For handling asynchronous callbacks in Android, use the following method:

#if ANDROID
private Task<T> ExecuteVideoCallbackAsync<T>(Action<VideoCallback> action) {
        var tcs = new TaskCompletionSource<T>();

        action.Invoke(new VideoCallback(
            success => MainThread.BeginInvokeOnMainThread(() =>
                tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
            failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
        ));

        return tcs.Task;
    }
#endif

This function executes image-related actions asynchronously and ensures results are processed on the main thread.

Video Information

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

private async void GetVideoInfo_OnClicked(object? sender, EventArgs e)
{

// iOS 
#if IOS
TruvideoVideoSdk.Shared.GetVideoInfo(_fileUri, (resultUrl, error) =>
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        if (error != null)
        {
            // Handle error
            LogTextEditor.Text = $"Error videos info: {error.LocalizedDescription}";
        }
        else
        {
            // Handle success
            LogTextEditor.Text = $"Videos info at: {resultUrl}";
        }
           
    });
});
#endif
#if ANDROID
string _resultPath =await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.GetVideoInfo(Application.Context, _inputPath,cb));
         LogTextEditor.Text = $"Video info: {_resultPath}";
#endif
}

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.

private async void CompareVideos__OnClicked(object? sender, EventArgs e)
{

// iOS implementation
#if IOS
TruvideoVideoSdk.Shared.CompareVideos(_videoUrls, (resultUrl, error) =>
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        if (error != null)
        {
            LogTextEditor.Text = $"Error comparing videos: {error.LocalizedDescription}";
        }
        else
        {
            LogTextEditor.Text = $"Comparison result: {resultUrl}";
        }
            
    });
});
#endif


// Android implementation
#if ANDROID
string _resultPath =
 await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.CompareVideos(Application.Context, _videoList, cb));
        LogTextEditor.Text = $"Compare Video: {_resultPath}";
#endif
}

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.

private async void Editvideo_OnClicked(object? sender, EventArgs e)
    {
    
// iOS implementation 
#if IOS
var inputUrl = NSUrl.FromFilename(_filePath);
var outputUrl = NSUrl.FromFilename(_outputPath);
var viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
TruvideoVideoSdk.Shared.EditVideo(inputUrl, outputUrl, viewController, (resultUrl, error) =>
{
    if (error != null)
    {
        LogTextEditor.Text = $"Error editing video: {error.LocalizedDescription}";
        return;
    }
    LogTextEditor.Text = $"Video edited successfully: {resultUrl}";
});
#endif

// Android implementation
#if ANDROID
string _resultPath = await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.EditVideo(Application.Context, _inputPath,_outputPath, cb));
LogTextEditor.Text = $"Edit Video: {_resultPath}";
#endif
}

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.

private async void Thumbnail_OnClicked(object? sender, EventArgs e)
{

// iOS implementation
#if IOS
var request = new ThumbnailRequest(_fileUri, 1, 320, 240, _outputUri);
TruvideoVideoSdk.Shared.GenerateThumbnail(request, (resultUrl, error) =>
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        if (error != null)
        {
            // Handle error
            LogTextEditor.Text = $"Error generating thumbnail: {error.LocalizedDescription}";
            return;
        }

        // Handle success
        LogTextEditor.Text = $"Thumbnail generated at: {resultUrl}";
        var resultFilePath = resultUrl.Path; // Use Path instead of LocalPath

        // Move or rename the result file to the desired output path
        if (File.Exists(resultFilePath))
        {
            File.Move(resultFilePath, _outputPath, true);
            LogTextEditor.Text = $"Thumbnail saved at: {_outputPath}";
        }
    });
});
#endif

// Android implementation
#if ANDROID
string _resultPath =
 await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.GenerateThumbnail(Application.Context, _inputPath, _outputPath,cb));
        LogTextEditor.Text = $"Generate Thumbnail: {_resultPath}";
#endif
    }

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.

private async void Noise_OnClicked(object? sender, EventArgs e)
{

// iOS implementation
#if IOS
TruvideoVideoSdk.Shared.ClearNoise(_fileUri, _outputUri, (resultUrl, error) =>
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        if (error != null)
        {
            // Handle error
            LogTextEditor.Text = $"Error Clear Noise: {error.LocalizedDescription}";
            return;
        }

        // Handle success
        LogTextEditor.Text = $"Thumbnail generated at: {resultUrl}";
        var resultFilePath = resultUrl.Path; // Use Path instead of LocalPath

        // Move or rename the result file to the desired output path
        if (File.Exists(resultFilePath))
        {
            File.Move(resultFilePath, _outputPath, true);
            LogTextEditor.Text = $"Clear Noise at: {_outputPath}";
        }
    });
});
#endif

// Android implementation
#if ANDROID 
_resultPath =
await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.ClearNoise(Application.Context, _inputPath,_outputPath, cb));
        LogTextEditor.Text = $"Clear Noise: {_resultPath}";
#endif
}

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.
private async void ConcatVideos_OnClicked(object? sender, EventArgs e)
{

// iOS implementation
#if IOS
TruvideoVideoSdk.Shared.ConcatVideos(_videoUrls, _outputUri, (resultUrl, error) =>
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        if (error != null)
        {
            LogTextEditor.Text = $"Error concatenating videos: {error.LocalizedDescription}";
        }
        else {
            LogTextEditor.Text = $"Concatenation successful: {resultUrl}";
        }
           
    });
});
#endif

// Android implementation
#if ANDROID
string _resultPath = await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.ConcatVideos(Application.Context, _videoList,_outputPath, cb));
        LogTextEditor.Text = $"Concat Video: {_resultPath}";
#endif
}

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.
private async void Merge_Videos_OnClicked(object? sender, EventArgs e)
{
#if ANDROID
    var frameRateType = FrameRateType.DefaultFrameRate;
    string _resultPath = await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.MergeVideos(Application.Context, videoList, outputPath, 480,900, frameRateType.ToString(),cb));
    LogTextEditor.Text = $"Merge Video: {_resultPath}";
#elif IOS
    TruvideoVideoSdk.Shared.MergeVideos(_videoUrls, _outputUri, 1280, 720, VideoFrameRate.FiftyFps,(resultUrl, error) =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            if (error != null)
            LogTextEditor.Text = $"Error merging videos: {error.LocalizedDescription}";
            else
            LogTextEditor.Text = $"Merge successful: {resultUrl}";
        });
    });
#endif
}

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)

private async void Encode_Video_OnClicked(object? sender, EventArgs e)
{
#if ANDROID
    var frameRateType = FrameRateType.DefaultFrameRate;
    string _resultPath = await ExecuteVideoCallbackAsync<string>(cb => TruVideoVideoLib.EncodeVideo(Application.Context, _inputPath,_outputPath,480,900, frameRateType.ToString(), cb));
    LogTextEditor.Text = $"Encode Video: {_resultPath}";
#elif IOS
    var outputFilePath = Path.Combine(_outputUri.AbsolutePath, "encode.mp4");
    TruvideoVideoSdk.Shared.EncodeVideo(_fileUri, new NSUrl(outputFilePath), 420, 820, VideoFrameRate.FiftyFps,(resultUrl, error) =>
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            if (error != null)
            {
            // Handle error
            LogTextEditor.Text = $"Error Encode videos: {error.LocalizedDescription}";
            return;
            }
            // Handle success
            LogTextEditor.Text = $"Videos Encode successfully: {resultUrl}";
            var resultFilePath = resultUrl.Path; // Use Path instead of LocalPath
            // Move or rename the result file to the desired output path
            if (File.Exists(resultFilePath))
            {
            File.Move(resultFilePath, _outputPath, true);
            LogTextEditor.Text = $"Videos Encode successfully: {_outputPath}";
            }
        });
    });
#endif
}

Last updated

Was this helpful?