AVAsset and Composition
Inspect and combine media tracks.
AVAsset and Composition is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
AVAsset Represents Media
AVAsset models a piece of timed media—its tracks, duration, and metadata—without committing to playback. It is the foundation for editing.
import AVFoundation
let asset = AVURLAsset(url: fileURL)Loading Properties Asynchronously
Asset properties load lazily. Use the async load(_:) API to fetch duration or tracks without blocking.
let duration = try await asset.load(.duration)
let tracks = try await asset.load(.tracks)Tracks
An asset has AVAssetTracks, each carrying one media type—video, audio, subtitles. You inspect and reuse these tracks when editing.
let videoTracks = try await asset.loadTracks(withMediaType: .video)
let audioTracks = try await asset.loadTracks(withMediaType: .audio)AVMutableComposition
AVMutableComposition is an editable asset you build by inserting time ranges from other assets—the core of trimming and stitching clips.
let composition = AVMutableComposition()Adding Composition Tracks
Create a mutable track for each media type, specifying its type and a track ID.
let videoTrack = composition.addMutableTrack(
withMediaType: .video,
preferredTrackID: kCMPersistentTrackID_Invalid)Inserting Time Ranges
Copy a segment from a source track into your composition track with insertTimeRange, choosing where it lands on the timeline.
let range = CMTimeRange(start: .zero, duration: try await asset.load(.duration))
try videoTrack?.insertTimeRange(range, of: sourceTrack, at: .zero)Trimming a Clip
Insert only part of a source by specifying a sub-range—the start and a shorter duration—to trim.
let clip = CMTimeRange(
start: CMTime(seconds: 5, preferredTimescale: 600),
duration: CMTime(seconds: 10, preferredTimescale: 600))
try videoTrack?.insertTimeRange(clip, of: sourceTrack, at: .zero)Stitching Multiple Clips
Append clips end to end by inserting each at the current composition duration, building a single continuous timeline.
var cursor = CMTime.zero
for src in sourceTracks {
let r = CMTimeRange(start: .zero, duration: src.timeRange.duration)
try videoTrack?.insertTimeRange(r, of: src, at: cursor)
cursor = cursor + r.duration
}Video Composition for Effects
For transforms, layering, or transitions, pair the composition with an AVMutableVideoComposition that defines per-time-range instructions.
let videoComposition = AVMutableVideoComposition()
videoComposition.frameDuration = CMTime(value: 1, timescale: 30)Exporting the Result
Render the composition to a file with AVAssetExportSession, choosing a preset and output URL.
let export = AVAssetExportSession(
asset: composition, presetName: AVAssetExportPresetHighestQuality)
export?.outputURL = outputURL
export?.outputFileType = .mp4
await export?.export()Previewing Before Export
Because a composition is itself an AVAsset, you can wrap it in an AVPlayerItem and preview the edit before committing to an export.
let previewItem = AVPlayerItem(asset: composition)
let player = AVPlayer(playerItem: previewItem)Quick Check: Composition
Test your understanding of AVAsset and composition.
Recap: AVAsset and Composition
AVAsset models media (tracks, duration, metadata) with async property loading. AVMutableComposition builds editable timelines by inserting time ranges from source tracks—enabling trimming and stitching.
Add an AVMutableVideoComposition for effects, preview by wrapping the composition in an AVPlayerItem, and render with AVAssetExportSession.
Frequently asked questions
Is the “AVAsset and Composition” lesson free?
Yes — the full text of “AVAsset and Composition” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “AVAsset and Composition”?
Inspect and combine media tracks. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “AVAsset and Composition” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Playing Media with AVPlayer
- Recording Audio
- AVAsset and Composition
- Embedding Players in SwiftUI