---
title: "#303: Editing Videos in UIKit 📼"
url: https://littlebitesofcocoa.com/303-editing-videos-in-uikit
author: Jake Marsh
date: 2017-03-10
published: 2017-03-10
topics: ["B-sides","UIKit"]
description: "Today we're continuing our hunt for hidden gems in UIKit with UIVideoEditorController. It's a cousin to UIImagePickerViewController that exposes just the..."
---

# #303: Editing Videos in UIKit 📼

By Jake Marsh, March 10, 2017. HTML: https://littlebitesofcocoa.com/303-editing-videos-in-uikit

Today we're continuing [our hunt for hidden gems in UIKit](https://littlebitesofcocoa.com/topics/80-b-sides?v=l) with _`UIVideoEditorController`_. 

It's a cousin to [`UIImagePickerViewController`](https://littlebitesofcocoa.com/83-uiimagepickercontroller-basics) that exposes <i>**just**</i> the **basic video editing** functionality from that class in a standalone, dedicated _video editing view controller_.

Let's give it a try.

First, we'll need a **video file** to edit. We'll use [this one](https://videos.pexels.com/videos/typing-on-keyboard-of-macbook-pro-323) which is part of a [freely available collection](https://videos.pexels.com/video-license). 

We'll download the **video**, rename it to something simple, and drag it into **Xcode**. We'll check the box next to our **app** in the dialog that appears, so it gets copied to our **app target**.

Now, let's write some code.

First, we'll make sure our video can be found:

``` swift
guard let path = Bundle.main.path(forResource: "video", ofType: "mp4") else { return }
```

...and that **UIKit** knows how to edit it:

``` swift
guard UIVideoEditorController.canEditVideo(atPath: path) else { return }
```

Nice. Next, we can create a new _video editor view controller_ and configure a few things on it:

``` swift
let editor = UIVideoEditorController()

editor.videoPath = path
editor.videoMaximumDuration = 10.0
editor.videoQuality = .typeIFrame1280x720
```

Here we've told it our path, and given it a 10 second max duration. (**Pro Tip:** Default is 10 minutes, set to `0` for no max).

From here we can simply present it like any other **view controller**:

``` swift
present(editor, animated: true, completion: nil)
```

Neat!

The best part is all the functionality is self-contained inside the **view controller**. 

The user can scrub through:

<img src="https://media.littlebitesofcocoa.com/303-1.png" style="max-width: 640px; border-radius: 4px;" />

Trim the edges:

<img src="https://media.littlebitesofcocoa.com/303-2.png" style="max-width: 640px; border-radius: 4px;" />

Then save the video back to the `videoPath` we set earlier:

<img src="https://media.littlebitesofcocoa.com/303-3.png" style="max-width: 640px; border-radius: 4px;" />

**Note:** [The documentation](https://developer.apple.com/reference/uikit/uivideoeditorcontroller) mentions _`UIVideoEditorController`_ <i>"only supporting Portrait"</i> **orientations**, but it seems to work fine in all **orientations**.

Last but not least, we can (optionally) set a `delegate` on our _`UIVideoEditorController`_ to get notified when the user saves or cancels (or a save fails):

``` swift
editor.delegate = self

extension SomeViewController : 
    UIVideoEditorControllerDelegate, 
    UINavigationControllerDelegate {

  func videoEditorController(_ editor: UIVideoEditorController, 
    didSaveEditedVideoToPath editedVideoPath: String) {

    print("saved!")
  }
}
```

That's all for today. Know of an interesting [UIKit B-side](https://www.littlebitesofcocoa.com/topics/80-b-sides?v=l)?

[Send it on over!](mailto:hello@littlebitesofcocoa.com?subject=UIKit%20B-side%20Alert!)

<p></p>
