Skip to content

Cli

Command-line interface for MGT-python.

The musicalgestures command provides quick access to the most common analysis and visualisation operations without writing Python code.

Usage::

musicalgestures --help
musicalgestures motion dancer.avi --threshold 0.05 --filtertype Regular
musicalgestures videograms dancer.avi
musicalgestures average dancer.avi
musicalgestures info dancer.avi
musicalgestures convert dancer.avi --to mp4

Install CLI dependencies with::

pip install musicalgestures[cli]

main

main()

Entry point registered in pyproject.toml as musicalgestures.

Source code in musicalgestures/cli.py
 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
def main() -> None:
    """Entry point registered in pyproject.toml as ``musicalgestures``."""
    click = _require_click()

    @click.group(
        context_settings={"help_option_names": ["-h", "--help"]},
        invoke_without_command=True,
    )
    @click.version_option(package_name="musicalgestures", prog_name="musicalgestures")
    @click.pass_context
    def cli(ctx):
        """Musical Gestures Toolbox – command-line interface.

        Analyse and visualise video and audio files from the command line.
        Run 'musicalgestures COMMAND --help' for details on each command.
        """
        if ctx.invoked_subcommand is None:
            click.echo(ctx.get_help())

    # ------------------------------------------------------------------
    # info
    # ------------------------------------------------------------------

    @cli.command("info")
    @click.argument("filename", type=click.Path(exists=True))
    def cmd_info(filename):
        """Print metadata about a video or audio file."""
        try:
            from musicalgestures._utils import get_length, get_fps, get_widthheight, has_audio
            length = get_length(filename)
            fps = get_fps(filename)
            width, height = get_widthheight(filename)
            audio = has_audio(filename)
            click.echo(f"File:     {filename}")
            click.echo(f"Size:     {width} × {height} px")
            click.echo(f"FPS:      {fps:.2f}")
            click.echo(f"Length:   {length:.3f} s")
            click.echo(f"Audio:    {'yes' if audio else 'no'}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # motion
    # ------------------------------------------------------------------

    @cli.command("motion")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--filtertype", default="Regular",
                  type=click.Choice(["Regular", "Binary", "Blob"], case_sensitive=False),
                  help="Filter type for motion detection.")
    @click.option("--threshold", default=0.05, type=float, show_default=True,
                  help="Pixel-value threshold (0–1).")
    @click.option("--blur", default="None",
                  type=click.Choice(["None", "Average"], case_sensitive=False),
                  help="Blur type.")
    @click.option("--color/--no-color", default=True, show_default=True,
                  help="Process in colour (default) or grayscale.")
    @click.option("--overwrite", is_flag=True, help="Overwrite existing output files.")
    def cmd_motion(filename, filtertype, threshold, blur, color, overwrite):
        """Render a motion video for FILENAME."""
        try:
            import musicalgestures as mg
            v = mg.MgVideo(filename, color=color)
            out = v.motion(filtertype=filtertype, threshold=threshold, blur=blur,
                           save_video=True, save_plot=False, save_data=False,
                           overwrite=overwrite)
            click.echo(f"Motion video saved: {out.filename}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # videograms
    # ------------------------------------------------------------------

    @cli.command("videograms")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--overwrite", is_flag=True, help="Overwrite existing output files.")
    def cmd_videograms(filename, overwrite):
        """Render horizontal and vertical videograms for FILENAME."""
        try:
            import musicalgestures as mg
            v = mg.MgVideo(filename)
            out = v.videograms(overwrite=overwrite)
            click.echo(f"Videograms saved: {[o.filename for o in out]}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # average
    # ------------------------------------------------------------------

    @cli.command("average")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--overwrite", is_flag=True, help="Overwrite existing output files.")
    def cmd_average(filename, overwrite):
        """Render a pixel-average (blend) image for FILENAME."""
        try:
            import musicalgestures as mg
            v = mg.MgVideo(filename)
            out = v.average(overwrite=overwrite)
            click.echo(f"Average image saved: {out.filename}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # history
    # ------------------------------------------------------------------

    @cli.command("history")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--overwrite", is_flag=True, help="Overwrite existing output files.")
    def cmd_history(filename, overwrite):
        """Render a motion history image for FILENAME."""
        try:
            import musicalgestures as mg
            v = mg.MgVideo(filename)
            out = v.history(overwrite=overwrite)
            click.echo(f"History image saved: {out.filename}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # convert
    # ------------------------------------------------------------------

    @cli.command("convert")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--to", "target_format", default="mp4", show_default=True,
                  help="Target container format (e.g. mp4, avi, mov).")
    @click.option("--overwrite", is_flag=True, help="Overwrite existing output files.")
    def cmd_convert(filename, target_format, overwrite):
        """Convert FILENAME to another container format."""
        try:
            import os
            from musicalgestures._utils import convert
            of = os.path.splitext(filename)[0]
            out = convert(filename, f"{of}.{target_format.lstrip('.')}", overwrite=overwrite)
            click.echo(f"Converted: {out}")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    # ------------------------------------------------------------------
    # motiongrams
    # ------------------------------------------------------------------

    @cli.command("motiongrams")
    @click.argument("filename", type=click.Path(exists=True))
    @click.option("--filtertype", default="Regular",
                  type=click.Choice(["Regular", "Binary", "Blob"], case_sensitive=False))
    @click.option("--threshold", default=0.05, type=float, show_default=True)
    @click.option("--overwrite", is_flag=True)
    def cmd_motiongrams(filename, filtertype, threshold, overwrite):
        """Render horizontal and vertical motiongrams for FILENAME."""
        try:
            import musicalgestures as mg
            v = mg.MgVideo(filename)
            out = v.motiongrams(filtertype=filtertype, threshold=threshold, overwrite=overwrite)
            click.echo(f"Motiongrams saved.")
        except Exception as exc:
            click.echo(f"Error: {exc}", err=True)
            sys.exit(1)

    cli()