{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# WebVTT Reading and Chunking Test" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime, timedelta\n", "from functools import partial\n", "from html import escape\n", "from io import BytesIO\n", "from IPython.display import display_html\n", "from itertools import chain\n", "import re\n", "from webvtt import Caption, WebVTT\n", "from webvtt.models import Timestamp\n", "from zoneinfo import ZoneInfo\n", "\n", "display_html = partial(display_html, raw=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "FILE_PATH = 'GMT20250411-223535_Recording.transcript.vtt'\n", "TIME_ZONE = ZoneInfo(\"America/New_York\")\n", "BASE_TIME = datetime(2025, 4, 11, hour=22, minute=35, second=35, tzinfo=ZoneInfo(\"GMT\")).astimezone(TIME_ZONE)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "with open(FILE_PATH, 'rb') as file:\n", " web_vtt = WebVTT.from_buffer(BytesIO(file.read()))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_html(''.join(chain('')))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " Caption #344\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "speaker_speech_pattern = re.compile('(?:([^:]+): )?(.*)')\n", "\n", "match web_vtt.captions[343]:\n", " case Caption(identifier=identifier, start_time=start_time, end_time=end_time, text=text):\n", " match speaker_speech_pattern.search(text).groups():\n", " case (speaker, speech):\n", " display_html(f\"\"\"\n", " Caption #{identifier}\n", " \n", " \"\"\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }