def frames_to_segments(df_frames, total_duration=None):
    """
    Converts a dataframe with labelled frames to segments.
    It merges adjacent frames with equal labels.
    Both input and output dataframes have columns ['start', 'end', 'label'].
    The exact total duration (end time) can be optionally specified.
    Times are in seconds.
    """
    df = df_frames.copy()
    labels = df['label']
    segment_start = labels != labels.shift(1)
    df_segments = df[segment_start].copy()
    end_time = total_duration if total_duration else df['end'].iloc[-1]
    df_segments['end'] = df_segments['start'].shift(-1).fillna(end_time)
    return df_segments