FragmentedMP4Writer

public class FragmentedMP4Writer

A class that reads in audio and video samples and produces fragmented mp4’s and playlists

Usage Example:

// URL to a temp dir
let outputDir   = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let writer      = FragmentedMP4Writer(outputDir) // setup the writer without any options
writer.configure(settings: videoSettings) // Pass in video settings config
writer.configure(settings: audioSettings) // Pass in audio settings confg

// Specify a hls vod style playlist (file will appear in outputDir/vod.m3u8)
let playlist = Playlist(type: .hls_vod, fileName: "vod.m3u8")
try writer.add(playlist: playlist)

// Append some samples
writer.append(compressedSample)

// Signal when complete.  All queued samples in the writer will be immediately flushed.
writer.stop()
  • Create a new writer

    Throws

    Will throw errors if any problems with setup arise

    Declaration

    Swift

    public init(_ outputDir: URL,
                targetDuration: TimeInterval = 6,
                streamType: StreamType = [.video, .audio],
                delegate: FileWriterDelegate? = nil) throws

    Parameters

    outputDir

    A URL on the local filesystem that playlists and fragmented mp4’s should be written to

    targetDuration

    The target duration that each segment should be

    streamType

    Type of stream that is being read. eg: audio/video, video only, audio only

    delegate

    Delegate that is notified when files are written or updated

  • Configure the writer with video settings. This happens after initial setup because a video source might not be known / available

    Declaration

    Swift

    public func configure(settings: VideoSettings)

    Parameters

    settings

    VideoSettings struct describing video portion of the stream

  • Configure the writer with audio settings This happens after initial setup because an audio source might not be known / available

    Declaration

    Swift

    public func configure(settings: AudioSettings)

    Parameters

    settings

    AudioSettings struct describing the audio portion of the stream

  • Append a sample for writing

    Declaration

    Swift

    public func append(sample: CompressedSample)

    Parameters

    sample

    Sample data that should be written to the stream

    type

    Flag indicating whether the sample is audio or video

  • Add another playlist to be generated. The mp4 writer can generate multiple playlists at once

    Throws

    Throws a writer error if the playlist could not be added

    Declaration

    Swift

    public func add(playlist: Playlist) throws

    Parameters

    playlist

    Playlist to be generated

  • Appends and end tag for now

    Declaration

    Swift

    public func stop()