media¶
Read videos as frames -- video files (PyAV), image sequences (Pillow), and the parts of one split video read as one stream -- behind one backend-agnostic reader interface, and write frames back out as an MP4.
Read recordings as frames: video files (PyAV) and image sequences (Pillow).
open_reader resolves a source to its
FrameReader once (ADR-0005); a consumer then
indexes it or streams it without knowing the kind of source. A new decode backend is
a FrameReader subclass and one entry in _VIDEO_BACKENDS.
reader = media.open_reader("clip.mp4")
block = reader[0:64] # (64, H, W, 3) uint8
for b in media.prefetch(reader.stream_blocks(gray_ok=True)):
model(b)
ConcatReader ¶
Bases: FrameReader
The parts of one video, read as a single stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
The parts, in decode order, two or more. |
required | |
factory
|
Callable[[Path], FrameReader]
|
Builds a part's reader from its path: the video backend. |
required |
counts
|
list[int] | None
|
Each part's exact frame count if already known, which skips the measuring decode. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
paths |
The parts, in decode order. |
Source code in src/deeperfly/io/media/concat.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/concat.py
__init__ ¶
__init__(
paths,
*,
factory: Callable[[Path], FrameReader],
counts: list[int] | None = None,
) -> None
Open the parts and build the global index (measuring counts if needed).
Source code in src/deeperfly/io/media/concat.py
__repr__ ¶
close ¶
count ¶
fps ¶
hold_open ¶
This stream with each part held open, and returns itself.
A part opens its decoder on its first read, so a viewer opens only the parts it reads.
Source code in src/deeperfly/io/media/concat.py
release ¶
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Walk the parts in order, forward, never seeking across a boundary.
The inherited stream_blocks then never breaks a block at a part boundary.
Source code in src/deeperfly/io/media/concat.py
FrameReader ¶
Bases: ABC
Reads one source as (H, W, 3) uint8 RGB frames, on the CPU.
reader[5] is one frame; reader[[0, 3, 5]] and reader[2:8:2] are
(T, H, W, 3) in the order asked; stream_frames and stream_blocks iterate
lazily and take gray_ok. A reader is a context manager, and close is a no-op
for the stateless ones.
Source code in src/deeperfly/io/media/reader.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
__enter__ ¶
__exit__ ¶
__getitem__
abstractmethod
¶
close ¶
count
abstractmethod
¶
fps ¶
hold_open ¶
This source again, with its decoder held open between reads.
For a viewer, which would otherwise pay a re-open and a seek per frame. The
base returns self and a backend overrides it. The caller owns the result,
which may be this same object, and closes it. gray_ok lets indexing one frame
return (H, W) for a source that carries no color.
Source code in src/deeperfly/io/media/reader.py
release ¶
Let go of what a held-open reader holds between reads; it reopens on demand.
For a viewer that keeps readers of footage no longer on screen. The base holds nothing, so there is nothing to let go of.
Source code in src/deeperfly/io/media/reader.py
stream_blocks ¶
stream_blocks(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
block_size: int = 64,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "T H W C"]]
Yield (T, H, W, C) uint8 blocks (T <= block_size) from one decode.
The default buffers stream_frames into blocks, with its arguments; a backend
with a cheaper block decode overrides it.
Source code in src/deeperfly/io/media/reader.py
stream_frames
abstractmethod
¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Yield (H, W, C) uint8 frames from one forward decode.
start, stop and step are as for range. gray_ok permits a single
channel, kept as (H, W, 1), for a frame that carries no color, which skips the
color conversion; a source that cannot tell cheaply returns RGB.
thread_count caps the decode threads, worth doing when several sources decode
at once.
Source code in src/deeperfly/io/media/reader.py
ImageSequenceReader ¶
Bases: FrameReader
Reads an ordered image sequence (a directory, glob, or explicit file list).
Files decode in parallel on up to workers threads; gray_ok and thread_count
are accepted and ignored.
Source code in src/deeperfly/io/media/images.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/images.py
__init__ ¶
Wrap an ordered list of image files, decoded on up to workers threads.
count ¶
from_pattern
classmethod
¶
A reader for a directory or glob, its files sorted by name.
stream_blocks ¶
stream_blocks(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
block_size: int = 64,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "T H W 3"]]
Yield (T, H, W, 3) uint8 blocks, each decoded in parallel across files.
Source code in src/deeperfly/io/media/images.py
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W 3"]]
Yield (H, W, 3) uint8 frames, one per file, decoded a block at a time.
Source code in src/deeperfly/io/media/images.py
PyavHeldReader ¶
Bases: PyavVideoReader
One video file with the decoder held open between single-frame reads.
A frame a short way ahead is decoded on from the live decode, a frame among the
last _KEPT_FRAMES decoded -- a seek's approach included -- is looked up, and any
other jump seeks the open container; a list, a slice and the streams take the
stateless paths. release lets go of the decoder until the next read, and a
reader dropped without close closes its container as it goes. A container that
cannot be opened or has no usable timestamps is logged once and read statelessly.
Every read takes a lock, since an av container cannot decode from two threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The video file. |
required |
gray_ok
|
bool
|
Whether a frame with no color may come back single-channel. |
False
|
thread_count
|
int | None
|
Decode threads; |
None
|
Source code in src/deeperfly/io/media/pyav.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__getitem__ ¶
Decode frames; one frame comes off the held decoder, the rest inherited.
Source code in src/deeperfly/io/media/pyav.py
__init__ ¶
Note what to open on the first read; open nothing yet.
Source code in src/deeperfly/io/media/pyav.py
close ¶
hold_open ¶
release ¶
Let go of the decoder and the kept frames; the next read opens them again.
Waits for a read in flight to finish, as close does.
PyavVideoReader ¶
Bases: FrameReader
Frame-accurate decode of one video file via PyAV.
A forward read seeks to its start's keyframe and decodes on from there, and a
single index or a list seeks per target; count and fps read the container
header. Every method opens and closes the
file.
Source code in src/deeperfly/io/media/pyav.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/pyav.py
__init__ ¶
count ¶
The container header's frame count, or None; never duration * fps.
Source code in src/deeperfly/io/media/pyav.py
fps ¶
Average frame rate from the container header, or None if absent.
Source code in src/deeperfly/io/media/pyav.py
hold_open ¶
The same file as a PyavHeldReader.
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Yield (H, W, C) uint8 frames from one forward decode.
Source code in src/deeperfly/io/media/pyav.py
VideoWriter ¶
Incremental H.264 (libx264) MP4 encoder, backed by PyAV.
The file opens on the first frame, whose size (rounded down to even for yuv420p)
sets the encode dimensions; later frames are cropped to it, and non-uint8 input is
clipped to [0, 255].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file. |
required |
fps
|
float
|
Frame rate written into the container. |
30.0
|
codec
|
str | None
|
Encoder, or |
None
|
pix_fmt
|
str
|
Pixel format; |
'yuv420p'
|
options
|
Mapping[str, str] | None
|
Private encoder options as FFmpeg spells them
( |
None
|
Source code in src/deeperfly/io/media/writer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__enter__ ¶
__exit__ ¶
__init__ ¶
__init__(
path: str | Path,
fps: float = 30.0,
*,
codec: str | None = None,
pix_fmt: str = "yuv420p",
options: Mapping[str, str] | None = None,
) -> None
Prepare a writer for path (no file is opened until the first frame).
Source code in src/deeperfly/io/media/writer.py
close ¶
Flush the encoder and close the file (idempotent).
Source code in src/deeperfly/io/media/writer.py
write_frame ¶
Append one (H, W, 3) RGB frame (NumPy, or a torch or DLPack array).
Source code in src/deeperfly/io/media/writer.py
write_frames ¶
Append a (T, H, W, 3) batch, or any iterable of frames or blocks.
Source code in src/deeperfly/io/media/writer.py
is_video_file ¶
Whether path is an existing video file (decoded as video, not an image).
list_image_files ¶
Sorted image files for a directory or glob pattern, ordered by name.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If nothing matches |
Source code in src/deeperfly/io/media/images.py
open_reader ¶
open_reader(
source: str | Path | list[Path],
*,
backend: str = DEFAULT_BACKEND,
workers: int | None = None,
) -> FrameReader
Open the right FrameReader for a source.
A video file opens with the video backend, several video files (the parts of one
split video) as a ConcatReader, and a
directory, glob or list of images as an
ImageSequenceReader decoding with
workers threads.
Source code in src/deeperfly/io/media/__init__.py
The backend-agnostic frame-reading interface, FrameReader (ADR-0005).
A reader is stateless by default: it opens the source, decodes and lets go, so any
number of callers may index one. A viewer, reading single frames in its own order,
asks for hold_open instead and gets
a reader that keeps its decoder between reads. A backend implements __getitem__,
stream_frames and count; the base class builds the rest on a forward decode.
FrameReader ¶
Bases: ABC
Reads one source as (H, W, 3) uint8 RGB frames, on the CPU.
reader[5] is one frame; reader[[0, 3, 5]] and reader[2:8:2] are
(T, H, W, 3) in the order asked; stream_frames and stream_blocks iterate
lazily and take gray_ok. A reader is a context manager, and close is a no-op
for the stateless ones.
Source code in src/deeperfly/io/media/reader.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
__enter__ ¶
__exit__ ¶
__getitem__
abstractmethod
¶
close ¶
count
abstractmethod
¶
fps ¶
hold_open ¶
This source again, with its decoder held open between reads.
For a viewer, which would otherwise pay a re-open and a seek per frame. The
base returns self and a backend overrides it. The caller owns the result,
which may be this same object, and closes it. gray_ok lets indexing one frame
return (H, W) for a source that carries no color.
Source code in src/deeperfly/io/media/reader.py
release ¶
Let go of what a held-open reader holds between reads; it reopens on demand.
For a viewer that keeps readers of footage no longer on screen. The base holds nothing, so there is nothing to let go of.
Source code in src/deeperfly/io/media/reader.py
stream_blocks ¶
stream_blocks(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
block_size: int = 64,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "T H W C"]]
Yield (T, H, W, C) uint8 blocks (T <= block_size) from one decode.
The default buffers stream_frames into blocks, with its arguments; a backend
with a cheaper block decode overrides it.
Source code in src/deeperfly/io/media/reader.py
stream_frames
abstractmethod
¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Yield (H, W, C) uint8 frames from one forward decode.
start, stop and step are as for range. gray_ok permits a single
channel, kept as (H, W, 1), for a frame that carries no color, which skips the
color conversion; a source that cannot tell cheaply returns RGB.
thread_count caps the decode threads, worth doing when several sources decode
at once.
Source code in src/deeperfly/io/media/reader.py
The PyAV video backend: frame-accurate decode of one video file (ADR-0006).
PyavVideoReader decodes to uint8 RGB with
per-target seeks; PyavHeldReader keeps the
decoder open for a viewer, and the frames it last decoded. With gray_ok, a
full-range frame with flat chroma (every fly-rig camera records grayscale as YUV)
comes back as its luma plane, which skips the YUV->RGB conversion, 100-300x the plane
copy's cost. TV range and a frame with color decode as RGB; the test is per frame,
since a color video can open on black.
PyavHeldReader ¶
Bases: PyavVideoReader
One video file with the decoder held open between single-frame reads.
A frame a short way ahead is decoded on from the live decode, a frame among the
last _KEPT_FRAMES decoded -- a seek's approach included -- is looked up, and any
other jump seeks the open container; a list, a slice and the streams take the
stateless paths. release lets go of the decoder until the next read, and a
reader dropped without close closes its container as it goes. A container that
cannot be opened or has no usable timestamps is logged once and read statelessly.
Every read takes a lock, since an av container cannot decode from two threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The video file. |
required |
gray_ok
|
bool
|
Whether a frame with no color may come back single-channel. |
False
|
thread_count
|
int | None
|
Decode threads; |
None
|
Source code in src/deeperfly/io/media/pyav.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__getitem__ ¶
Decode frames; one frame comes off the held decoder, the rest inherited.
Source code in src/deeperfly/io/media/pyav.py
__init__ ¶
Note what to open on the first read; open nothing yet.
Source code in src/deeperfly/io/media/pyav.py
close ¶
hold_open ¶
release ¶
Let go of the decoder and the kept frames; the next read opens them again.
Waits for a read in flight to finish, as close does.
PyavVideoReader ¶
Bases: FrameReader
Frame-accurate decode of one video file via PyAV.
A forward read seeks to its start's keyframe and decodes on from there, and a
single index or a list seeks per target; count and fps read the container
header. Every method opens and closes the
file.
Source code in src/deeperfly/io/media/pyav.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/pyav.py
__init__ ¶
count ¶
The container header's frame count, or None; never duration * fps.
Source code in src/deeperfly/io/media/pyav.py
fps ¶
Average frame rate from the container header, or None if absent.
Source code in src/deeperfly/io/media/pyav.py
hold_open ¶
The same file as a PyavHeldReader.
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Yield (H, W, C) uint8 frames from one forward decode.
Source code in src/deeperfly/io/media/pyav.py
Several video files read as one stream: ConcatReader.
A long capture split across files is one stream with a global frame index. Its
failure mode is silent, a boundary off by one shifting every later frame, so every
part's count is exact (decoded when the header has none, never duration * fps) and
the parts must agree on frame size and rate.
ConcatReader ¶
Bases: FrameReader
The parts of one video, read as a single stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
The parts, in decode order, two or more. |
required | |
factory
|
Callable[[Path], FrameReader]
|
Builds a part's reader from its path: the video backend. |
required |
counts
|
list[int] | None
|
Each part's exact frame count if already known, which skips the measuring decode. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
paths |
The parts, in decode order. |
Source code in src/deeperfly/io/media/concat.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/concat.py
__init__ ¶
__init__(
paths,
*,
factory: Callable[[Path], FrameReader],
counts: list[int] | None = None,
) -> None
Open the parts and build the global index (measuring counts if needed).
Source code in src/deeperfly/io/media/concat.py
__repr__ ¶
close ¶
count ¶
fps ¶
hold_open ¶
This stream with each part held open, and returns itself.
A part opens its decoder on its first read, so a viewer opens only the parts it reads.
Source code in src/deeperfly/io/media/concat.py
release ¶
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W C"]]
Walk the parts in order, forward, never seeking across a boundary.
The inherited stream_blocks then never breaks a block at a part boundary.
Source code in src/deeperfly/io/media/concat.py
Image sequences, one frame per file, decoded in parallel by Pillow (ADR-0007).
Frames come back as (T, H, W, 3) uint8 RGB: gray broadcast to three channels, alpha
dropped, and samples wider than 8-bit rescaled onto 0..255 (_to_uint8). A
sequence has no frame rate.
ImageSequenceReader ¶
Bases: FrameReader
Reads an ordered image sequence (a directory, glob, or explicit file list).
Files decode in parallel on up to workers threads; gray_ok and thread_count
are accepted and ignored.
Source code in src/deeperfly/io/media/images.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
__getitem__ ¶
Decode frames (int / list / slice) into a uint8 RGB array.
Source code in src/deeperfly/io/media/images.py
__init__ ¶
Wrap an ordered list of image files, decoded on up to workers threads.
count ¶
from_pattern
classmethod
¶
A reader for a directory or glob, its files sorted by name.
stream_blocks ¶
stream_blocks(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
block_size: int = 64,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "T H W 3"]]
Yield (T, H, W, 3) uint8 blocks, each decoded in parallel across files.
Source code in src/deeperfly/io/media/images.py
stream_frames ¶
stream_frames(
*,
start: int = 0,
stop: int | None = None,
step: int = 1,
gray_ok: bool = False,
thread_count: int | None = None,
) -> Iterator[UInt8[np.ndarray, "H W 3"]]
Yield (H, W, 3) uint8 frames, one per file, decoded a block at a time.
Source code in src/deeperfly/io/media/images.py
list_image_files ¶
Sorted image files for a directory or glob pattern, ordered by name.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If nothing matches |
Source code in src/deeperfly/io/media/images.py
MP4 writing: VideoWriter, backed by PyAV.
Frames in, H.264 out, encoded incrementally so a long clip never sits in memory.
with media.VideoWriter("out.mp4", fps=100) as writer:
for frame in render(): # each an (H, W, 3) uint8 RGB frame
writer.write_frame(frame)
VideoWriter ¶
Incremental H.264 (libx264) MP4 encoder, backed by PyAV.
The file opens on the first frame, whose size (rounded down to even for yuv420p)
sets the encode dimensions; later frames are cropped to it, and non-uint8 input is
clipped to [0, 255].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file. |
required |
fps
|
float
|
Frame rate written into the container. |
30.0
|
codec
|
str | None
|
Encoder, or |
None
|
pix_fmt
|
str
|
Pixel format; |
'yuv420p'
|
options
|
Mapping[str, str] | None
|
Private encoder options as FFmpeg spells them
( |
None
|
Source code in src/deeperfly/io/media/writer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
__enter__ ¶
__exit__ ¶
__init__ ¶
__init__(
path: str | Path,
fps: float = 30.0,
*,
codec: str | None = None,
pix_fmt: str = "yuv420p",
options: Mapping[str, str] | None = None,
) -> None
Prepare a writer for path (no file is opened until the first frame).
Source code in src/deeperfly/io/media/writer.py
close ¶
Flush the encoder and close the file (idempotent).
Source code in src/deeperfly/io/media/writer.py
write_frame ¶
Append one (H, W, 3) RGB frame (NumPy, or a torch or DLPack array).
Source code in src/deeperfly/io/media/writer.py
write_frames ¶
Append a (T, H, W, 3) batch, or any iterable of frames or blocks.
Source code in src/deeperfly/io/media/writer.py
Overlap frame decoding with the work that consumes the frames.
prefetch runs a frame iterator in a background
thread and hands its items over through a bounded queue, so the next block decodes
while this one is consumed; PyAV releases the GIL, and peak memory is about
depth + 2 blocks. A rig prefetches one zipped multi-view iterator.
for block in media.prefetch(reader.stream_blocks(block_size=64, gray_ok=True)):
model(block) # the next block decodes during this call
prefetch ¶
Yield source's items in order, decoding up to depth ahead in a thread.
An exception raised by source is re-raised here after the items before it.
Abandoning the iterator stops the producer and closes source if it can be
closed, so no decoder is left running behind a full queue.