Encoding video for offline transcoding
At a glance
| Item | Summary |
|---|---|
| Purpose | Configure a compression session to transcode video in offline workflows. |
| App architecture | A C/Objective-C header, Objective-C, Swift sample bundle with entry-bearing project variants Objective-C, Swift, each leading to VideoToolbox APIs. |
| Main patterns | Actor isolation |
| Project style | 15 scanned source file(s) across C/Objective-C header, Objective-C, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: async declaration or closure, Sendable or @Sendable, Task, DispatchQueue(label:), actor; none alone proves a background thread. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | AVFoundation, VideoToolbox, ArgumentParser, string.h; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Swift/
│ ├── VTEncoderForTranscoding/
│ │ ├── CommandOptions.swift
│ │ └── VTEncoderForTranscoding.swift
│ └── VTEncoderUtils/
│ ├── VideoSource.swift
│ ├── RealTimeVideoSource.swift
│ ├── VTEncoderUtil.swift
│ └── VideoSink.swift
└── Objective-C/
├── VTEncoderForTranscoding/
│ ├── main.m
│ └── VTEncoderForTranscoding.h
└── VTEncoderUtils/
├── VideoSource.h
├── VideoSource.m
├── VideoSink.h
└── VideoSink.m
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Objective-C, Swift.
- The verified tree contains 6 project/configuration file(s) and 24 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Objective-C"]
V2["Swift"]
Boundary["VideoToolbox APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Swift/VTEncoderForTranscoding/CommandOptions.swift:13 — architecture anchor
@main
struct CommandOptions: AsyncParsableCommand {
// ...
static var configuration = CommandConfiguration(
usage: """
VTEncoderForTranscoding <source-movie> [<options>]
""")
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
CommandOptions *-- CommandConfiguration : configuration
CommandOptions *-- String : sourceMovie
CommandOptions *-- Int : bitrate
CommandOptions o-- CodecType : codecType
Ownership evidence
Swift/VTEncoderForTranscoding/CommandOptions.swift:16 — stored dependency or nearest verified ownership anchor
@main
struct CommandOptions: AsyncParsableCommand {
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
CommandOptions |
CommandConfiguration (configuration) |
creates and retains | App/module collaborators |
CommandOptions |
String (sourceMovie) |
owns value state | App/module collaborators |
CommandOptions |
Int (bitrate) |
owns value state | App/module collaborators |
CommandOptions |
CodecType (codecType) |
stores or receives | App/module collaborators |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Suspension boundary | async declaration or closure |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | Swift/VTEncoderForTranscoding/CommandOptions.swift:169 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | Swift/VTEncoderForTranscoding/VTEncoderForTranscoding.swift:12 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | Swift/VTEncoderForTranscoding/VTEncoderForTranscoding.swift:413 |
| Queue scheduling | DispatchQueue(label:) |
The source constructs a dispatch queue; its label alone does not prove a thread. | Swift/VTEncoderUtils/RealTimeVideoSource.swift:50 |
| Actor isolation | actor |
The cited type is actor-isolated; this does not select a background thread. | Swift/VTEncoderUtils/VideoSource.swift:229 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
Swift/VTEncoderForTranscoding/CommandOptions.swift:169 — representative execution boundary
mutating func run() async throws {
// ...
let codec: CMVideoCodecType
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | AVFoundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/VTEncoderForTranscoding/VTEncoderForTranscoding.m:8 |
| Source import | VideoToolbox |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/VTEncoderForTranscoding/VTEncoderForTranscoding.m:9 |
| Source import | ArgumentParser |
The cited file imports this module; runtime use and architectural role are not inferred. | Swift/VTEncoderForTranscoding/CommandOptions.swift:8 |
| Source import | string.h |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/VTEncoderUtils/VTEncoderUtil.m:9 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
Swift/VTEncoderForTranscoding/CommandOptions.swift:14 — representative type boundary
@main
struct CommandOptions: AsyncParsableCommand {
// ...
var sourceMovie: String
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CommandOptions |
Carries feature or framework configuration | AsyncParsableCommand |
CodecType |
Defines a closed set of feature states or choices | String, CaseIterable, ExpressibleByArgument |
PixelFormatType |
Defines a closed set of feature states or choices | String, CaseIterable, ExpressibleByArgument |
PresetType |
Defines a closed set of feature states or choices | String, CaseIterable, ExpressibleByArgument |
ProfileType |
Defines a closed set of feature states or choices | String, CaseIterable, ExpressibleByArgument |
QPModulationLevel |
Defines a closed set of feature states or choices | String, CaseIterable, ExpressibleByArgument |
VideoSource |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
SourceInfo |
Represents feature data | Sendable |
VideoSourceFrames |
Represents a feature value or composable behavior | AsyncSequence |
SampleBufferQueue |
Owns feature behavior and collaborator lifecycle | @unchecked Sendable |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
frameRate (Objective-C/VTEncoderUtils/VideoSource.h:23) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
width (Objective-C/VTEncoderUtils/VideoSource.h:26) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
height (Objective-C/VTEncoderUtils/VideoSource.h:29) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
sourceMoviePath (Swift/VTEncoderForTranscoding/VTEncoderForTranscoding.swift:25) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
Objective-C/VTEncoderUtils/VideoSource.h:23 — representative boundary
#ifndef VideoSource_h
@property(nonatomic, readonly) float frameRate;
#endif /* VideoSource_h */Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Feature and framework orchestration | CommandOptions |
The sample keeps the demonstrated path in its primary concrete type. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Actor isolation | Swift/VTEncoderUtils/VideoSource.swift:229 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
participant AsyncIterator
participant sampleBufferQueue
participant SampleBufferQueue
participant source
AsyncIterator->>sampleBufferQueue: dequeue()
AsyncIterator->>SampleBufferQueue: SampleBufferQueue()
AsyncIterator->>source: sourceTrack()
AsyncIterator->>AsyncIterator: next()
Reference code
Swift/VTEncoderUtils/VideoSource.swift:294 — next()
public mutating func next() async throws -> (CVImageBuffer, CMTime)? {
if let sampleBufferQueue = self.sampleBufferQueue {
guard framesEmitted < self.frameCount else { return nil }
guard let sbuf = await sampleBufferQueue.dequeue() else {
return nil
}
guard let imageBuffer = sbuf.imageBuffer else {
return nil
}
framesEmitted += 1
return (imageBuffer, sbuf.presentationTimeStamp)
} else {
let sampleBufferQueue = try await SampleBufferQueue(sourceAsset: source.sourceAsset,
videoTrack: try await source.sourceTrack,
outputPixelFormat: source.outputPixelFormat,
alwaysCopiesSampleData: source.alwaysCopiesSampleData,
frameCount: self.frameCount)
self.sampleBufferQueue = sampleBufferQueue
return try await self.next()
}
}Naming conventions
- Types: feature-specific names rather than reusable layer suffixes.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
validate,run,frames,enqueue,dequeue,wait,signal,next. - Files:
Swift/VTEncoderForTranscoding/CommandOptions.swift,Swift/VTEncoderUtils/VideoSource.swift,Swift/VTEncoderUtils/RealTimeVideoSource.swift,Objective-C/VTEncoderUtils/VideoSource.h,Objective-C/VTEncoderUtils/VideoSource.m,Objective-C/VTEncoderUtils/VideoSink.h.
Architecture takeaways
CommandOptionsis the main source-visible entry or composition anchor for this sample.- Framework work reaches AVFoundation, VideoToolbox, ArgumentParser through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Swift/VTEncoderForTranscoding/CommandOptions.swift |
Cited implementation, CommandOptions, async declaration or closure, ArgumentParser, CodecType, PixelFormatType, PresetType, ProfileType, QPModulationLevel |
Objective-C/VTEncoderUtils/VideoSource.h |
frameRate, width, height, VideoSource, RealTimeVideoSource |
Swift/VTEncoderForTranscoding/VTEncoderForTranscoding.swift |
Cited implementation, Sendable or @Sendable, Task, EncodePreset, CodecProfile, Options |
Swift/VTEncoderUtils/VideoSource.swift |
Semaphore, actor, VideoSource, SourceInfo, VideoSourceFrames, SampleBufferQueue, AsyncIterator |
Swift/VTEncoderUtils/RealTimeVideoSource.swift |
DispatchQueue(label:), RealTimeVideoSource, RealTimeVideoSourceFrames, LazyState, AsyncIterator |
Objective-C/VTEncoderForTranscoding/VTEncoderForTranscoding.m |
AVFoundation, VideoToolbox, Feature implementation |
Objective-C/VTEncoderUtils/VTEncoderUtil.m |
string.h, Feature implementation |
Objective-C/VTEncoderForTranscoding/main.m |
Feature implementation |
Objective-C/VTEncoderUtils/VideoSource.m |
VideoSource, RealTimeVideoSource |
Objective-C/VTEncoderUtils/VideoSink.h |
VideoSink |
Objective-C/VTEncoderUtils/VideoSink.m |
VideoSink |
Swift/VTEncoderUtils/VTEncoderUtil.swift |
RuntimeError |
Swift/VTEncoderUtils/VideoSink.swift |
VideoSink |
Objective-C/VTEncoderForTranscoding/VTEncoderForTranscoding.h |
Feature implementation |
Objective-C/VTEncoderUtils/VTEncoderUtil.h |
Feature implementation |