TensorStack.Media
0.15.3
Prefix Reserved
See the version list below for details.
dotnet add package TensorStack.Media --version 0.15.3
NuGet\Install-Package TensorStack.Media -Version 0.15.3
<PackageReference Include="TensorStack.Media" Version="0.15.3" />
<PackageVersion Include="TensorStack.Media" Version="0.15.3" />
<PackageReference Include="TensorStack.Media" />
paket add TensorStack.Media --version 0.15.3
#r "nuget: TensorStack.Media, 0.15.3"
#:package TensorStack.Media@0.15.3
#addin nuget:?package=TensorStack.Media&version=0.15.3
#tool nuget:?package=TensorStack.Media&version=0.15.3
TensorStack.Media
Loading, saving and streaming of images, audio and video, using SkiaSharp, OpenCvSharp and FFmpeg.
Nuget Package
Setup
Video and audio use FFmpeg, set its location and the temporary directory once at startup (defaults to ffmpeg.exe, ffprobe.exe and Temp in the working directory)
MediaManager.Initialize("ffmpeg.exe", "ffprobe.exe", "Temp");
An FFmpeg failure throws with its error output, a cancelled operation stops the FFmpeg process.
Images
ImageInput
ImageInput extends ImageTensor with loading and saving images to disk
var image = new ImageInput("Input.png");
var resized = new ImageInput("Input.png", 1024, 1024, ResizeMode.Crop);
await image.SaveAsync("Output.png");
// Any ImageTensor can be saved directly
await imageTensor.SaveAsync("Output.png");
Video
VideoInput
VideoInput extends VideoSequence, a buffered video with all frames and audio in memory
var video = new VideoInput("Input.mp4");
Console.WriteLine($"{video.FrameCount} frames @ {video.FrameRate}fps");
await video.SaveAsync("Output.mp4");
VideoInputStream
VideoInputStream is a video backed by a file stream, loading frames on demand so long videos can be processed one frame at a time
var stream = await VideoInputStream.CreateAsync("Input.mp4");
await foreach (var frame in stream.GetAsync())
{
// Process frame
}
// Or chain a pipeline and save the result, with the source audio track
await pipeline.RunAsync(new UpscaleStreamOptions { Stream = stream.GetAsync() }).SaveAsync("Output.mp4", audioSourceFile: stream.SourceFile);
// Process every frame with buffered read, process and write, the audio track is written in the same pass
var result = await stream.ProcessAsync("Output.mp4", async frame => await ProcessFrameAsync(frame));
// Read frames into a VideoSequence, with the audio trimmed to the frames read
var sequence = await stream.GetSequenceAsync(512, 512, frameRateOverride: 24, ResizeMode.Crop, maxFrames: 81, includeAudio: true);
// Replace the audio track with another file or a mixed AudioTimeline
result = await result.AddAudioAsync("Music.mp3");
HasAudio/Audio- The audio track info, null when the video has no audio
VideoSequence
GetSequence- A range of frames by index or time, the audio is trimmed to match
VideoManager
LoadVideoInfoAsync- Video info and thumbnailLoadVideoSequenceAsync- Load aVideoSequence, with optional size and frame rate overridesWriteVideoStreamAsync- Write a frame stream to a file, with optional frame processor and audio source
Video encoding
Video is written by FFmpeg as H.264 (MP4) with the first encoder that works at the output size, the result is cached per size:
NVIDIA h264_nvenc, AMD h264_amf, Intel h264_qsv, then the CPU encoders libopenh264 and mpeg4 (MPEG-4 Part 2, for sizes the H.264 encoders can't take).
Odd sizes are rounded down to even. An audio track is encoded as AAC and padded or trimmed to the video length, in the same pass as the frames.
OpenCvSharp is used for reading video frames and cameras only.
Audio
AudioInput
AudioInput extends AudioTensor with loading and saving audio to disk. Audio is interleaved [Samples, Channels], see AudioTensor layout
// Any audio or video file, converted to the sample rate and channels (default 16 kHz mono)
var audio = await AudioInput.CreateAsync("Input.mp3", sampleRate: 44100, channels: 2);
await audio.SaveAsync("Output.wav");
// Any AudioTensor can be saved, resampled or downmixed
await audioTensor.Resample(24000).ToMono().SaveAsync("Output.wav");
AudioInputStream
AudioInputStream is audio backed by a file, loading data on demand
SampleRate/Channels/Duration/AudioCodec- The audio stream infoGetAsync- Load asAudioInput, at a sample rate and channel count or the source format when not setMoveAsync/CopyAsync- Move or copy the source fileCreateAsync- Open a file
AudioManager
LoadInfoAsync- Audio stream info, null when the file has no audioLoadTensorAsync- Load the audio track of an audio or video file as anAudioTensor, decoded as 32-bit floatSaveAudio- Save anAudioTensorAddAudioAsync- Add the audio track of a file, or a mixedAudioTimeline, to a video. The video is not encoded again, the audio is encoded as AAC and padded or trimmed to the video length, a source with no audio is skippedCreateAudioTimelineAsync- Mix anAudioTimelineof segments to a file
Capture
CameraCapture
CameraCapture reads the cameras attached to the system, with OpenCvSharp over Windows Media Foundation
var cameras = CameraCapture.GetDevices();
// Frames are BGR24, the camera uses the closest size and frame rate it supports
await foreach (var frame in CameraCapture.CaptureAsync(cameras[0], 1280, 720, 30, stopToken))
{
using (frame)
{
var image = frame.ToTensor();
var cropped = frame.Resize(512, 512, ResizeMode.Crop);
}
}
Cancelling the stop token ends the stream without an exception. Frame buffers are pooled, dispose each frame when it is no longer needed.
CameraRecording
CameraRecording keeps camera frames in memory as JPEG and reads them back at a constant frame rate, cameras send frames at an uneven rate that drops in low light
var recording = new CameraRecording();
recording.Add(frame);
// A VideoSequence or a video file, at a frame rate and an exact frame count (zero for the camera rate and the recorded length)
var sequence = await recording.ToSequenceAsync(frameRate: 16, frameCount: 81, audio: audioRecording);
await recording.SaveAsync("Output.mp4", frameRate: 16, frameCount: 81, audio: audioRecording);
AudioCapture
AudioCapture reads the Windows recording devices, Windows converts the device format to the requested format
var microphones = AudioCapture.GetDevices();
// Chunks are AudioTensors with the time they were captured, a null device is the default recording device
await foreach (var chunk in AudioCapture.CaptureAsync(sampleRate: 16000, channels: 1, device: microphones[0], stopToken: stopToken))
{
// Process chunk
}
AudioRecording
AudioRecording keeps captured chunks in memory. GetAudio returns the audio for a range of capture time (Stopwatch timestamps), so it lines up with camera frames captured on the same clock.
Pass one to a CameraRecording to add the audio recorded with the frames to its VideoSequence or video file.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows10.0.17763 is compatible. |
-
net10.0-windows10.0.17763
- OpenCvSharp5 (>= 5.0.0.20261109)
- OpenCvSharp5.runtime.win (>= 5.0.0.20261109)
- SkiaSharp (>= 4.155.0)
- TensorStack.Common (>= 0.15.3)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on TensorStack.Media:
| Package | Downloads |
|---|---|
|
TensorStack.WPF
Package Description |
|
|
TensorStack.StableDiffusionCpp
.NET Wrapper API for StableDiffusion.cpp |
|
|
TensorStack.LlamaCpp
.NET Wrapper API for llama.cpp |
|
|
TensorStack.VisionCpp
.NET Wrapper API for vision.cpp and ncnn |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.16.1 | 80 | 11/30/2026 |
| 0.16.0 | 75 | 11/30/2026 |
| 0.15.3 | 94 | 11/27/2026 |
| 0.15.1 | 137 | 11/21/2026 |
| 0.15.0 | 144 | 11/18/2026 |
| 0.14.14 | 126 | 11/17/2026 |
| 0.14.13 | 128 | 11/16/2026 |
| 0.14.12 | 111 | 11/15/2026 |
| 0.14.11 | 110 | 11/15/2026 |
| 0.14.10 | 117 | 11/14/2026 |
| 0.14.9 | 119 | 11/13/2026 |
| 0.14.8 | 107 | 11/10/2026 |
| 0.14.7 | 108 | 11/8/2026 |
| 0.14.6 | 119 | 11/5/2026 |
| 0.14.5 | 124 | 11/1/2026 |
| 0.14.4 | 204 | 10/29/2026 |
| 0.14.3 | 129 | 10/28/2026 |
| 0.14.2 | 152 | 10/20/2026 |
| 0.14.1 | 149 | 10/16/2026 |
| 0.13.13 | 138 | 10/11/2026 |