Media
An input media file opened for reading: container metadata plus typed stream views.
Probing is eager — open() runs avformat_open_input + avformat_find_stream_info,
so metadata and streams are available immediately.
Media owns the input AVFormatContext. The stream views it hands out borrow from it and
hold a refcount on the Media, so the file stays open as long as any view is alive.
Properties
Section titled “Properties”All readonly.
| Property | Type | Description |
|---|---|---|
$duration | float | Container duration in seconds (0.0 if unknown). |
$format | string | Demuxer/container short name, e.g. "mov,mp4,m4a,3gp,3g2,mj2". |
$bitrate | int | Container bit rate in bits/sec. |
Methods
Section titled “Methods”static open(string $path): Media
Section titled “static open(string $path): Media”Open and probe a media file.
- Throws
FileNotFoundExceptionif$pathdoes not exist. - Throws
InvalidFormatExceptionif the file is not probeable media.
streams(): list<VideoStream|AudioStream>
Section titled “streams(): list<VideoStream|AudioStream>”All video and audio streams as typed views, in container order.
videoStream(int $index = 0): ?VideoStream
Section titled “videoStream(int $index = 0): ?VideoStream”The nth video stream (0-based among video streams), or null.
audioStream(int $index = 0): ?AudioStream
Section titled “audioStream(int $index = 0): ?AudioStream”The nth audio stream (0-based among audio streams), or null.
from(float $seconds): static · to(float $seconds): static · take(float $seconds): static
Section titled “from(float $seconds): static · to(float $seconds): static · take(float $seconds): static”Open the file as a slice of itself — a fast input seek applied to the whole input, so video
and audio stay in sync. from() sets the start; bound the end with to() (absolute timestamp) or
take() (duration — sugar for to(), named take to avoid the $duration property). Immutable:
each returns a new windowed Media.
The window resets the timeline — the slice starts at t = 0, $duration reports its own
length, and filters, frameAt, and the expression variable t
all speak the slice’s time. Frames outside it don’t exist to your code.
$clip = Media::open('lecture.mp4')->from(120.0)->take(60.0); // from 2:00, for one minuteExample
Section titled “Example”use FFmpeg\Media;
$media = Media::open('clip.mkv');printf("%s — %.2fs @ %d bps\n", $media->format, $media->duration, $media->bitrate);
foreach ($media->streams() as $s) { echo $s::class, ' ', $s->codec, "\n";}See also
Section titled “See also”An Artisan Build project.
Built on FFmpeg — an independent binding, not affiliated with or endorsed by the FFmpeg project. Support FFmpeg.