<!--
{
  "documentType" : "article",
  "framework" : "AVKit",
  "identifier" : "/documentation/AVKit/implementing-trimming-in-a-macos-player",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Implementing Trimming in a macOS Player"
}
-->

# Implementing Trimming in a macOS Player

Provide a QuickTime media-trimming experience in your macOS app.

## Discussion

You use [`AVPlayerView`](/documentation/AVKit/AVPlayerView) to provide a playback experience like that of QuickTime Player in macOS. However, `AVPlayerView` not only provides the QuickTime playback interface, but it also provides the QuickTime media-trimming experience.

![Single image with multiple video frames selected and Trim button in an enabled state.](images/com.apple.avkit/media-2948756.png)

### Verify that Trimming Is Allowed

Before attempting to put the player into trimming mode, verify that trimming is allowed by querying the player view’s [`canBeginTrimming`](/documentation/AVKit/AVPlayerView/canBeginTrimming) property. This property returns `false` if you’re playing an asset delivered over HTTP Live Streaming or if the asset is content protected. If you’re presenting a menu item to initiate trimming, a good place to perform this check is in the <doc://com.apple.documentation/documentation/AppKit/NSDocument/validateUserInterfaceItem(_:)> method of <doc://com.apple.documentation/documentation/AppKit/NSDocument>, so that the menu item can automatically be disabled if trimming is disallowed.

```swift
override func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
    if item.action == #selector(beginTrimming) {
        return playerView.canBeginTrimming
    }
    return super.validateUserInterfaceItem(item)
}
```

### Enter Trimming Mode

After you’ve determined that the media supports trimming, you call the [`beginTrimming(completionHandler:)`](/documentation/AVKit/AVPlayerView/beginTrimming(completionHandler:)). This method takes a completion block that you use to determine whether the user completed the trim or canceled the operation.

```swift
@IBAction func beginTrimming(_ sender: AnyObject) {
    playerView.beginTrimming { result in
        if result == .okButton {
            // user selected Trim button (AVPlayerViewTrimResult.okButton)...
        } else {
            // user selected Cancel button (AVPlayerViewTrimResult.cancelButton)...
        }
    }
}
```

### Transcode the Trimmed Asset

Because <doc://com.apple.documentation/documentation/AVFoundation/AVAsset> is an immutable object, you may be wondering how its duration is changed when you click the Trim button. Trimming relies on a feature of <doc://com.apple.documentation/documentation/AVFoundation/AVPlayerItem> to adjust the presented time range. `AVPlayerItem` provides the <doc://com.apple.documentation/documentation/AVFoundation/AVPlayerItem/reversePlaybackEndTime> and <doc://com.apple.documentation/documentation/AVFoundation/AVPlayerItem/forwardPlaybackEndTime> properties that set the in and out points for a media item. It doesn’t change the underlying asset, but essentially changes your effective view of it. To save the results of the user’s trim operation, you export a new copy of the asset, trimming it to the specified times. The simplest way to do this is to use <doc://com.apple.documentation/documentation/AVFoundation/AVAssetExportSession>, which provides a simple and performant way for you to transcode the media of an asset. You create a new export session, passing it the asset to export along with a transcoding preset to use.

```swift
// Transcoding preset
let preset = AVAssetExportPresetAppleM4V720pHD
let exportSession = AVAssetExportSession(asset: playerItem.asset, presetName: preset)!
exportSession.outputFileType = AVFileTypeAppleM4V
exportSession.outputURL = // Output URL
```

This example uses a preset to export the media as a 720p, M4V file, but `AVAssetExportSession` supports a wide variety of export presets. To find out what export session presets are supported for the current asset, you can use the session’s <doc://com.apple.documentation/documentation/AVFoundation/AVAssetExportSession/exportPresets(compatibleWith:)> class method, passing it the asset you want to export. This method returns an array of valid presets that you can use in your export.

### Select the Trimmed Asset

To export only the content the user trimmed, you use the current player item’s reverse and forward end-time values to define a <doc://com.apple.documentation/documentation/CoreMedia/CMTimeRange> to set on the export session.

```swift
// Create CMTimeRange with the trim in/out point times
let startTime = self.playerItem.reversePlaybackEndTime
let endTime = self.playerItem.forwardPlaybackEndTime
let timeRange = CMTimeRangeFromTimeToTime(startTime, endTime)
exportSession.timeRange = timeRange
```

### Export the Trimmed Asset

To perform the actual export operation, you call its <doc://com.apple.documentation/documentation/AVFoundation/AVAssetExportSession/exportAsynchronously(completionHandler:)> method. Check the status of the export session in the completion handler and handle completion and failure cases.

```swift
exportSession.exportAsynchronously {
    switch exportSession.status {
    case .completed:
        // Export Complete
    case .failed:
        // failed
    default:
        // handle others
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)