Little python script to export session markers to text file

Hello there,

In case this is useful to anyone, here’s a small python script that can be used to export all session markers (except ranges) to a tabulated text file.

"""This script outputs a text file containing markers names and positions
from an Ardour session file.

Range/interval markers are not supported yet.
"""

from datetime import timedelta
from dataclasses import dataclass
from pathlib import Path
from xml.etree import ElementTree


SESSION_PATH = Path() / 'session.ardour'

MARKERS_PATH = Path() / 'markers.txt'


@dataclass(order=True)
class Marker:
    sample: int
    name: str
    sample_rate: int

    @property
    def time(self):
        return timedelta(seconds=self.sample/self.sample_rate)

    def __str__(self):
        return f"Marker {self.name} @ {self.time}"


class Session:
    def __init__(self, path):
        self.tree = ElementTree.parse(path)

    @property
    def sample_rate(self):
        """Session sample rate in Hz."""
        return int(self.tree.getroot().get('sample-rate'))

    @property
    def markers(self):
        """Session markers as a sorted list of dataclasses."""
        return sorted((
            Marker(int(el.get('start')), el.get('name'), self.sample_rate)
            for el in self._iter_markers()
        ))

    def _iter_markers(self):
        """Iterator yielding session markers XML Elements."""
        return filter(
            lambda el: 'IsMark' in el.get('flags').split(','),
            self.tree.iterfind('.//Location')
        )

    def make_marker_table(self):
        """Return a tabulated marker table from the session data."""
        return "Marker\tTimecode\tSample\n\n" + '\n'.join(
            "\t".join((marker.name, str(marker.time), str(marker.sample)))
            for marker in self.markers
        )


if __name__ == '__main__':
    session = Session(SESSION_PATH)
    print(*session.markers, sep='\n')

    with open(MARKERS_PATH, mode='wt') as file:
        file.write(session.make_marker_table())

The output looks like this:

Marker		Timecode		Sample

something	0:00:00			0
another		0:04:09.999773	11024990
third		0:14:36.941406	38673116
1 Like

Thanks! I will try it and it will be useful at work.

OK, feel free to ask if you need any help or encounter issues.

1 Like