This commit is contained in:
RopeBunnyVJ 2025-02-05 15:58:45 -08:00
parent d4a3e9355e
commit 7f75b31176
4 changed files with 144 additions and 6 deletions

View File

@ -66,20 +66,21 @@ public class RT3script : MonoBehaviour
} }
// Update is called once per frame // Update is called once per frame
void Update() void LateUpdate()
{ {
float expectedFrames = Time.timeSinceLevelLoad * targetFrameRate; float expectedFrames = Time.timeSinceLevelLoad * targetFrameRate;
if (frameCount < expectedFrames) { if (frameCount < expectedFrames) {
// Graphics.CopyTexture(colorImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 0, 0); // 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); // Graphics.CopyTexture(depthImage, 0, 0, 0, 0, 256, 256, outputImage, 0, 0, 256, 0);
RenderTexture.active = colorImage; // RenderTexture.active = colorImage;
outputImage.ReadPixels(new Rect(0, 0, 256, 256), 0, 0); // outputImage.ReadPixels(new Rect(0, 0, 256, 256), 0, 0);
outputImage.Apply(); // outputImage.Apply();
RenderTexture.active = depthImage; 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(); outputImage.Apply();
RenderTexture.active = null;
// Get the raw pixel data and write it to FFmpeg's input stream // Get the raw pixel data and write it to FFmpeg's input stream
byte[] frameBytes = outputImage.GetRawTextureData(); byte[] frameBytes = outputImage.GetRawTextureData();

129
Assets/RT3script.cs.newbak Normal file
View File

@ -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;
}
}
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 64763bbc0a4c16443a107fefed83ff75
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -154,7 +154,8 @@ PlayerSettings:
resetResolutionOnWindowResize: 0 resetResolutionOnWindowResize: 0
androidSupportedAspectRatio: 1 androidSupportedAspectRatio: 1
androidMaxAspectRatio: 2.1 androidMaxAspectRatio: 2.1
applicationIdentifier: {} applicationIdentifier:
Standalone: com.DefaultCompany.DNN-Depth-Capture
buildNumber: buildNumber:
Standalone: 0 Standalone: 0
iPhone: 0 iPhone: 0