"""Read per-shape PowerPoint video trims and flag unsupported timing.""" import math import re P = 'http://schemas.openxmlformats.org/presentationml/2006/main' P14 = 'http://schemas.microsoft.com/office/powerpoint/2010/main' R = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' def time_offset(value): """Office ST_UniversalTimeOffset: default unit is milliseconds.""" match = re.fullmatch(r'(\d+(?:\.\d+)?)(h|min|s|ms|[μµ]s|ns)?', value.strip()) if not match: raise ValueError(f'Invalid time offset: {value}') seconds = float(match[1]) * { None: .001, 'h': 3600, 'min': 60, 's': 1, 'ms': .001, 'μs': .000001, 'µs': .000001, 'ns': .000000001, }[match[2]] if not math.isfinite(seconds): raise ValueError('Non-finite time offset') return seconds def shape_playbacks(root, relationships, video_parts, slide): parents = {child: parent for parent in root.iter() for child in parent} result = [] for shape in root.iter(): if shape.tag not in (f'{{{P}}}pic', f'{{{P}}}sp', f'{{{P}}}graphicFrame'): continue identity = shape.find(f'.//{{{P}}}cNvPr') shape_id = identity.get('id') if identity is not None else None parts = {} for el in shape.iter(): if el.tag.rsplit('}', 1)[-1] not in ('videoFile', 'media'): continue for attr in (f'{{{R}}}embed', f'{{{R}}}link'): part = relationships.get(el.get(attr), (None, ''))[0] if part in video_parts: parts.setdefault(part, []) if el.tag == f'{{{P14}}}media': parts[part].extend(el.findall(f'{{{P14}}}trim')) for part, trims in parts.items(): item = {'archive_path': part, 'slide': slide, 'shape_id': shape_id, 'trim_start_seconds': 0.0, 'trim_end_seconds': 0.0, 'playback_status': 'normal', 'playback_notes': []} try: values = {(time_offset(t.get('st', '0')), time_offset(t.get('end', '0'))) for t in trims} if len(values) > 1: raise ValueError('Conflicting trims') if values: item['trim_start_seconds'], item['trim_end_seconds'] = values.pop() except ValueError: item['playback_status'] = 'invalid-trim' item['playback_notes'].append('Invalid or conflicting trim settings.') if shape_id is not None: for media in root.iter(f'{{{P}}}cMediaNode'): target = media.find(f'{{{P}}}tgtEl/{{{P}}}spTgt') if target is None or target.get('spid') != shape_id: continue # The media node and its ancestor timing containers can repeat. timing = list(media.findall(f'{{{P}}}cTn')) parent = parents.get(media) while parent is not None: if parent.tag == f'{{{P}}}cTn': timing.append(parent) parent = parents.get(parent) for node in timing: repeat = node.get('repeatCount') repeated = repeat is not None and repeat not in ('1000', '1000.0', '100%') if repeated or node.get('repeatDur') not in (None, '0'): item['playback_status'] = 'looping' item['playback_notes'].append('Repeated playback; not exported to Ontime.') unsupported = ( node.get('spd') not in (None, '100000', '100%') or node.get('autoRev') in ('1', 'true') or node.get('accel') not in (None, '0', '0%') or node.get('decel') not in (None, '0', '0%') or node.get('tmFilter') is not None or node.find(f'{{{P}}}endCondLst') is not None ) if unsupported: if item['playback_status'] != 'looping': item['playback_status'] = 'custom-timing' item['playback_notes'].append('Custom speed, reverse, or end conditions are not simulated.') if media.get('numSld') not in (None, '1'): if item['playback_status'] != 'looping': item['playback_status'] = 'custom-timing' item['playback_notes'].append('Playback across slides is not simulated.') # Natural media duration is commonly encoded as "indefinite". # A finite timing cap needs timeline evaluation, not trim maths. for node in media.findall(f'{{{P}}}cTn'): if node.get('dur') not in (None, 'indefinite', 'media'): if item['playback_status'] != 'looping': item['playback_status'] = 'custom-timing' item['playback_notes'].append('Explicit animation duration is not simulated.') item['playback_notes'] = list(dict.fromkeys(item['playback_notes'])) if item not in result: result.append(item) return result def effective_playback(video, settings): result = {**video, **settings} result['slides'] = [settings['slide']] if settings['slide'] is not None else [] original = video['duration_seconds'] result['original_duration_seconds'] = original start, end = settings['trim_start_seconds'], settings['trim_end_seconds'] status = settings['playback_status'] result['duration_seconds'] = None if status == 'normal': if original is None: status = 'unknown' elif start + end >= original and (start or end): status = 'invalid-trim' result['playback_notes'] = [*settings['playback_notes'], 'Trim removes the whole media or exceeds its duration.'] else: result['duration_seconds'] = round(original - start - end, 9) status = 'trimmed' if start or end else 'normal' result['playback_status'] = status result['ontime_eligible'] = status in ('normal', 'trimmed', 'unknown') return result