title: AVAsset description: New AVAsset / AVAssetTrack APIs: a heavyweight handle to a media asset for loading metadata, tracks, and extracting still images without spinning up an AVPlayer. Pass the same instance to AVPlayer.setSource(asset) to share the underlying media.
AVAsset is a heavyweight handle to a media resource (audio / video) backed by an iOS AVURLAsset. It lets you read media properties, metadata, and tracks, and extract still images at arbitrary times — without spinning up an AVPlayer.
You can also pass an AVAsset to AVPlayer.setSource(asset) to share the underlying media (and any already-loaded metadata) between inspection and playback.
Getting Started
Validation
The constructor is intentionally lazy and matches the iOS AVURLAsset semantics:
- It does not verify file existence.
- It does not verify URL reachability.
- For remote URLs, only obviously malformed strings (where the underlying
URL(string:)returnsnil) throw immediately. - All other failures (404, network errors, missing local files, unrecognized formats) surface as Promise rejections on the first
loadXxx()call.
API Reference
Constructor
new AVAsset(filePathOrURL: string, options?: { headers?: Record<string, string> })
Creates an asset from either a local file path or a remote http(s):// URL.
For remote URLs, you may pass HTTP headers (useful for authenticated content):
Properties
source: string
The original file path or URL string used to construct this asset. Read-only.
Async Loaders
All loadXxx() methods return a Promise. Failures (including missing files / unreachable URLs / unsupported formats) reject with an Error.
loadDuration(): Promise<MediaTime>
The total duration of the asset.
loadIsPlayable(): Promise<boolean>
Whether the asset can be played back.
loadIsExportable(): Promise<boolean>
Whether the asset can be exported (e.g. via AVAssetExportSession).
loadIsReadable(): Promise<boolean>
Whether the asset's media data can be read.
loadHasProtectedContent(): Promise<boolean>
Whether the asset has DRM-protected content.
loadPreferredTransform(): Promise<{ a, b, c, d, tx, ty }>
The 6 components of the preferred affine transform (rotation / scale / translation) used when rendering the video portion.
loadMetadata(): Promise<AVMetadataItem[] | null>
Loads all metadata items from the asset.
loadCommonMetadata(): Promise<AVMetadataItem[] | null>
Loads the common metadata items (each carries a commonKey).
Tracks
loadTracks(mediaType?: AVMediaType): Promise<AVAssetTrack[]>
Loads tracks from the asset, optionally filtered by media type.
AVMediaType is one of:
AVAssetTrack
Image Generation
generateImage(time, options?): Promise<{ image, actualTime }>
Generates a still image at a single requested time.
generateImages(times, options?): Promise<...>
Generates still images for an array of times. Each result is reported independently — successful entries carry image and actualTime; failed entries carry error.
AVAssetImageGenerateOptions
Lifecycle
dispose(): void
Releases the underlying AVURLAsset. Subsequent loadXxx() calls will reject. The script runtime auto-disposes any AVAsset you do not explicitly dispose when the script ends.
Sharing With AVPlayer
AVPlayer.setSource(asset) reuses the underlying media so any property already loaded on the asset is shared:
Best Practices
- Use the right level of API — for inspection only, prefer
AVAssetoverAVPlayerto avoid the playback machinery. - Share assets between inspection and playback — construct an
AVAssetonce, pass it toAVPlayer.setSource(asset). - Always handle Promise rejections — failures surface there, not in the constructor.
- Prefer batch image generation —
generateImages(times)is more efficient than callinggenerateImage(time)in a loop. - Tolerances trade exactness for speed — for thumbnails, a small
toleranceBefore/After(e.g. half a second) is usually faster than exact-frame seeking.
