From 7f75b3117611680b20efa49db38a180579be6fc9 Mon Sep 17 00:00:00 2001 From: RopeBunnyVJ Date: Wed, 5 Feb 2025 15:58:45 -0800 Subject: [PATCH] bump --- Assets/RT3script.cs | 11 ++- Assets/RT3script.cs.newbak | 129 ++++++++++++++++++++++++++ Assets/RT3script.cs.newbak.meta | 7 ++ ProjectSettings/ProjectSettings.asset | 3 +- 4 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 Assets/RT3script.cs.newbak create mode 100644 Assets/RT3script.cs.newbak.meta diff --git a/Assets/RT3script.cs b/Assets/RT3script.cs index f4d43cd..15848cd 100644 --- a/Assets/RT3script.cs +++ b/Assets/RT3script.cs @@ -66,20 +66,21 @@ public class RT3script : MonoBehaviour } // Update is called once per frame - void Update() + void LateUpdate() { float expectedFrames = Time.timeSinceLevelLoad * targetFrameRate; if (frameCount < expectedFrames) { // Graphics.CopyTexture(colorImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 0, 0); // Graphics.CopyTexture(depthImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 256, 0); - RenderTexture.active = colorImage; - outputImage.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); - outputImage.Apply(); + // RenderTexture.active = colorImage; + // outputImage.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); + // outputImage.Apply(); RenderTexture.active = depthImage; - outputImage.ReadPixels(new Rect(0, 0, 256, 256), 256, 0); + outputImage.ReadPixels(new Rect(0, 0, depthImage.width, depthImage.height), 0, 0); outputImage.Apply(); + RenderTexture.active = null; // Get the raw pixel data and write it to FFmpeg's input stream byte[] frameBytes = outputImage.GetRawTextureData(); diff --git a/Assets/RT3script.cs.newbak b/Assets/RT3script.cs.newbak new file mode 100644 index 0000000..86f88a0 --- /dev/null +++ b/Assets/RT3script.cs.newbak @@ -0,0 +1,129 @@ +using System.Collections; +using System.Diagnostics; +using System.IO; +using UnityEngine; + +public class FFmpegVideoRecorder : MonoBehaviour +{ + public Camera targetCamera; // Assign your camera in the Inspector + public int outputWidth = 1920; // Output video width + public int outputHeight = 1080; // Output video height + public int outputFrameRate = 30; // Output video frame rate + public string ffmpegPath = "ffmpeg"; // Path to the FFmpeg binary + public string outputFilePath = "output.mp4"; // Path to save the video + + private RenderTexture renderTexture; + private Texture2D frameTexture; + private Process ffmpegProcess; + private BinaryWriter ffmpegInputStream; + + private void Start() + { + // Setup FFmpeg process + StartFFmpeg(); + + // Create a RenderTexture and Texture2D for capturing frames + renderTexture = new RenderTexture(outputWidth, outputHeight, 24); + frameTexture = new Texture2D(outputWidth, outputHeight, TextureFormat.RGB24, false); + + // Set the camera's target texture to render to the RenderTexture + targetCamera.targetTexture = renderTexture; + } + + private void LateUpdate() + { + // Capture the camera's current frame + RenderTexture currentRT = RenderTexture.active; + RenderTexture.active = renderTexture; + targetCamera.Render(); + + // Read the pixels from the RenderTexture into the Texture2D + frameTexture.ReadPixels(new Rect(0, 0, outputWidth, outputHeight), 0, 0); + frameTexture.Apply(); + + // Write the frame's raw RGB data to the FFmpeg input stream + byte[] frameData = frameTexture.GetRawTextureData(); + if (ffmpegInputStream != null) + { + ffmpegInputStream.Write(frameData); + } + + // Restore the previous active RenderTexture + RenderTexture.active = currentRT; + } + + private void OnApplicationQuit() + { + StopFFmpeg(); + + // Clean up temporary objects + if (renderTexture != null) + { + RenderTexture.ReleaseTemporary(renderTexture); + } + if (frameTexture != null) + { + Destroy(frameTexture); + } + } + + private void StartFFmpeg() + { + var ffmpegStartInfo = new ProcessStartInfo + { + FileName = ffmpegPath, + Arguments = $"-y -f rawvideo -vcodec rawvideo -pix_fmt rgb24 -s {outputWidth}x{outputHeight} -r {outputFrameRate} -i - -an -vcodec libx264 -pix_fmt yuv420p \"{outputFilePath}\"", + UseShellExecute = false, + RedirectStandardInput = true, + RedirectStandardOutput = false, + RedirectStandardError = true, // Optional: Redirect standard error for debugging + CreateNoWindow = true + }; + + ffmpegProcess = new Process + { + StartInfo = ffmpegStartInfo + }; + + ffmpegProcess.Start(); + + // Attach the input stream + ffmpegInputStream = new BinaryWriter(ffmpegProcess.StandardInput.BaseStream); + } + + private void StopFFmpeg() + { + if (ffmpegInputStream != null) + { + try + { + // Close the input stream + ffmpegInputStream.Close(); + } + catch + { + // Ignore any errors when closing the stream + } + ffmpegInputStream = null; + } + + if (ffmpegProcess != null) + { + try + { + // Wait for the FFmpeg process to exit + ffmpegProcess.WaitForExit(); + } + catch + { + // Ignore any errors when waiting for the process + } + finally + { + ffmpegProcess.Close(); + } + + ffmpegProcess = null; + } + } +} \ No newline at end of file diff --git a/Assets/RT3script.cs.newbak.meta b/Assets/RT3script.cs.newbak.meta new file mode 100644 index 0000000..cb7437e --- /dev/null +++ b/Assets/RT3script.cs.newbak.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 64763bbc0a4c16443a107fefed83ff75 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 25506f6..2789cc8 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -154,7 +154,8 @@ PlayerSettings: resetResolutionOnWindowResize: 0 androidSupportedAspectRatio: 1 androidMaxAspectRatio: 2.1 - applicationIdentifier: {} + applicationIdentifier: + Standalone: com.DefaultCompany.DNN-Depth-Capture buildNumber: Standalone: 0 iPhone: 0