NCKitVideoProcessor
Adding a cleaned audio track to an MP4 is an Android API. On iOS, denoise a file with NCKitFileProcessor.
Adds a noise-cancelled audio track to an MP4. The picture is not copied. The new track is AAC-LC, 48 kHz, mono, 128 kbps, and it becomes the first audio track. Existing audio stays as an alternate.
Camera After stop already has raw microphone PCM: call addDenoisedTrack. A picked video does not: processVideo decodes it first. Both calls are blocking. Run them off the main thread. The file must be writable.
Gold polish is on. The 30 ms model delay is removed inside this path. Do not also run NCKitAudioNormalizer.
Denoising uses about 60 second windows and NCKitFileProcessor.recommendedThreads().
APIโ
object NCKitVideoProcessor {
@Throws(NCKitException::class, CancellationException::class)
fun addDenoisedTrack(
videoFile: File,
pcmFile: File,
processor: NCKitProcessor,
rewriteAsMono48k: Boolean = false,
onDenoised: NCKitOrderedSink? = null,
onProgress: (Double) -> Unit = {},
)
@Throws(NCKitException::class, CancellationException::class)
fun decodeToPcm(
videoFile: File,
pcmFile: File,
onProgress: (Double) -> Unit = {},
): Long
@Throws(NCKitException::class, CancellationException::class)
fun processVideo(
videoFile: File,
processor: NCKitProcessor,
onDenoised: NCKitOrderedSink? = null,
onProgress: (Double) -> Unit = {},
): Long
fun markAudioAlternates(videoFile: File): Int
}
pcmFile for addDenoisedTrack is raw little-endian 16-bit PCM, 48 kHz mono, with no WAV header.
rewriteAsMono48k writes the new track as AAC-LC 48 kHz mono. Pass true for a gallery file whose original audio is a different rate or channel count. Leave it false when the file was recorded as 48 kHz mono AAC. processVideo always rewrites.
onDenoised receives cleaned samples in order. Finish with them before returning. onProgress is 0.0 to 1.0. For processVideo, decode is the first fifth and denoise is the rest. Throw CancellationException from onProgress to stop.
decodeToPcm and processVideo return the duration in milliseconds. processVideo deletes its temporary PCM. markAudioAlternates puts every audio track in one alternate group, leaves the first enabled, and returns how many audio tracks were patched. Call it after a live mux wrote the cleaned track first.
Picked videoโ
import com.fiveexceptions.nckit.NCKitProcessor
import com.fiveexceptions.nckit.NCKitVideoProcessor
NCKitVideoProcessor.processVideo(videoFile, processor) { fraction ->
// 0..1
}
When the app still needs the decoded audio, for a waveform or an A/B compare, keep the PCM and call the two steps yourself:
val pcm = File(videoFile.parentFile, "gallery.pcm")
NCKitVideoProcessor.decodeToPcm(videoFile, pcm) { fraction ->
// decode progress
}
NCKitVideoProcessor.addDenoisedTrack(
videoFile = videoFile,
pcmFile = pcm,
processor = processor,
rewriteAsMono48k = true,
) { fraction ->
// denoise progress
}
Camera after stopโ
NCKitVideoProcessor.addDenoisedTrack(
videoFile,
pcmFile,
processor,
) { fraction ->
// denoise progress
}
pcmFile is the raw 48 kHz mono PCM16 kept while the camera was recording. rewriteAsMono48k stays false.
Live recordingโ
Denoise the microphone with NCKitStreamProcessor and mux that audio as the first track. Then:
NCKitVideoProcessor.markAudioAlternates(videoFile)
The stream processor does not drop the 30 ms delay. Drop frameLength * 3 samples in the app, as the sample does.
Throwsโ
| Error | When |
|---|---|
CannotOpenInput | The video cannot be opened for decode |
CannotCreateOutput | The file is not writable, or AAC encoding does not finish |
UnsupportedFormat | No audio track, or the PCM file is empty |
ResampleFailed | Decode or resample to 48 kHz mono failed |
CancellationException | onProgress cancelled the call |