Search Docs
Loading...
Skip to content

For Audio Processing

Export audio from pages, video blocks, audio blocks, and tracks to WAV, MP4 or WebM format for external processing, transcription, or analysis.

The exportAudio API allows you to extract audio from any block that contains audio content. This is particularly useful when integrating with external audio processing services like speech-to-text transcription, audio enhancement, or music analysis platforms.

Audio can be exported from multiple block types:

  • Page blocks - Export the complete mixed audio timeline
  • Video blocks - Extract audio tracks from videos
  • Audio blocks - Export standalone audio content
  • Track blocks - Export audio from specific timeline tracks

Export Audio#

Export audio from any block using the exportAudio API:

val page = requireNotNull(engine.scene.getCurrentPage())
val audioData = engine.block.exportAudio(
block = page,
mimeType = MimeType.AUDIO_WAV,
options = ExportAudioOptions(timeOffset = 0.0, duration = 10.0),
progressCallback = { progress ->
Log.d("AudioExport", "Encoded ${progress.encodedFrames} of ${progress.totalFrames} frames")
},
)
Log.d("AudioExport", "Exported ${audioData.remaining()} bytes")

exportAudio suspends until the file is complete. The export runs on a copy of the scene in a background engine, like export and exportVideo, so the exported block must be part of the scene. Cancel the calling coroutine to stop it.

Export Options#

Choose the file type with mimeType:

  • MimeType.AUDIO_WAV - Uncompressed audio. This is the default.
  • MimeType.AUDIO_MP4 - Compressed AAC audio.
  • MimeType.AUDIO_WEBM - Compressed Opus audio.

Configure the rest with ExportAudioOptions:

  • timeOffset - Start time in seconds. The default is 0.
  • duration - Length to export in seconds. The default, null, exports the whole block.
  • sampleRate and numberOfChannels - The export writes 48000 Hz stereo. Another value fails the export.
  • audioCodec - The codec of an MP4 or WebM file. AudioCodec.AUTO picks AAC for MP4 and Opus for WebM.

The optional progressCallback receives the rendered, encoded and total frame counts.

Find Audio Sources#

To find blocks with audio in your scene:

// Find audio blocks
val audioBlocks = engine.block.findByType(DesignBlockType.Audio)
// Find video fills with audio
val videoFills = engine.block.findByType(FillType.Video.key)
val videosWithAudio = videoFills.filter { videoFill ->
// The track count needs the loaded video, and a fill without a file has no tracks to load.
try {
engine.block.forceLoadAVResource(videoFill)
engine.block.getAudioTrackCountFromVideo(videoFill) > 0
} catch (e: EngineException) {
false
}
}

Working with Multi-Track Video Audio#

Videos can contain multiple audio tracks (e.g., different languages). CE.SDK provides APIs to inspect and extract specific tracks.

Check audio track count#

val videoFillId = engine.block.findByType(FillType.Video.key).first()
val trackCount = engine.block.getAudioTrackCountFromVideo(videoFillId)
Log.d("AudioExport", "Video has $trackCount audio track(s)")

Get track information#

val audioTracks = engine.block.getAudioInfoFromVideo(videoFillId)
audioTracks.forEachIndexed { index, track ->
Log.d("AudioExport", """
Track $index:
- Channels: ${track.channels} // 1=mono, 2=stereo
- Sample Rate: ${track.sampleRate} Hz
- Language: ${track.language.ifEmpty { "unknown" }}
- Label: ${track.trackName.ifEmpty { "Track $index" }}
""".trimIndent())
}

Extract a specific track#

// Create audio block from track 0 (first track)
val audioBlockId = engine.block.createAudioFromVideo(videoFillId, 0)
// The export runs on a copy of the scene, so the new block must be part of it
engine.block.appendChild(parent = page, child = audioBlockId)
// Export just this track's audio, then remove the block again
val trackAudioData = engine.block.exportAudio(audioBlockId, MimeType.AUDIO_WAV)
engine.block.destroy(audioBlockId)

Extract all tracks#

// Create audio blocks for all tracks, and make them part of the scene
val audioBlockIds = engine.block.createAudiosFromVideo(videoFillId)
audioBlockIds.forEach { engine.block.appendChild(parent = page, child = it) }
// Export each track with one background engine
val tracksData = engine.block.exportAudio(audioBlockIds, MimeType.AUDIO_WAV)
tracksData.forEachIndexed { i, trackData ->
Log.d("AudioExport", "Track $i: ${trackData.remaining()} bytes")
}
audioBlockIds.forEach { engine.block.destroy(it) }

Complete Workflow: Audio to Captions#

A common workflow is to export audio, send it to a transcription service, and use the returned captions in your scene.

Step 1: Export Audio#

val page = requireNotNull(engine.scene.getCurrentPage())
val audioBuffer = engine.block.exportAudio(page, MimeType.AUDIO_WAV)
// The buffer can be a direct buffer without a backing array, so copy the bytes out with `get`.
val audioData = ByteArray(audioBuffer.remaining()).also { audioBuffer.get(it) }

Step 2: Send to Transcription Service#

Send the audio to a service that returns SubRip (SRT) format captions:

Provide transcriptionEndpoint and transcriptionAuthorizationHeader from your app’s configuration. Point the endpoint at an app-owned backend or proxy, and keep third-party service credentials out of the Android client.

suspend fun transcribeAudio(
audioData: ByteArray,
transcriptionEndpoint: String,
authorizationHeader: String,
): String = withContext(Dispatchers.IO) {
val client = OkHttpClient()
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"audio",
"audio.wav",
audioData.toRequestBody("audio/wav".toMediaType())
)
.addFormDataPart("format", "srt")
.build()
val request = Request.Builder()
.url(transcriptionEndpoint)
.addHeader("Authorization", authorizationHeader)
.post(requestBody)
.build()
val response = client.newCall(request).execute()
response.body?.string() ?: throw Exception("Empty response")
}
val srtContent = transcribeAudio(
audioData = audioData,
transcriptionEndpoint = transcriptionEndpoint,
authorizationHeader = transcriptionAuthorizationHeader,
)

Step 3: Import Captions from SRT#

Use the built-in API to create caption blocks from the SRT response:

import java.io.File
// Save SRT to temporary file
val tempFile = File.createTempFile("captions", ".srt")
tempFile.writeText(srtContent)
// Import captions from file URI
val uri = tempFile.toURI().toString()
val captions = engine.block.createCaptionsFromURI(uri)
// Clean up temporary file
tempFile.delete()
// Add captions to page
val captionTrack = engine.block.create(DesignBlockType.CaptionTrack)
captions.forEach { caption ->
engine.block.appendChild(captionTrack, caption)
}
engine.block.appendChild(page, captionTrack)
// Center the first caption as a reference point
engine.block.alignHorizontally(listOf(captions[0]), HorizontalBlockAlignment.CENTER)
engine.block.alignVertically(listOf(captions[0]), VerticalBlockAlignment.CENTER)

Other Processing Services#

Audio export also supports these workflows:

  • Audio enhancement - Noise removal, normalization
  • Music analysis - Tempo, key, beat detection
  • Language detection - Identify spoken language
  • Speaker diarization - Identify who spoke when

Next Steps#

  • To MP4 — Export video compositions as MP4 files with configurable encoding options, progress tracking, and resolution control.
  • Add Captions — Add synchronized captions to video scenes and import subtitle files.
  • Control Audio and Video — Play, pause, seek, and preview audio and video content programmatically.