from typing import Optional
import re


def parse_timestamp(timestamp: str) -> Optional[int]:
    """Parse a playback timestamp into a number of seconds.

    Expects the timestamp to be in HH:MM:SS or MM:SS format.
    """
    match = re.match(
        r"^(?:(?P<hours>\d+):)?(?P<minutes>\d+):(?P<seconds>\d+)$", timestamp
    )

    if not match:
        return None

    time_fields = match.groupdict()
    return (
        int(time_fields["hours"] or 0) * 3600 +
        int(time_fields["minutes"]) * 60 +
        int(time_fields["seconds"])
    )