Skip to content

Timecode

Absolute-clock helpers: parse recording start times from filenames.

The regexes are byte-identical to ambiscape's (ambiscape/io.py), so a folder of phone/recorder/360-camera files resolves to the same wall-clock timeline in both toolboxes.

filename_datetime

filename_datetime(path)

Parse a YYYYMMDD_HHMMSS / YYMMDD_HHMMSS filename stamp.

Source code in musicalgestures/_timecode.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def filename_datetime(path) -> dt.datetime | None:
    """Parse a ``YYYYMMDD_HHMMSS`` / ``YYMMDD_HHMMSS`` filename stamp."""
    name = Path(path).name
    m = _TS_LONG.search(name)
    if m:
        y, mo, d, hh, mm, ss = (int(g) for g in m.groups())
        try:
            return dt.datetime(y, mo, d, hh, mm, ss)
        except ValueError:
            pass
    m = _TS_SHORT.search(name)
    if m:
        yy, mo, d, hh, mm, ss = (int(g) for g in m.groups())
        try:
            return dt.datetime(2000 + yy, mo, d, hh, mm, ss)
        except ValueError:
            pass
    return None

media_start_datetime

media_start_datetime(path)

Start time of a recording: filename stamp, else file mtime.

Source code in musicalgestures/_timecode.py
37
38
39
40
41
42
43
44
45
def media_start_datetime(path) -> dt.datetime | None:
    """Start time of a recording: filename stamp, else file mtime."""
    stamped = filename_datetime(path)
    if stamped is not None:
        return stamped
    try:
        return dt.datetime.fromtimestamp(os.path.getmtime(path))
    except OSError:
        return None