Skip to content

3 ‐ Preprocessing

Alexander Refsum Jensenius edited this page Jun 27, 2026 · 21 revisions

For complete documentation see Preprocessing on the docs site.


All preprocessing is applied at load time via MgVideo constructor arguments. Steps execute in this order: trim → skip → fix → rotate → contrast/brightness → crop → grayscale.

import musicalgestures as mg

mv = mg.MgVideo(
    '/path/to/video.avi',
    starttime=5,        # trim start (seconds)
    endtime=15,         # trim end (seconds)
    skip=3,             # keep every 4th frame
    rotate=90,          # degrees
    contrast=100,       # -100 to 100
    brightness=20,      # -100 to 100
    crop='auto',        # 'auto', 'manual', or None
    color=False,        # False = grayscale
)

Key parameters

Parameter Description
starttime, endtime Trim to a time range in seconds
skip Skip n frames between each kept frame
frames Fix total frame count (−1 for keyframes only)
rotate Rotation angle in degrees
contrast, brightness Percentage adjustment, −100 to 100
crop 'auto' (motion-based) or 'manual' (draw rectangle)
color False for grayscale mode
keep_all Keep intermediate files from each step

Resampling (frame rate, speed, frame decimation)

resample() is a method on an already-loaded MgVideo. It returns a new MgVideo and leaves the original untouched:

mv = mg.MgVideo('/path/to/video.avi')

mv25 = mv.resample(fps=25)        # retime to 25 fps (duration-preserving)
fast = mv.resample(speed=2.0)     # 2× faster — video + audio retimed in sync
slow = mv.resample(speed=0.5)     # 2× slower / longer
dec  = mv.resample(skip=2)        # discard 2 frames for every one kept
  • fps — duration-preserving frame-rate change (FFmpeg fps filter)
  • speed — playback-speed factor (>1 faster/shorter, <1 slower/longer); retimes video + audio in sync
  • skip — integer frame decimation (also speeds up)

When combined, they are applied in order skipspeed/fps. The output name defaults to the input name + _resampled; target_name/overwrite work as elsewhere.

Output files

By default each method appends a suffix: dance_history.avi. With overwrite=True (default) an existing file is replaced in place; set overwrite=False for the old auto-increment behaviour: dance_history_0.avi, dance_history_1.avi, etc.

Clone this wiki locally