AVFormatContext

public final class AVFormatContext

Format I/O context.

  • Create an AVFormatContext.

    Declaration

    Swift

    public init()
  • url

    Input or output URL.

    • demuxing: Set by openInput(_ url:format:options:), initialized to an empty string if url parameter was nil in openInput(_ url:format:options:).
    • muxing: May be set by the caller before calling writeHeader(options:) to a string. Set to an empty string if it was nil in writeHeader(options:).

    Declaration

    Swift

    public var url: String? { get set }
  • pb

    I/O context.

    • demuxing: Either set by the user before openInput(_ url:format:options:) (then the user must close it manually) or set by openInput(_ url:format:options:).
    • muxing: Set by the user before writeHeader(options:). The caller must take care of closing the IO context.

    Declaration

    Swift

    public var pb: AVIOContext? { get set }
  • The number of streams in the file.

    Declaration

    Swift

    public var streamCount: Int { get }
  • A list of all streams in the file. New streams are created with addStream(codec:).

    • demuxing: Streams are created by libavformat in openInput(_ url:format:options:). If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams may also appear in readFrame(into:).
    • muxing: Streams are created by the user before writeHeader(options:).

    Declaration

    Swift

    public var streams: [AVStream] { get }
  • The first video stream in the file.

    Declaration

    Swift

    public var videoStream: AVStream? { get }
  • The first audio stream in the file.

    Declaration

    Swift

    public var audioStream: AVStream? { get }
  • The first subtitle stream in the file.

    Declaration

    Swift

    public var subtitleStream: AVStream? { get }
  • The flags used to modify the (de)muxer behaviour.

    • demuxing: Set by the caller before openInput(_ url:format:options:).
    • muxing: Set by the caller before writeHeader(options:).

    Declaration

    Swift

    public var flags: Flag { get set }
  • Metadata that applies to the whole file.

    • demuxing: Set by libavformat in openInput(_ url:format:options:).
    • muxing: May be set by the caller before writeHeader(options:).

    Declaration

    Swift

    public var metadata: [String : String] { get set }
  • Custom interrupt callbacks for the I/O layer.

    • demuxing: Set by the user before openInput(_ url:format:options:).
    • muxing: Set by the user before writeHeader(options:) (mainly useful for AVOutputFormat.Flag.noFile formats). The callback should also be passed to avio_open2() if it’s used to open the file.

    Declaration

    Swift

    public var interruptCallback: AVIOInterruptCallback { get set }
  • The first stream index for the specified media type.

    Declaration

    Swift

    public func streamIndex(for mediaType: AVMediaType) -> Int?

    Parameters

    mediaType

    media type

    Return Value

    stream index if it exists

  • Print detailed information about the input or output format, such as duration, bitrate, streams, container, programs, metadata, side data, codec and timebase.

    Declaration

    Swift

    public func dumpFormat(url: String? = nil, isOutput: Bool = false)

    Parameters

    url

    the URL to print, such as source or destination file

    isOutput

    Select whether the specified context is an input(false) or output(true).

  • Send control message from application to device.

    Throws

    • AVError.noSystem when device doesn’t implement handler of the message.
    • othrer errors

    Declaration

    Swift

    public func sendMessageToDevice(
        type: AVAppToDevMessageType,
        data: UnsafeMutableRawBufferPointer?
    ) throws

    Parameters

    type

    message type.

    data

    message data. Exact type depends on message type.

  • Send control message from device to application.

    Throws

    • AVError.noSystem when device doesn’t implement handler of the message.
    • othrer errors

    Declaration

    Swift

    public func sendMessageToApplication(
        type: AVDevToAppMessageType,
        data: UnsafeMutableRawBufferPointer?
    ) throws

    Parameters

    type

    message type.

    data

    message data. Can be nil.

  • Flags used to modify the (de)muxer behaviour.

    See more

    Declaration

    Swift

    public struct Flag : OptionSet
  • Open an input stream and read the header. The codecs are not opened.

    Throws

    AVError

    Declaration

    Swift

    public convenience init(
        url: String,
        format: AVInputFormat? = nil,
        options: [String: String]? = nil
    ) throws

    Parameters

    url

    URL of the stream to open.

    format

    If non-nil, this parameter forces a specific input format. Otherwise the format is autodetected.

    options

    A dictionary filled with AVFormatContext and demuxer-private options.

  • The input container format.

    Declaration

    Swift

    public var inputFormat: AVInputFormat? { get set }
  • Position of the first frame of the component, in AVTimestamp.timebase fractional seconds.

    Declaration

    Swift

    public var startTime: Int64 { get }
  • Duration of the stream, in AVTimestamp.timebase fractional seconds.

    Declaration

    Swift

    public var duration: Int64 { get }
  • Total stream bitrate in bit/s, 0 if not available.

    Declaration

    Swift

    public var bitRate: Int64 { get }
  • The size of the file.

    Declaration

    Swift

    public var size: Int64 { get }
  • Open an input stream and read the header.

    Throws

    AVError

    Declaration

    Swift

    public func openInput(
        _ url: String? = nil,
        format: AVInputFormat? = nil,
        options: [String: String]? = nil
    ) throws

    Parameters

    url

    URL of the stream to open.

    • url: URL of the stream to open.
    • format: If non-nil, this parameter forces a specific input format. Otherwise the format is autodetected.
    • options: A dictionary filled with AVFormatContext and demuxer-private options.

  • Read packets of a media file to get stream information.

    This is useful for file formats with no headers such as MPEG. This function also computes the real framerate in case of MPEG-2 repeat frame mode. The logical file position is not changed by this function; examined packets may be buffered for later processing.

    Note

    This function isn’t guaranteed to open all the codecs, so options being non-empty at return is a perfectly normal behavior.

    Throws

    AVError

    Declaration

    Swift

    public func findStreamInfo(options: [[String: String]]? = nil) throws

    Parameters

    options

    If non-NULL, an streamCount long array of pointers to dictionaries, where i-th member contains options for codec corresponding to i-th stream. On return each dictionary will be filled with options that were not found.

  • Find the best stream in the file.

    Declaration

    Swift

    public func findBestStream(
        type: AVMediaType,
        wantedStreamIndex: Int = -1,
        relatedStreamIndex: Int = -1
    ) -> Int?

    Parameters

    type

    stream type

    wantedStreamIndex

    user-requested stream index, or -1 for automatic selection

    relatedStreamIndex

    try to find a stream related (eg. in the same program) to this one, or -1 if none

    Return Value

    stream index if it exists

  • Guess the sample aspect ratio of a frame, based on both the stream and the frame aspect ratio.

    Since the frame aspect ratio is set by the codec but the stream aspect ratio is set by the demuxer, these two may not be equal. This function tries to return the value that you should use if you would like to display the frame.

    Basic logic is to use the stream aspect ratio if it is set to something sane otherwise use the frame aspect ratio. This way a container setting, which is usually easy to modify can override the coded value in the frames.

    Declaration

    Swift

    public func guessSampleAspectRatio(stream: AVStream?, frame: AVFrame? = nil) -> AVRational

    Parameters

    stream

    the stream which the frame is part of

    frame

    the frame with the aspect ratio to be determined

    Return Value

    the guessed (valid) sample aspect ratio, 0/1 if no idea

  • Guess the frame rate, based on both the container and codec information.

    Declaration

    Swift

    public func guessFrameRate(stream: AVStream, frame: AVFrame? = nil) -> AVRational

    Parameters

    stream

    the stream which the frame is part of

    frame

    the frame for which the frame rate should be determined

    Return Value

    the guessed (valid) frame rate, 0/1 if no idea

  • Return the next frame of a stream.

    This function returns what is stored in the file, and does not validate that what is there are valid frames for the decoder. It will split what is stored in the file into frames and return one for each call. It will not omit invalid data between valid frames so as to give the decoder the maximum information possible for decoding.

    Throws

    AVError

    Declaration

    Swift

    public func readFrame(into packet: AVPacket) throws

    Parameters

    packet

    the packet used to store data

  • Seek to the keyframe at timestamp.

    Throws

    AVError

    Declaration

    Swift

    public func seekFrame(to timestamp: Int64, streamIndex: Int, flags: SeekFlag) throws

    Parameters

    timestamp

    Timestamp in AVStream.timebase units or, if no stream is specified, in AVTimestamp.timebase units.

    streamIndex

    If streamIndex is -1, a default stream is selected, and timestamp is automatically converted from AVTimestamp.timebase units to the stream specific timebase.

    flags

    flags which select direction and seeking mode

  • Discard all internally buffered data. This can be useful when dealing with discontinuities in the byte stream. Generally works only with formats that can resync. This includes headerless formats like MPEG-TS/TS but should also work with NUT, Ogg and in a limited way AVI for example.

    The set of streams, the detected duration, stream parameters and codecs do not change when calling this function. If you want a complete reset, it’s better to open a new AVFormatContext.

    This does not flush the AVIOContext (pb). If necessary, call pb.flush before calling this function.

    Declaration

    Swift

    public func flush()
  • Start playing a network-based stream (e.g. RTSP stream) at the current position.

    Throws

    AVError

    Declaration

    Swift

    public func play() throws
  • Pause a network-based stream (e.g. RTSP stream).

    Use play to resume it.

    Throws

    AVError

    Declaration

    Swift

    public func pause() throws
  • Allocate an AVFormatContext for an output format.

    Throws

    AVError

    Declaration

    Swift

    public convenience init(
        format: AVOutputFormat?,
        formatName: String? = nil,
        filename: String? = nil
    ) throws

    Parameters

    format

    the format to use for allocating the context, if nil formatName and filename are used instead

    formatName

    the name of output format to use for allocating the context, if nil filename is used instead

    filename

    the name of the filename to use for allocating the context, may be nil

  • The output container format.

    Declaration

    Swift

    public var outputFormat: AVOutputFormat? { get set }
  • Create and initialize a AVIOContext for accessing the resource indicated by url.

    Throws

    AVError

    Declaration

    Swift

    public func openOutput(url: String, flags: AVIOContext.Flag) throws

    Parameters

    url

    resource to access

    flags

    flags which control how the resource indicated by url is to be opened

  • Add a new stream to a media file.

    Declaration

    Swift

    public func addStream(codec: AVCodec? = nil) -> AVStream?

    Parameters

    codec

    If non-nil, the AVCodecContext corresponding to the new stream will be initialized to use this codec. This is needed for e.g. codec-specific defaults to be set, so codec should be provided if it is known.

    Return Value

    newly created stream or nil on error.

  • Allocate the stream private data and write the stream header to an output media file.

    Note

    The outputFormat field must be set to the desired output format; The pb field must be set to an already opened AVIOContext.

    Throws

    AVError

    Declaration

    Swift

    public func writeHeader(options: [String: String]? = nil) throws

    Parameters

    options

    the AVFormatContext and muxer-private options

  • Write a packet to an output media file.

    This function passes the packet directly to the muxer, without any buffering or reordering. The caller is responsible for correctly interleaving the packets if the format requires it. Callers that want libavformat to handle the interleaving should call AVFormatContext.interleavedWriteFrame(_:) instead of this function.

    This parameter can be nil (at any time, not just at the end), in order to immediately flush data buffered within the muxer, for muxers that buffer up data internally before writing it to the output.

    Packet’s AVPacket.streamIndex field must be set to the index of the corresponding stream in streams.

    The timestamps (AVPacket.pts, AVPacket.dts) must be set to correct values in the stream’s timebase (unless the output format is flagged with the AVOutputFormat.Flag.noTimestamps flag, then they can be set to AVTimestamp.noPTS). The dts for subsequent packets passed to this function must be strictly increasing when compared in their respective timebases (unless the output format is flagged with the AVOutputFormat.Flag.tsNonstrict, then they merely have to be nondecreasing). AVPacket.duration should also be set if known.

    Throws

    AVError

    Declaration

    Swift

    public func writeFrame(_ pkt: AVPacket?) throws

    Parameters

    pkt

    The packet containing the data to be written. Note that unlike AVFormatContext.interleavedWriteFrame(_:), this function does not take ownership of the packet passed to it (though some muxers may make an internal reference to the input packet).

  • Write a packet to an output media file ensuring correct interleaving.

    This function will buffer the packets internally as needed to make sure the packets in the output file are properly interleaved in the order of increasing dts. Callers doing their own interleaving should call AVFormatContext.writeFrame(_:) instead of this function.

    Using this function instead of AVFormatContext.writeFrame(_:) can give muxers advance knowledge of future packets, improving e.g. the behaviour of the mp4 muxer for VFR content in fragmenting mode.

    If the packet is reference-counted, this function will take ownership of this reference and unreference it later when it sees fit. The caller must not access the data through this reference after this function returns. If the packet is not reference-counted, libavformat will make a copy.

    This parameter can be nil (at any time, not just at the end), to flush the interleaving queues.

    Packet’s AVPacket.streamIndex field must be set to the index of the corresponding stream in streams.

    The timestamps (AVPacket.pts, AVPacket.dts) must be set to correct values in the stream’s timebase (unless the output format is flagged with the AVOutputFormat.Flag.noTimestamps flag, then they can be set to AVTimestamp.noPTS). The dts for subsequent packets in one stream must be strictly increasing (unless the output format is flagged with the AVOutputFormat.Flag.tsNonstrict, then they merely have to be nondecreasing). AVPacket.duration should also be set if known.

    Throws

    AVError

    See also

    writeFrame

    Declaration

    Swift

    public func interleavedWriteFrame(_ pkt: AVPacket?) throws

    Parameters

    pkt

    The packet containing the data to be written.

  • Write the stream trailer to an output media file and free the file private data.

    May only be called after a successful call to writeHeader(options:).

    Throws

    AVError

    Declaration

    Swift

    public func writeTrailer() throws
  • Undocumented

    Declaration

    Swift

    public static let `class`: AVClass
  • Undocumented

    Declaration

    Swift

    public func withUnsafeClassObjectPointer<T>(_ body: (UnsafeMutableRawPointer) throws -> T) rethrows -> T
  • Undocumented

    Declaration

    Swift

    public func withUnsafeObjectPointer<T>(_ body: (UnsafeMutableRawPointer) throws -> T) rethrows -> T