Streams¶
Stream collections¶
- class av.container.streams.StreamContainer¶
Bases:
object
A tuple-like container of
Stream
.# There are a few ways to pulling out streams. first = container.streams[0] video = container.streams.video[0] audio = container.streams.get(audio=(0, 1))
Dynamic Slicing¶
- StreamContainer.get(streams=None, video=None, audio=None, subtitles=None, data=None)¶
Get a selection of
Stream
as alist
.Positional arguments may be
int
(which is an index into the streams), orlist
ortuple
of those:# Get the first channel. streams.get(0) # Get the first two audio channels. streams.get(audio=(0, 1))
Keyword arguments (or dicts as positional arguments) as interpreted as
(stream_type, index_value_or_set)
pairs:# Get the first video channel. streams.get(video=0) # or streams.get({'video': 0})
Stream
objects are passed through untouched.If nothing is selected, then all streams are returned.
Typed Collections¶
These attributes are preferred for readability if you don’t need the
dynamic capabilities of get()
:
- StreamContainer.video¶
A tuple of
VideoStream
.
- StreamContainer.audio¶
A tuple of
AudioStream
.
- StreamContainer.subtitles¶
A tuple of
SubtitleStream
.
- StreamContainer.data¶
A tuple of
DataStream
.
- StreamContainer.other¶
A tuple of
Stream
Streams¶
- class av.stream.Stream¶
Bases:
object
A single stream of audio, video or subtitles within a
Container
.>>> fh = av.open(video_path) >>> stream = fh.streams.video[0] >>> stream <av.VideoStream #0 h264, yuv420p 1280x720 at 0x...>
This encapsulates a
CodecContext
, located atStream.codec_context
. Attribute access is passed through to that context when attributes are missing on the stream itself. E.g.stream.options
will be the options on the context.
Transcoding¶
- Stream.encode(frame=None)¶
Encode an
AudioFrame
orVideoFrame
and return a list ofPacket
.See also
This is mostly a passthrough to
CodecContext.encode()
.
- Stream.decode(packet=None)¶
Decode a
Packet
and return a list ofAudioFrame
orVideoFrame
.See also
This is mostly a passthrough to
CodecContext.decode()
.
Timing¶
See also
Time for a discussion of time in general.
- Stream.time_base¶
The unit of time (in fractional seconds) in which timestamps are expressed.
- Type
Fraction
orNone
- Stream.start_time¶
The presentation timestamp in
time_base
units of the first frame in this stream.- Type
int
orNone
Frame Rates¶
These attributes are different ways of calculating frame rates.
Since containers don’t need to explicitly identify a frame rate, nor even have a static frame rate, these attributes are not guaranteed to be accurate. You must experiment with them with your media to see which ones work for you for your purposes.
Whenever possible, we advise that you use raw timing instead of frame rates.
- Stream.average_rate¶
The average frame rate of this video stream.
This is calculated when the file is opened by looking at the first few frames and averaging their rate.
- Type
Fraction
orNone
- Stream.base_rate¶
The base frame rate of this stream.
This is calculated as the lowest framerate at which the timestamps of frames can be represented accurately. See AVStream.r_frame_rate for more.
- Type
Fraction
orNone
- Stream.guessed_rate¶
The guessed frame rate of this stream.
This is a wrapper around av_guess_frame_rate, and uses multiple heuristics to decide what is “the” frame rate.
- Type
Fraction
orNone