# Checking a Composition Before Export

Find out what will make an export differ from what you asked for, before rendering it.

## Overview

AVFoundation fails late and quietly. A 60 fps clip becomes 30 fps without a word, an HDR
clip comes out SDR, and an export on the iOS Simulator with an overlay throws only after
everything else has been built. All of it can be known before rendering, from the
composition and its sources' metadata. ``Video/diagnose()`` reports it, each with a fix:

```swift
let video = Video {
    VideoClip(url: beachURL)
    Transition.dissolve(duration: 0.5)
    VideoClip(url: sunsetURL).filter(.mono)
}
.preset(.reelsAndShorts)

for diagnostic in await video.diagnose() {
    print(diagnostic)
}
```

```
warning [frameRateDowngrade] clip [0]: beach.mov is 59.94 fps and the export is 30 fps, so frames are dropped and motion is less smooth. Fix: Export at its rate with .preset(.custom(width: 1080, height: 1920, frameRate: 60, codec: .hevc)).
info [aspectFillCrop] clip [0]: beach.mov is 1920×1080 and the export is 1080×1920, so filling the frame crops 68% of it. Fix: Choose a preset of its shape, or show all of it with .transform(Transform(scale: 0.32)).
info [filterPreRender] clip [2]: This clip's filters are rendered in a pass of their own before the export, which costs one extra encode.
```

It reads only metadata (the clip tree, and each source's tracks, format descriptions and
frame timing), so it takes a moment, not a render. It never throws: a source it can't
read is itself a diagnostic.

## What it checks

| Kind | Severity | When |
|---|---|---|
| ``Diagnostic/Kind/unreadableSource`` | error | A `VideoClip`'s file is missing, isn't a file URL, has no video track, or can't be read |
| ``Diagnostic/Kind/overlaysOnSimulator`` | error | Overlays or a watermark, on the iOS Simulator. See <doc:KnownPlatformIssues>. |
| ``Diagnostic/Kind/frameRateDowngrade`` | warning | A source shows more frames a second than the preset exports, after speed: a 60 fps clip at `.speed(0.5)` fits a 30 fps export |
| ``Diagnostic/Kind/variableFrameRate`` | warning | A source's frames aren't evenly spaced, as a phone records in low light |
| ``Diagnostic/Kind/hdrToSDR`` | warning | A source is HDR (HLG or PQ). kadr exports SDR, so it's converted. |
| ``Diagnostic/Kind/ignoredCompositor`` | warning | ``Video/compositor(_:)-(MultiInputCompositor)`` is set, but every clip plays on one track, so it never runs |
| ``Diagnostic/Kind/aspectFillCrop`` | info | A source's shape differs from the preset's by more than 10%, so filling the frame crops it. Not reported for a clip with a transform. |
| ``Diagnostic/Kind/filterPreRender`` | info | A clip's filters or compositors cost an extra encode |

A source used by several clips is reported once per check, at the first clip that uses
it.

## Reading the result

The list is sorted worst first, then in builder order, so `first` is the worst problem,
and an empty list means there's nothing to report:

```swift
let diagnostics = await video.diagnose()
if let worst = diagnostics.first, worst.severity == .error {
    print("Not exporting: \(worst)")
    return
}
try await video.export(to: outputURL)
```

Each ``Diagnostic`` has a ``Diagnostic/clip`` saying which clip it's about: its ``ClipID``
if you gave it one, and its position in the builder either way. `[2]` is the third entry
of the `Video` (transitions count), and `[3].clips[0]` is the first clip inside a
``Track`` that is the fourth.

Match on ``Diagnostic/kind`` and ``Diagnostic/severity``. They're stable; the wording of
``Diagnostic/message`` and ``Diagnostic/fix`` may improve in any release. New kinds can be
added in a minor release, which is why ``Diagnostic/Kind`` is a struct and not an enum.

## For coding agents

An agent that writes a composition can call `diagnose()` and act on the result, without
rendering and watching the output. Each `description` is one line holding the kind, the
clip, what will happen and the fix.

## Topics

### Diagnosing

- ``Video/diagnose()``
- ``Diagnostic``
- ``ClipReference``
