Video learning series 2: Exporting a Video with a Watermark using AVFoundation in Swift

In a previous post, we discussed how to add a watermark to a video using AVFoundation in Swift. Now, we will take it a step further and learn how to export the video with the watermark.

Creating the Export Function
First, let’s create a function that exports a video with a watermark. This function will take an input URL of a video, an output URL to save the video, and a UIImage of a watermark.

Understanding the Code
This function starts by calling the addWatermark(to:watermark:) function to create a composition of the video and the watermark. If the watermark cannot be added, it prints an error message and returns.

Next, it creates an AVAssetExportSession with the composition and the highest quality preset. If the export session cannot be created, it prints an error message and returns.

The function then sets the output URL, output file type, and optimization for network use of the export session.

Finally, it exports the video asynchronously. When the export is completed, it prints a message. If the export fails or is cancelled, it also prints a message.

In this blog post, we learned how to export a video with a watermark using AVFoundation in Swift. This is a powerful technique that can be used to brand videos, add copyright notices, or provide additional information to viewers. With the power of AVFoundation, you can create professional-quality videos with ease. Happy coding!



Leave a Reply

Your email address will not be published. Required fields are marked *