逐字稿
以字幕片段呈現錄音的內容,並在播放時跟著進度移動。
本頁內容
逐字稿以字幕片段(cue)清單呈現錄音的內容,每個片段都標有開始時間。播放錄音時,逐字稿會跟著進度移動:目前播放的片段以醒目的文字和有層次感的底色突顯,已播放的片段會淡化,尚未播放的片段則維持一般內文色。選擇某個片段,錄音就會從該處開始播放。transcript 行為會將它連接到媒體播放器或一般的 <audio>、<video>,連接對象由 aria-controls 中的 id 指定。
逐字稿
<div class="grid gap-sm max-w(480) mx-auto"> <div class="player" id="talk" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <svg class="icon player-off" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M7.633 4.7883L14.865 8.9606A1.2 1.2 0 0 1 14.865 11.0394L7.633 15.2117A1.2 1.2 0 0 1 5.8333 14.1723L5.8333 5.8277A1.2 1.2 0 0 1 7.633 4.7883Z" /> </svg> <svg class="icon player-on" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="5" y="3.75" width="3.3333" height="12.5" rx="0.8" /><rect x="11.6667" y="3.75" width="3.3333" height="12.5" rx="0.8" /> </svg> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div> </div> <section class="transcript" aria-label="Transcript" aria-controls="talk" style="--transcript-height: 12rem"> <ol class="transcript-list scroll-area"> <li class="transcript-cue" data-start="0.3"> <button type="button" class="transcript-time">0:00</button> <p class="transcript-text"> This is a short recording, made to show the player and its transcript. </p> </li> <li class="transcript-cue" data-start="4.7"> <button type="button" class="transcript-time">0:04</button> <p class="transcript-text"> Each line of the transcript is a cue, with the time it begins. </p> </li> <li class="transcript-cue" data-start="8.7"> <button type="button" class="transcript-time">0:08</button> <p class="transcript-text"> The line being spoken is marked, and the transcript keeps it in view. </p> </li> <li class="transcript-cue" data-start="13.06"> <button type="button" class="transcript-time">0:13</button> <p class="transcript-text"> Lines you have heard turn quiet. The ones still to come wait in plain ink. </p> </li> <li class="transcript-cue" data-start="17.65"> <button type="button" class="transcript-time">0:17</button> <p class="transcript-text"> Choose any line to jump to it, or scroll away and come back later. </p> </li> <li class="transcript-cue" data-start="21.79"> <button type="button" class="transcript-time">0:21</button> <p class="transcript-text">That is the whole recording. Thank you for listening.</p> </li> </ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-react/native/20/soft/play'import PauseIcon from '@master/icons-react/native/20/soft/pause'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="grid gap-sm max-w(480) mx-auto"> <div className="player" id="talk" role="group" aria-label="A short recording"> <audio className="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div className="player-controls"> <button type="button" className="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon className="icon player-off" /> <PauseIcon className="icon player-on" /> </button> <span className="player-elapsed">0:00</span> <input type="range" className="slider player-seek" min="0" max="0" step="any" defaultValue="0" aria-label="Seek" /> <span className="player-duration">0:00</span> </div> </div> <section className="transcript" aria-label="Transcript" aria-controls="talk" style={{ '--transcript-height': '12rem' } as React.CSSProperties}> <ol className="transcript-list scroll-area"> <li className="transcript-cue" data-start="0.3"> <button type="button" className="transcript-time">0:00</button> <p className="transcript-text"> This is a short recording, made to show the player and its transcript. </p> </li> <li className="transcript-cue" data-start="4.7"> <button type="button" className="transcript-time">0:04</button> <p className="transcript-text"> Each line of the transcript is a cue, with the time it begins. </p> </li> <li className="transcript-cue" data-start="8.7"> <button type="button" className="transcript-time">0:08</button> <p className="transcript-text"> The line being spoken is marked, and the transcript keeps it in view. </p> </li> <li className="transcript-cue" data-start="13.06"> <button type="button" className="transcript-time">0:13</button> <p className="transcript-text"> Lines you have heard turn quiet. The ones still to come wait in plain ink. </p> </li> <li className="transcript-cue" data-start="17.65"> <button type="button" className="transcript-time">0:17</button> <p className="transcript-text"> Choose any line to jump to it, or scroll away and come back later. </p> </li> <li className="transcript-cue" data-start="21.79"> <button type="button" className="transcript-time">0:21</button> <p className="transcript-text">That is the whole recording. Thank you for listening.</p> </li> </ol> <button type="button" className="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section> </div> )}<script> import { attach } from '@master/ui/behaviors' import PlayIcon from '@master/icons-svelte/native/20/soft/play' import PauseIcon from '@master/icons-svelte/native/20/soft/pause'</script><div class="grid gap-sm max-w(480) mx-auto" {@attach attach}> <div class="player" id="talk" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div> </div> <section class="transcript" aria-label="Transcript" aria-controls="talk" style="--transcript-height: 12rem"> <ol class="transcript-list scroll-area"> <li class="transcript-cue" data-start="0.3"> <button type="button" class="transcript-time">0:00</button> <p class="transcript-text"> This is a short recording, made to show the player and its transcript. </p> </li> <li class="transcript-cue" data-start="4.7"> <button type="button" class="transcript-time">0:04</button> <p class="transcript-text"> Each line of the transcript is a cue, with the time it begins. </p> </li> <li class="transcript-cue" data-start="8.7"> <button type="button" class="transcript-time">0:08</button> <p class="transcript-text"> The line being spoken is marked, and the transcript keeps it in view. </p> </li> <li class="transcript-cue" data-start="13.06"> <button type="button" class="transcript-time">0:13</button> <p class="transcript-text"> Lines you have heard turn quiet. The ones still to come wait in plain ink. </p> </li> <li class="transcript-cue" data-start="17.65"> <button type="button" class="transcript-time">0:17</button> <p class="transcript-text"> Choose any line to jump to it, or scroll away and come back later. </p> </li> <li class="transcript-cue" data-start="21.79"> <button type="button" class="transcript-time">0:21</button> <p class="transcript-text">That is the whole recording. Thank you for listening.</p> </li> </ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import PlayIcon from '@master/icons-vue/native/20/soft/play'import PauseIcon from '@master/icons-vue/native/20/soft/pause'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="grid gap-sm max-w(480) mx-auto"> <div class="player" id="talk" role="group" aria-label="A short recording"> <audio class="player-media" src="/demo/recording.m4a" controls preload="metadata"></audio> <div class="player-controls"> <button type="button" class="player-button player-play" aria-label="Play" aria-pressed="false"> <PlayIcon class="icon player-off" /> <PauseIcon class="icon player-on" /> </button> <span class="player-elapsed">0:00</span> <input type="range" class="slider player-seek" min="0" max="0" step="any" value="0" aria-label="Seek"> <span class="player-duration">0:00</span> </div> </div> <section class="transcript" aria-label="Transcript" aria-controls="talk" style="--transcript-height: 12rem"> <ol class="transcript-list scroll-area"> <li class="transcript-cue" data-start="0.3"> <button type="button" class="transcript-time">0:00</button> <p class="transcript-text"> This is a short recording, made to show the player and its transcript. </p> </li> <li class="transcript-cue" data-start="4.7"> <button type="button" class="transcript-time">0:04</button> <p class="transcript-text"> Each line of the transcript is a cue, with the time it begins. </p> </li> <li class="transcript-cue" data-start="8.7"> <button type="button" class="transcript-time">0:08</button> <p class="transcript-text"> The line being spoken is marked, and the transcript keeps it in view. </p> </li> <li class="transcript-cue" data-start="13.06"> <button type="button" class="transcript-time">0:13</button> <p class="transcript-text"> Lines you have heard turn quiet. The ones still to come wait in plain ink. </p> </li> <li class="transcript-cue" data-start="17.65"> <button type="button" class="transcript-time">0:17</button> <p class="transcript-text"> Choose any line to jump to it, or scroll away and come back later. </p> </li> <li class="transcript-cue" data-start="21.79"> <button type="button" class="transcript-time">0:21</button> <p class="transcript-text">That is the whole recording. Thank you for listening.</p> </li> </ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section> </div></template>#跟隨與捲離
錄音播放時,行為會以 aria-current="true" 標示目前的片段:也就是播放進度已到達其 data-start 的片段中,最後一個片段,因此在兩個片段之間的停頓期間,位置仍會保留。在它之前的每個片段都會以已播放的樣式繪製,這完全由樣式表負責。
清單會讓目前的片段保持在中央。自動跟隨時,只有逐字稿清單會捲動,頁面會維持原位。清單會平滑捲動,使用者啟用減少動態效果時則直接跳到定位。當讀者用滾輪、手指、按鍵或捲軸捲動清單,使目前的片段離開視野時,清單會停止跟隨,並在下緣顯示一顆小型膠囊按鈕 transcript-return,可將清單帶回目前位置。選擇某個片段,或將目前的片段捲回視野內,都會恢復跟隨。
清單在 --transcript-height 的高度內捲動,未設定時為 20rem。為清單加上 scroll-area,即可使用纖細的捲軸,並在內容延續的邊緣顯示淡出效果。
#從 WebVTT 軌道產生
清單為空時,行為會透過 TextTrack API 讀取媒體的第一條 captions 或 subtitles 軌道,寫入字幕片段:包括時間、去除標籤後的文字,以及語音標籤所指名的說話者(<v Ana>)。軌道只有在未停用時才會載入字幕片段,因此行為會將停用的軌道設為 hidden,這樣不會在畫面上顯示任何內容。下面的逐字稿跟隨的是一個使用瀏覽器控制項的一般 <audio>。
從軌道產生
<div class="grid gap-sm max-w(480) mx-auto"> <audio id="talk-audio" class="w-full" src="/demo/recording.m4a" controls preload="metadata"> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </audio> <section class="transcript" aria-label="Transcript" aria-controls="talk-audio" style="--transcript-height: 15rem"> <ol class="transcript-list scroll-area"></ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="grid gap-sm max-w(480) mx-auto"> <audio id="talk-audio" className="w-full" src="/demo/recording.m4a" controls preload="metadata"> <track kind="captions" src="/demo/recording.vtt" srcLang="en" label="English" /> </audio> <section className="transcript" aria-label="Transcript" aria-controls="talk-audio" style={{ '--transcript-height': '15rem' } as React.CSSProperties}> <ol className="transcript-list scroll-area"></ol> <button type="button" className="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div class="grid gap-sm max-w(480) mx-auto" {@attach attach}> <audio id="talk-audio" class="w-full" src="/demo/recording.m4a" controls preload="metadata"> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </audio> <section class="transcript" aria-label="Transcript" aria-controls="talk-audio" style="--transcript-height: 15rem"> <ol class="transcript-list scroll-area"></ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="grid gap-sm max-w(480) mx-auto"> <audio id="talk-audio" class="w-full" src="/demo/recording.m4a" controls preload="metadata"> <track kind="captions" src="/demo/recording.vtt" srclang="en" label="English"> </audio> <section class="transcript" aria-label="Transcript" aria-controls="talk-audio" style="--transcript-height: 15rem"> <ol class="transcript-list scroll-area"></ol> <button type="button" class="btn btn-sm btn-pill transcript-return" hidden> Back to current </button> </section> </div></template>#行為
transcript 接收根元素,也就是帶有 transcript 類別的元素,並傳回用來解除行為的函式。
import { transcript } from '@master/ui/behaviors/transcript.js'const detach = transcript(document.querySelector('.transcript'))- 媒體是
aria-controls所指的元素:若是媒體播放器,會取用其中的player-media;也可以是<audio>或<video>。每次媒體發出事件時,行為都會重新查找,因此在逐字稿之後才寫入的媒體,或之後改指定的其他媒體,都能被跟隨。media選項則可改為直接指定元素。 - 字幕片段是帶有
data-start(開始的秒數)的li,內含一個transcript-time按鈕和一個transcript-text。在片段上任一處按下都會從它的開頭播放,除非讀者正在選取其中的文字;在它的時間上按 Enter 也有相同效果。 - 由其他程式寫入的列。 行為每次都會重新讀取清單,不保存任何內容,因此算繪程式可以寫入並變更字幕片段。
draw: false會將空清單留給算繪程式處理;track用來選擇寫入字幕片段的來源軌道。行為解除時,會移除它所寫入的列。
| 選項 | 預設值 | |
|---|---|---|
media | 取自 aria-controls | 要跟隨的媒體元素 |
track | 第一條 captions 或 subtitles 軌道 | 清單為空時,用來產生字幕片段的來源軌道 |
draw | true | 清單為空時,是否從軌道讀取並填入字幕片段 |
未掛上行為時,字幕片段就是供讀者閱讀的清單;不會標示任何片段,不會跟隨,選擇片段也不會跳轉。
#鍵盤操作
所有時間按鈕共用一個 Tab 停駐點,也就是目前片段的時間按鈕,並以方向鍵在它們之間移動,因此很長的逐字稿也不會讓 Tab 鍵的巡覽順序變得冗長。
| 按鍵 | 動作 |
|---|---|
| Tab | 進入清單時焦點落在目前片段的時間(若無則為第一個片段),再按即離開:整個清單只占一個 Tab 停駐點 |
| ArrowDown, ArrowUp | 移到下一個或上一個片段的時間 |
| Home, End | 移到第一個片段或最後一個片段的時間 |
| Enter, Space | 從取得焦點的片段時間開始播放,行為與原生按鈕相同 |
#無障礙
- 請以
aria-label,或以aria-labelledby指向的標題為逐字稿命名,並在aria-controls中指定它的媒體。 - 目前的片段設有
aria-current="true";樣式表透過文字明暗和視覺層次突顯目前片段,無須依賴色相。強制色彩模式下則使用系統外框線。 - 錄音播放時,逐字稿絕不會自行取得焦點。只有讀者移動焦點時焦點才會移動,啟用返回按鈕時,焦點會回到目前的片段。
- 沒有畫面的錄音需要逐字稿,才能在沒有聲音的情況下被理解。若頁面必須能在沒有腳本的情況下閱讀,請將逐字稿寫在頁面中。
#React、Svelte 和 Vue
transcript 元件會根據 cues 寫入字幕片段,每個片段包含 start、它的 text,以及選用的 speaker;它會指定以 media 傳入的媒體,並掛上行為,由行為負責標示和跟隨。用 npx master-ui add transcript 將元件複製到專案中。元件的建置與使用方式請參閱 React、Svelte 和 Vue。
#類別
| 類別 | 說明 |
|---|---|
transcript | 逐字稿本體,放在其媒體旁。請設定 aria-label,並以 aria-controls 指向它所跟隨的播放器或媒體元素的 id |
transcript-list | 以 ol 呈現的片段清單,在 --transcript-height 範圍內捲動。加上 scroll-area 可讓邊緣淡出 |
transcript-cue | 一個片段,使用帶有 data-start(開始的秒數)的 li:先是時間,接著是文字。播放過後轉為低調,成為目前片段時則以浮起的底板醒目顯示。在片段上任何位置按下,都會從其時間開始播放 |
transcript-time | 片段開始的時間,以小字等寬數字顯示在按鈕上,按下即從該處播放。也是清單的 Tab 停駐點 |
transcript-text | 片段的文字 |
transcript-speaker | 錄音中有多位說話者時,顯示在文字上方的說話者 |
transcript-return | 回到目前片段:位於清單下緣的小型膠囊按鈕,在讀者將目前片段捲出可見範圍之前都會隱藏 |
返回按鈕是帶有 transcript-return 部件的按鈕,即 btn btn-sm btn-pill。
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--transcript-height | 20rem | 清單開始捲動前可達到的最大高度設定於 transcript |