Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 35489d6

Browse files
committed
Support video transparency
1 parent 5627a18 commit 35489d6

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

OnnxStack.Core/Video/OnnxVideo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public void Resize(int height, int width)
137137
/// <param name="filename">The filename.</param>
138138
/// <param name="cancellationToken">The cancellation token.</param>
139139
/// <returns></returns>
140-
public Task SaveAsync(string filename, CancellationToken cancellationToken = default)
140+
public Task SaveAsync(string filename, bool preserveTransparency = false, CancellationToken cancellationToken = default)
141141
{
142-
return VideoHelper.WriteVideoFramesAsync(this, filename, cancellationToken);
142+
return VideoHelper.WriteVideoFramesAsync(this, filename, preserveTransparency, cancellationToken);
143143
}
144144

145145

OnnxStack.Core/Video/VideoHelper.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public static void SetConfiguration(OnnxStackConfig configuration)
3232
/// <param name="onnxVideo">The onnx video.</param>
3333
/// <param name="filename">The filename.</param>
3434
/// <param name="cancellationToken">The cancellation token.</param>
35-
public static async Task WriteVideoFramesAsync(OnnxVideo onnxVideo, string filename, CancellationToken cancellationToken = default)
35+
public static async Task WriteVideoFramesAsync(OnnxVideo onnxVideo, string filename, bool preserveTransparency = false, CancellationToken cancellationToken = default)
3636
{
37-
await WriteVideoFramesAsync(onnxVideo.Frames, filename, onnxVideo.FrameRate, onnxVideo.AspectRatio, cancellationToken);
37+
await WriteVideoFramesAsync(onnxVideo.Frames, filename, onnxVideo.FrameRate, onnxVideo.AspectRatio, preserveTransparency, cancellationToken);
3838
}
3939

4040

@@ -45,11 +45,11 @@ public static async Task WriteVideoFramesAsync(OnnxVideo onnxVideo, string filen
4545
/// <param name="filename">The filename.</param>
4646
/// <param name="frameRate">The frame rate.</param>
4747
/// <param name="cancellationToken">The cancellation token.</param>
48-
public static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImages, string filename, float frameRate = 15, CancellationToken cancellationToken = default)
48+
public static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImages, string filename, float frameRate = 15, bool preserveTransparency = false, CancellationToken cancellationToken = default)
4949
{
5050
var firstImage = onnxImages.First();
5151
var aspectRatio = (double)firstImage.Width / firstImage.Height;
52-
await WriteVideoFramesAsync(onnxImages, filename, frameRate, aspectRatio, cancellationToken);
52+
await WriteVideoFramesAsync(onnxImages, filename, frameRate, aspectRatio, preserveTransparency, cancellationToken);
5353
}
5454

5555

@@ -61,12 +61,12 @@ public static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImages
6161
/// <param name="frameRate">The frame rate.</param>
6262
/// <param name="aspectRatio">The aspect ratio.</param>
6363
/// <param name="cancellationToken">The cancellation token.</param>
64-
private static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImages, string filename, float frameRate, double aspectRatio, CancellationToken cancellationToken = default)
64+
private static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImages, string filename, float frameRate, double aspectRatio, bool preserveTransparency, CancellationToken cancellationToken = default)
6565
{
6666
if (File.Exists(filename))
6767
File.Delete(filename);
6868

69-
using (var videoWriter = CreateWriter(filename, frameRate, aspectRatio))
69+
using (var videoWriter = CreateWriter(filename, frameRate, aspectRatio, preserveTransparency))
7070
{
7171
// Start FFMPEG
7272
videoWriter.Start();
@@ -91,12 +91,12 @@ private static async Task WriteVideoFramesAsync(IEnumerable<OnnxImage> onnxImage
9191
/// <param name="frameRate">The frame rate.</param>
9292
/// <param name="aspectRatio">The aspect ratio.</param>
9393
/// <param name="cancellationToken">The cancellation token.</param>
94-
public static async Task WriteVideoStreamAsync(VideoInfo videoInfo, IAsyncEnumerable<OnnxImage> videoStream, string filename, CancellationToken cancellationToken = default)
94+
public static async Task WriteVideoStreamAsync(VideoInfo videoInfo, IAsyncEnumerable<OnnxImage> videoStream, string filename, bool preserveTransparency = false, CancellationToken cancellationToken = default)
9595
{
9696
if (File.Exists(filename))
9797
File.Delete(filename);
9898

99-
using (var videoWriter = CreateWriter(filename, videoInfo.FrameRate, videoInfo.AspectRatio))
99+
using (var videoWriter = CreateWriter(filename, videoInfo.FrameRate, videoInfo.AspectRatio, preserveTransparency))
100100
{
101101
// Start FFMPEG
102102
videoWriter.Start();
@@ -323,11 +323,13 @@ private static Process CreateReader(string inputFile, float fps)
323323
/// <param name="fps">The FPS.</param>
324324
/// <param name="aspectRatio">The aspect ratio.</param>
325325
/// <returns></returns>
326-
private static Process CreateWriter(string outputFile, float fps, double aspectRatio)
326+
private static Process CreateWriter(string outputFile, float fps, double aspectRatio, bool preserveTransparency)
327327
{
328328
var ffmpegProcess = new Process();
329+
var codec = preserveTransparency ? "png" : "libx264";
330+
var format = preserveTransparency ? "yuva420p" : "yuv420p";
329331
ffmpegProcess.StartInfo.FileName = _configuration.FFmpegPath;
330-
ffmpegProcess.StartInfo.Arguments = $"-hide_banner -loglevel error -framerate {fps:F4} -i - -c:v libx264 -movflags +faststart -vf format=yuv420p -aspect {aspectRatio} {outputFile}";
332+
ffmpegProcess.StartInfo.Arguments = $"-hide_banner -loglevel error -framerate {fps:F4} -i - -c:v {codec} -movflags +faststart -vf format={format} -aspect {aspectRatio} {outputFile}";
331333
ffmpegProcess.StartInfo.RedirectStandardInput = true;
332334
ffmpegProcess.StartInfo.UseShellExecute = false;
333335
ffmpegProcess.StartInfo.CreateNoWindow = true;

0 commit comments

Comments
 (0)