Repository navigation
Subtitles
SubtitleEndpoint.kt provides two subtitle capabilities backed by PikPak's cloud index and real-time speech-to-text service:
-
Cloud subtitle lookup (
GET /oracle/subtitle) — fetches uploaded, extracted, or speech-generated subtitle files indexed by GCID. -
Real-time AI subtitles (
GET /subsrelay/v1/node+ WebSocket) — streams live 16 kHz mono PCM audio to asubsrelaynode and receives timestamped caption cues, optional translation, and quota updates.
val subtitles = client.getCloudSubtitles(
gcid = fileGcid,
durationSeconds = videoDurationSeconds, // optional; sent as `duration` when > 0
)Each CloudSubtitle carries gcid, url, ext (without leading dot, e.g. "srt", "ass", "ssa"), name, durationMs, languages, score, and source (CloudSubtitleSource.UPLOADED = 0, EXTRACTED = 1, SPEECH = 2).
val node = client.getAiSubtitleNode(gcid)
// node.url, node.sourceLanguage, node.supportedLanguages, node.quota- When the account's period allowance is exhausted (
node.quota?.exceeded == true),getAiSubtitleNodethrowsAiSubtitleQuotaExceededException(carryingnode, witherrorDescriptionset tonextResetTimewhen non-blank). - For non-primary audio tracks of the same file, derive a distinct track GCID with
aiSubtitleAudioTrackGcid(fileGcid, streamIndex)(SHA-1("$canonicalGcid/audio/$streamIndex"), uppercase hex) so tracks do not collide insubsrelay's per-GCID caption cache. - When
client.domainispikpakdrive.comorpikpak.me, the server may echosubsrelay.pikpakdrive.comorsubsrelay.pikpak.me(NXDOMAIN in public DNS).openAiSubtitleSessionnormalises those suffixes back to.mypikpak.comautomatically, mirroring OSS upload endpoint normalisation.
openAiSubtitleSession has two overloads: pass an already resolved node.url, or pass AiSubtitleStart alone to resolve the node and connect in one call.
val (node, session) = client.openAiSubtitleSession(
AiSubtitleStart(
gcid = trackGcid,
mediaId = mediaId,
fileId = fileId,
targetLanguage = "zh-CN", // from node.supportedLanguages, or "" for original speech
),
)Audio expectations (AiSubtitleAudioFormat):
- Encoding:
pcm_s16le,16_000Hz,1channel (mono). - Recommended frame size:
100ms per binary frame (RECOMMENDED_FRAME_MS), fed ~10s ahead of playback (RECOMMENDED_LEAD_MS) so cues arrive before speech is heard. - Wire framing (handled inside
AiSubtitleSession): 2-byte big-endian JSON header length, UTF-8{"position_ms":<positionMs>}, followed by raw PCM bytes.
Sending audio and control signals:
-
trySendAudio(positionMs, pcm)— non-blocking; drops and returnsfalseonce the 256-frame outbound queue reaches half capacity so control frames never stall behind audio. -
sendAudio(positionMs, pcm)— suspending counterpart. -
trySendControl(control)/sendControl(control)— sendsAiSubtitleControl.SEEK,PAUSE,RESUME,FINALIZE, orSTOP.
for (event in session.events) {
when (event) {
is AiSubtitleEvent.Cue -> renderCue(event.cue) // startMs, endMs, text, language
is AiSubtitleEvent.Quota -> updateQuota(event.quota)
is AiSubtitleEvent.Error -> if (!event.isRecoverable) handleError(event)
is AiSubtitleEvent.Stop -> if (event.isQuotaExceeded) showQuotaExceeded()
}
}-
AiSubtitleEvent.Error.isRecoverableistrueforsoniox_unavailable, where the server leaves the socket open for subsequent audio frames. -
One concurrent session per account:
subsrelayallows only one active WebSocket session per account. Always finish withsession.closeGracefully(timeout = 3.seconds)(sends{"type":"stop"}behind queued frames and waits for the server to close the socket and release the slot) before opening a replacement session. Non-suspendingsession.close()is also available asAutoCloseablefor synchronous teardown.
WebSocket upgrades require HTTP/1.1 and live for the duration of a video without an overall request timeout. PikPakClient creates a dedicated WebSocket HttpClient lazily on first use (OkHttp pinned to HTTP/1.1 on JVM/Android, Darwin on Apple) and closes it in PikPakClient.close(). If you inject your own httpClient, the SDK reuses it when WebSockets is already installed, or derives a lightweight config { install(WebSockets) } copy that it owns and closes.
Using the SDK
- Getting Started
- Best Practices
- Playback
- Original Link Pools
- Subtitles
- Magnets and Instant Create
- Drive Operations
- Delta Sync
- Uploads and Offline Tasks
- Errors and Retries
Understanding it
Working on it