Tracks¶
One-pass extraction for long recordings, with a completeness check that reads the data rather than the file.
One pass over a long recording, and everything a timeline needs afterwards.
mg_motion is built for a clip and for a person looking at the result: it can write
a motion video, plots, motiongrams and a data file, and it computes centroid and area
whether or not you asked for them. That generosity is the right default for interactive
use and the wrong one for a two-hour session, where the cost decomposes like this on
120 s of 1080p video:
motion_analysis='all', motiongrams on 245 s
motion_analysis='qom', motiongrams on 215 s
motion_analysis='qom', motiongrams off 62 s
The motiongrams are 71 per cent of it and the area of motion another 12. This module
does the one pass those numbers argue for: convert each motion frame to greyscale
once, and take everything from that --- the quantity of motion, both motiongram
columns. centroid() converts to greyscale internally and then throws the conversion
away; doing it once and reusing it is most of the saving, and working on one channel
rather than three is the rest.
Nothing is appended to a growing array. The frame count is known before the pass
starts, so the columns go into a preallocated memory-mapped file. That is not a
micro-optimisation: growing these by np.append is what made a session take an
extrapolated 215 hours before 2026-08-24.
The videogram is stored as a pyramid, the way an audio editor stores peaks. A column per frame is finer than any page can show --- 50 columns per second on an A4 width is one column per 20 pixels even when zoomed to a single action --- but the whole session at that rate is 475,680 columns and cannot be drawn at all. So each level halves the one below it by taking the extremes rather than the mean, because a brief motion must survive being zoomed out of; averaging is what makes a spike disappear at low magnification. Levels are built once, after the pass, from the base that is already on disk, and cost a geometric series: less than the base again.
Reading is then a slice: pick the level whose width is nearest the pixels available and take the columns for the time range wanted.
extract_tracks ¶
extract_tracks(video, out_dir=None, filtertype='Regular', threshold=0.05, videograms=True, blur='None', use_median=False, kernel_size=5, plate_every=None, progress=True)
Quantity of motion and both videogram bases, in one pass over the video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
path to the recording. |
required | |
out_dir
|
where |
None
|
|
filtertype, threshold, blur, use_median, kernel_size
|
passed to the same
ffmpeg filter chain |
required | |
plate_every
|
keep one raw frame in this many for the room plate, or None to keep none. The frames are sampled across the whole recording, so a plate built from them describes the whole room rather than one stretch. |
None
|
|
progress
|
show a progress bar. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
paths written, and the parameters that made them. |
Source code in musicalgestures/_tracks.py
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 | |
extract_videograms ¶
extract_videograms(video, analysis_dir, frames=None, width=None, height=None, ffmpeg_input_args=None)
True videograms of the whole video, one column (row) per frame, as memmap bases.
A videogram averages the picture across one axis; the motiongram averages the motion
frame. extract_tracks computes the latter in its pass over the filtered stream, so the
videogram needs one more decode, which this does with a single ffmpeg filter graph that
writes both axes straight to disk (videogram_v.u1: frames Ă— height, videogram_h.u1:
frames Ă— width, uint8 grey). When frames is given the bases are trimmed at the front to
that many rows, so column j lines up with motion frame j (the motion frame is the
difference to the previous picture, hence one fewer). Returns the meta keys to merge.
Source code in musicalgestures/_tracks.py
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 | |
build_pyramid ¶
build_pyramid(analysis_dir, which='videogram_v')
Halve a videogram base repeatedly, keeping extremes rather than means.
Level 0 is the base, one column per frame. Level k is 2^k frames per column, and each column holds the greatest value of the columns beneath it. Extremes, not means: a motion lasting a few frames is exactly what a viewer zooms out to find, and averaging is what makes it vanish at low magnification.
Returns the paths written, coarsest last.
Source code in musicalgestures/_tracks.py
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 | |
read_columns ¶
read_columns(analysis_dir, start_s=0.0, end_s=None, max_columns=2000, which='videogram_v')
The videogram for a time range, at the coarsest level that still fills the width.
This is how an audio editor draws a waveform: choose the level whose resolution the display can use and read a slice of it, rather than reading everything and throwing most of it away.
Levels are built on first use (build_pyramid), so a fresh extraction can be read
straight away. Returns (columns, seconds_per_column).
Source code in musicalgestures/_tracks.py
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 | |
extract_tracks_parallel ¶
extract_tracks_parallel(video, out_dir=None, workers=None, chunk_s=120.0, filtertype='Regular', threshold=0.05, blur='None', use_median=False, kernel_size=5, plate_every=None, resume=True, videograms=True)
The same pass, split over processes by time. Resumable.
The work is embarrassingly parallel because each frame's motion depends only on its predecessor, so a chunk needs one frame of lead-in and nothing else. Workers write into disjoint slices of the same memory-mapped files, which is why no merging step is needed and why a crashed worker costs one chunk rather than the run.
resume=True skips chunks that already left a marker, so restarting after a
failure at hour five does not redo hours one to four --- the lesson the SINS
producers learned by truncating a completed table.
Source code in musicalgestures/_tracks.py
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 | |
check_tracks ¶
check_tracks(analysis_dir)
What an extraction actually produced, read from the data rather than the file.
extract_tracks_parallel preallocates its memmaps to an estimated frame count, so
the files reach full size in the first second of a run and every cheap check ---
size, existence, ls -la, the last row of the array --- reports a finished
extraction over a file that may be mostly zeros.
Three numbers are returned separately and unreconciled, because on a run killed at 08:28 on 2026-08-25 they disagreed by 42,000 and 211,000 frames and each was right about something different:
preallocatedis the estimate the file was sized to, and was never a measurement;last_nonzerois where data stops, because workers write continuously and only drop a marker when a whole chunk closes;highest_markeris the last chunk that closed, and is whatresume=Truetrusts.
complete is true only when tracks_run.json exists, since that file is written
last and by the runner alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis_dir
|
The directory holding |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
|
dict
|
|
Source code in musicalgestures/_tracks.py
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |