ScriptCaptureDemo.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2012-2017 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. /*[SerializeField]
  15. private int _width = 512;
  16. [SerializeField]
  17. private int _height = 512;
  18. [SerializeField]
  19. private int _frameRate = 30;
  20. [SerializeField]
  21. private string _filePath;*/
  22. // State
  23. private int _videoCodecIndex;
  24. private int _encoderHandle;
  25. private void Start()
  26. {
  27. if (NativePlugin.Init())
  28. {
  29. // Find the index for the video codec
  30. _videoCodecIndex = FindVideoCodecIndex(X264CodecName);
  31. }
  32. else
  33. {
  34. this.enabled = false;
  35. }
  36. }
  37. private void OnDestroy()
  38. {
  39. NativePlugin.Deinit();
  40. }
  41. public void CreateVideoFromByteArray(string filePath, int width, int height, int frameRate)
  42. {
  43. byte[] frameData = new byte[width * height * 4];
  44. GCHandle frameHandle = GCHandle.Alloc(frameData, GCHandleType.Pinned);
  45. // Start the recording session
  46. int encoderHandle = NativePlugin.CreateRecorderAVI(filePath, (uint)width, (uint)height, frameRate, (int)NativePlugin.PixelFormat.RGBA32, false, _videoCodecIndex, false, 0, 0, -1, -1, false, false, false);
  47. if (encoderHandle >= 0)
  48. {
  49. NativePlugin.Start(encoderHandle);
  50. // Write out 100 frames
  51. int numFrames = 100;
  52. for (int i = 0; i < numFrames; i++)
  53. {
  54. // TODO: fill the byte array with your own data :)
  55. // Wait for the encoder to be ready for the next frame
  56. int numAttempts = 32;
  57. while (numAttempts > 0)
  58. {
  59. if (NativePlugin.IsNewFrameDue(encoderHandle))
  60. {
  61. // Encode the new frame
  62. NativePlugin.EncodeFrame(encoderHandle, frameHandle.AddrOfPinnedObject());
  63. break;
  64. }
  65. System.Threading.Thread.Sleep(1);
  66. numAttempts--;
  67. }
  68. }
  69. // End the session
  70. NativePlugin.Stop(encoderHandle, false);
  71. NativePlugin.FreeRecorder(encoderHandle);
  72. }
  73. if (frameHandle.IsAllocated)
  74. {
  75. frameHandle.Free();
  76. }
  77. }
  78. private static int FindVideoCodecIndex(string name)
  79. {
  80. int result = -1;
  81. int numVideoCodecs = NativePlugin.GetNumAVIVideoCodecs();
  82. for (int i = 0; i < numVideoCodecs; i++)
  83. {
  84. if (NativePlugin.GetAVIVideoCodecName(i) == name)
  85. {
  86. result = i;
  87. break;
  88. }
  89. }
  90. return result;
  91. }
  92. }
  93. }