ScriptCaptureDemo.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProMovieCapture.Demos
  7. {
  8. /// <summary>
  9. /// Demo code to create and write frames manually into a movie using the low-level API via scripting
  10. /// </summary>
  11. public class ScriptCaptureDemo : MonoBehaviour
  12. {
  13. private const string X264CodecName = "x264vfw - H.264/MPEG-4 AVC codec";
  14. private const string FallbackCodecName = "Uncompressed";
  15. /*[SerializeField]
  16. private int _width = 512;
  17. [SerializeField]
  18. private int _height = 512;
  19. [SerializeField]
  20. private int _frameRate = 30;
  21. [SerializeField]
  22. private string _filePath;*/
  23. // State
  24. private Codec _videoCodec;
  25. private int _encoderHandle;
  26. private void Start()
  27. {
  28. if (NativePlugin.Init())
  29. {
  30. // Find the index for the video codec
  31. _videoCodec = CodecManager.FindCodec(CodecType.Video, X264CodecName);
  32. if (_videoCodec == null)
  33. {
  34. _videoCodec = CodecManager.FindCodec(CodecType.Video, FallbackCodecName);
  35. }
  36. }
  37. else
  38. {
  39. this.enabled = false;
  40. }
  41. }
  42. private void OnDestroy()
  43. {
  44. NativePlugin.Deinit();
  45. }
  46. public void CreateVideoFromByteArray(string filePath, int width, int height, int frameRate)
  47. {
  48. byte[] frameData = new byte[width * height * 4];
  49. GCHandle frameHandle = GCHandle.Alloc(frameData, GCHandleType.Pinned);
  50. // Start the recording session
  51. int encoderHandle = NativePlugin.CreateRecorderVideo(filePath, (uint)width, (uint)height, frameRate, (int)NativePlugin.PixelFormat.RGBA32, false, false, _videoCodec.Index, AudioCaptureSource.None, 0, 0, -1, -1, true, null);
  52. if (encoderHandle >= 0)
  53. {
  54. NativePlugin.Start(encoderHandle);
  55. // Write out 100 frames
  56. int numFrames = 100;
  57. for (int i = 0; i < numFrames; i++)
  58. {
  59. // TODO: fill the byte array with your own data :)
  60. // Wait for the encoder to be ready for the next frame
  61. int numAttempts = 32;
  62. while (numAttempts > 0)
  63. {
  64. if (NativePlugin.IsNewFrameDue(encoderHandle))
  65. {
  66. // Encode the new frame
  67. NativePlugin.EncodeFrame(encoderHandle, frameHandle.AddrOfPinnedObject());
  68. break;
  69. }
  70. System.Threading.Thread.Sleep(1);
  71. numAttempts--;
  72. }
  73. }
  74. // End the session
  75. NativePlugin.Stop(encoderHandle, false);
  76. NativePlugin.FreeRecorder(encoderHandle);
  77. }
  78. if (frameHandle.IsAllocated)
  79. {
  80. frameHandle.Free();
  81. }
  82. }
  83. }
  84. }