Search Unity

Video Control Unity Recorder from Script

Discussion in 'Audio & Video' started by EmmetOT, Mar 5, 2020.

  1. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Audio should be there. Do you hear it when you play your scene without recording?
    I've tested this with a WAV file playing on a GameObject and it works.
     
  2. jokerthecrack

    jokerthecrack

    Joined:
    Dec 8, 2016
    Posts:
    1



    IIIIIIIIIIIIIIIIII LLLLLLLLLLLLLLLLLOOOOOOOOOOOVEEEEEEEEEEEEEEEEEE YYYYYYYYYYOOOOOOOOOOOUUUUUUUUUUUUUUUU!!!!!!!!!!!!!!!!!!!
     
    unitybru likes this.
  3. LTPStudioXR

    LTPStudioXR

    Joined:
    Nov 1, 2016
    Posts:
    11
    @unitybru I'm trying to create an instance of FBXRecorderSettings like I can with AnimationRecorderSettings, but it doesn't show up. Do I need an additional namespace beyond UnityEditor.Recorder or are FBX settings not accessible atm?
     
  4. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Please share your code.
     
  5. deepnothing

    deepnothing

    Joined:
    Sep 8, 2021
    Posts:
    2
    @unitybru I am using this recorder script for a scene I have with a few hundred cameras. I am trying to capture 10 seconds of mp4 video from each one at runtime. How would I go about setting the source for the recorder as the camera name or tagname? thanks for your help

    Code (CSharp):
    1. using UnityEditor.Recorder;
    2. using UnityEditor.Recorder.Input;
    3. using UnityEngine;
    4.  
    5. public class Recorder : MonoBehaviour
    6. {
    7.     private void OnEnable()
    8.     {
    9.         for (int i = 0; i < 2; i++)
    10.         {
    11.             RecorderOptions.VerboseMode = true;
    12.  
    13.             var controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
    14.  
    15.             var TestRecorderController = new RecorderController(controllerSettings);
    16.  
    17.             var videoRecorder = ScriptableObject.CreateInstance<MovieRecorderSettings>();
    18.  
    19.             videoRecorder.name = "TestRecorder";
    20.             videoRecorder.Enabled = true;
    21.  
    22.             videoRecorder.AudioInputSettings.PreserveAudio = false;
    23.  
    24.             videoRecorder.OutputFile = "Recordings/" + $"Camera{i}";
    25.  
    26.  
    27.             controllerSettings.AddRecorderSettings(videoRecorder);
    28.             controllerSettings.SetRecordModeToTimeInterval(0, 60);
    29.             controllerSettings.FrameRate = 30;
    30.             TestRecorderController.PrepareRecording();
    31.             TestRecorderController.StartRecording();
    32.         }
    33.     }
    34. }
     
  6. Give_up

    Give_up

    Joined:
    Sep 28, 2021
    Posts:
    7
    how to change the location of the file saved using script?
     
  7. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Here is how to use the main camera:
    Code (CSharp):
    1. videoRecorder.ImageInputSettings = new CameraInputSettings
    2. {
    3.     Source = ImageSource.MainCamera,
    4.     OutputWidth = 320,
    5.     OutputHeight = 240
    6. };
    And for tagged cameras:

    Here is how to use the main camera:
    Code (CSharp):
    1. videoRecorder.ImageInputSettings = new CameraInputSettings
    2. {
    3.     Source = ImageSource.TaggedCamera,
    4.     OutputWidth = 320,
    5.     OutputHeight = 240,
    6.     CameraTag = "SomeCamTag"
    7. };
     
  8. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Change the value of MovieRecorderSettings.OutputFile but don't include the extension there.
     
  9. GeniusKoala

    GeniusKoala

    Joined:
    Oct 13, 2017
    Posts:
    97
    I tried every output paths I found on this tread but only relative paths and paths from C:// are working. Not even my local D:// works, the recorder windows always overwrite the path by C:/ at the root (even before recording) and obviously it can't record there. We have a server at work and its name is Y:/ but it won't record there either. I have no problem recording there from other software like MVN Animate Pro so is there a limitation on maybe the disk on which Unity is installed? I would like to record on our server and not on local if possible please.
     
  10. deepnothing

    deepnothing

    Joined:
    Sep 8, 2021
    Posts:
    2
    thank you, my recordings are coming out upside down, any way to flip vertical in the script?
     
  11. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Which version are you using? All the vertical flip issues were fixed in version 3.0.1.
     
  12. unity_1EAF334E09DF0BBC883D

    unity_1EAF334E09DF0BBC883D

    Joined:
    Aug 2, 2021
    Posts:
    1

    Hi there, is changing output format still not possible? Is there a way around it? I updated to version 3.0.3 just in case.

    Here, i am trying to change it to MOV just to capture alpha. File name changes accordingly, yet I cant seem to change format. Thanks in advance!

    Code (CSharp):
    1.  
    2. videoRecorder.OutputFile = root + path;
    3. if (someCondition)
    4. {
    5.     videoRecorder.OutputFormat = MovieRecorderSettings.VideoRecorderOutputFormat.MP4;
    6.     videoRecorder.ImageInputSettings = new GameViewInputSettings()
    7.     {
    8.         OutputHeight = arrToUseY[j],
    9.         OutputWidth = arrToUseX[j]
    10.     };
    11. }
    12. else
    13. {
    14.     videoRecorder.OutputFormat = MovieRecorderSettings.VideoRecorderOutputFormat.MOV;
    15.  
    16.     videoRecorder.ImageInputSettings = new CameraInputSettings()
    17.     {
    18.         Source = ImageSource.MainCamera,
    19.         OutputHeight = arrToUseY[j],
    20.         OutputWidth = arrToUseX[j],
    21.         FlipFinalOutput = true,
    22.         RecordTransparency = true
    23.     };
    24. }
    25.  
     
  13. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    It's not yet possible, but we are working on it!
     
  14. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Is it possible to control the recording frame by frame? I was hoping I could feed the recorder with a renderTexture ( populated elsewhere ) and then issue a recorder.recordFrame type of command for it to append the contents of the renderTexture to the current video stream.

    I can't use real-time recording as the data comes in from hardware and is not guaranteed to be a specific framerate.

    The final part of this project will be to use the same hardware data to drive various VFX Graphs and although I've done some testing with constant framerate and fpsCap, it would be much easier to specify when to record a frame, as frame timings are likely to be inconsistent and trying to sync up getting the hardware data reliably is a challenge. The issue is if recording the frame takes a long time it might end up missing input from the hardware. So basically have full control over when the recorder captures frame would be ideal..
     
  15. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    The Recorder package is not built for this.
    You could build something like this to export PNG images when you want, without using the Recorder package.
     
  16. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Thank you for your reply.

    Seems like an odd arbitrary restriction and if developers end up having to build there own versions of an already created system to support subtle differences like this it kind of defeats the purpose of having a built in editor recorder in the first place.

    In the end I used Natcorder from the Asset Store as it has this exact functionality built in for MP4 recordings, though I may not be able to use for the final render as the requirement may be to export via ProRes4444 for alpha support.

    Is there any chance such a feature could be added in the future?
     
  17. Jrnze

    Jrnze

    Joined:
    Nov 16, 2021
    Posts:
    12
    Hi, I want to control the Unity Recorder via script but have no idea how. I want to be able to press a button (like "R" to start recording and "S" to stop recording) during runtime. Is it possible? Thanks! :)
     
  18. morybest

    morybest

    Joined:
    Nov 7, 2013
    Posts:
    6
    Hi , How I can create a controller for record gif animation clips with unity recorder ? any idea ?
     
  19. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Please have a look at the sample called MovieRecorderExample.
    To see the whole list of available samples, select the Recorder package in the Package Manager, then expand the Samples list.

    The example starts recording upon entering play mode. You'll have to modify this to listen to your keyboard, as per this document.
     
  20. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Please have a look at the sample called MovieRecorderExample.
    To see the whole list of available samples, select the Recorder package in the Package Manager, then expand the Samples list.

    You're going to have to change the code from
    m_Settings.EncoderSettings = new CoreEncoderSettings
    {
    EncodingQuality = CoreEncoderSettings.VideoEncodingQuality.High,
    Codec = CoreEncoderSettings.OutputCodec.MP4
    };

    to
    m_Settings.EncoderSettings = new GifEncoderSettings()
    {
    Loop = false,
    Quality = 100
    };
     
  21. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    Would you mind using this link to submit your request? https://unity.com/roadmap/virtual-production.

    upload_2022-6-22_15-37-10.png
     
  22. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    unitybru likes this.
  23. Mapiwe

    Mapiwe

    Joined:
    Aug 8, 2022
    Posts:
    1
    Hey Folks,

    I have to build a VR Room in Unity for my bachelor thesis. The room should have a menu with buttons or a button (gameobject) in the scene which can record audio & video if you pressed the button. The capturing should ended and saved if you pressed the Button again (or if you pressed another button ingame). Can you guys help me with this problem? I really don’t know how the script for this should look like…I would be very happy and thankful if you can help me.
     
  24. unitybru

    unitybru

    Unity Technologies

    Joined:
    Jan 28, 2020
    Posts:
    225
    For those who want sample code to record audio, here is a behaviour that records when it enters Play mode and stops when it exits Play mode. This requires Recorder 4.x because before that the AudioRecorderSettings class was internal.

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using UnityEditor.Recorder;
    4. using UnityEngine;
    5.  
    6. /// <summary>
    7. /// Record an audio file manually in Recorder 4.x
    8. /// </summary>
    9. public class ScriptedAudioRecorder : MonoBehaviour
    10. {
    11.     RecorderController TestRecorderController;
    12.    
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         var controllerSettings = ScriptableObject.CreateInstance<RecorderControllerSettings>();
    17.         TestRecorderController = new RecorderController(controllerSettings);
    18.         var videoRecorder = ScriptableObject.CreateInstance<AudioRecorderSettings>();
    19.         videoRecorder.name = "My Audio Recorder";
    20.         videoRecorder.Enabled = true;
    21.         videoRecorder.OutputFile = "myrecording"; // no extension!
    22.         var fiOut = new FileInfo(videoRecorder.OutputFile + ".wav");
    23.         controllerSettings.AddRecorderSettings(videoRecorder);
    24.         controllerSettings.SetRecordModeToManual(); // will stop when closing
    25.         controllerSettings.FrameRate = 30;
    26.         RecorderOptions.VerboseMode = false;
    27.         TestRecorderController.PrepareRecording();
    28.         TestRecorderController.StartRecording();
    29.        
    30.         Debug.Log($"Started recording to file '{fiOut.FullName}'");
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.        
    37.     }
    38.  
    39.     void OnDisable()
    40.     {
    41.         TestRecorderController.StopRecording();
    42.         Debug.Log($"Stopped recording");
    43.     }
    44. }
    45.  
     
    GeniusKoala and Noisecrime like this.
  25. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Can someone confirm if recorder is editor only?
    Or can we script / use it also in executable?
     
  26. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    Unfortunately, Editor only.
     
  27. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    To generate movies at runtime, I save a bunch of screenshots and run ffmpeg to generate the movie afterwards, is there any nice alternative (besides screengrabbers)?
     
  28. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    I've used Natcorder from the Asset Store in the past and been very happy with it. However i've just seen its been deprecated on the asset store a few months ago. I can't find anything from the developer about this, but did find that it appears to be on github, but using it now requires a subscription to the authors cloud based product at an absurd $29pm. Granted if you want the other machine learning products they offer it might be worth it, but otherwise this seems like a nasty cash grab.

    Honestly not sure if there is any other good recommendations from the asset store. Nothing stands out from a quick look at the highest rated.

    Depending on your target platform, you might find you could use FFMPEG directly and send frame image data to it, though it would likely be a large amount of effort to get working.
     
    Cascho01 likes this.
  29. Jrnze

    Jrnze

    Joined:
    Nov 16, 2021
    Posts:
    12
    Can this work outside of Unity Editor?
     
  30. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Right, if anyone has managed to get this working I would be highly interested in the code! ;)
     
  31. Dust999Games

    Dust999Games

    Joined:
    Jan 10, 2014
    Posts:
    7
    Hi there. I successfully create a script that takes screenshots from the game view with Recorder. I'm faced with the problem of how to restore Game View screen dimensions?

    UPD: Oh it succefetly restored after exit play mode! :)
     
    Last edited: Sep 13, 2022
  32. KapaRZA

    KapaRZA

    Joined:
    May 30, 2016
    Posts:
    2
    Is it possible to run Unity Recorder in batchmode via command line?
     
  33. aRuuu

    aRuuu

    Joined:
    Nov 21, 2018
    Posts:
    1
    Hi, i was able to use the Video Recorder by script, but when i try to build and export my scene, i have a error corresponding to the class that i use for referer the video recorder, how did i export including this class ?
    Thanks
     

    Attached Files:

  34. GeniusKoala

    GeniusKoala

    Joined:
    Oct 13, 2017
    Posts:
    97
    If I'm not wrong you can't build with some package that are Editor only. You need to import the package withtin a :

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3. import package;
    4.  
    5. #endif
    6.  
    7. #if UNITY_EDITOR
    8.  
    9. code using this package...
    10.  
    11. #endif
     
  35. Haze-Games

    Haze-Games

    Joined:
    Mar 1, 2015
    Posts:
    189
    Hi,

    I've managed to successfully record the Game View during Play Mode Tests thanks to your code @unitybru - thank you!

    It works fine in the Editor (Unity 2021.3.11f1 with Recorder 3.0.3).

    Unfortunately, recording in Batch Mode in Jenkins (with graphics enabled) fails when we request to Stop Recording, with this error:

    Code (CSharp):
    1. ArgumentException: Object of type 'System.Int32[]' cannot be converted to type 'UnityEditor.GameViewSize'.
    2. 12:49:44   at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00071] in <e40e5a8f982c4b618a930d29f9bd091c>:0
    3. 12:49:44   at System.Reflection.RuntimeMethodInfo.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x00069] in <e40e5a8f982c4b618a930d29f9bd091c>:0
    4. 12:49:44   at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00040] in <e40e5a8f982c4b618a930d29f9bd091c>:0
    5. 12:49:44   at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <e40e5a8f982c4b618a930d29f9bd091c>:0
    6. 12:49:44   at UnityEditor.Recorder.Input.GameViewSize.IndexOf (System.Object sizeObj) [0x00019] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorders\_Inputs\GameViewSize.cs:118
    7. 12:49:44   at UnityEditor.Recorder.Input.GameViewSize.SelectSize (System.Object size) [0x00004] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorders\_Inputs\GameViewSize.cs:157
    8. 12:49:44   at UnityEditor.Recorder.Input.GameViewSize.RestoreSize () [0x00000] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorders\_Inputs\GameViewSize.cs:185
    9. 12:49:44   at UnityEditor.Recorder.Input.GameViewInput.Dispose (System.Boolean disposing) [0x00026] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorders\_Inputs\GameView\GameViewInput.cs:141
    10. 12:49:44   at UnityEditor.Recorder.RecorderInput.Dispose () [0x00000] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\RecorderInput.cs:31
    11. 12:49:44   at UnityEditor.Recorder.Recorder.EndRecording (UnityEditor.Recorder.RecordingSession session) [0x00064] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorder.cs:205
    12. 12:49:44   at UnityEditor.Recorder.BaseTextureRecorder`1[T].EndRecording (UnityEditor.Recorder.RecordingSession session) [0x00000] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\BaseTextureRecorder.cs:159
    13. 12:49:44   at UnityEditor.Recorder.MovieRecorder.EndRecording (UnityEditor.Recorder.RecordingSession session) [0x00000] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\Recorders\MovieRecorder\MovieRecorder.cs:189
    14. 12:49:44   at UnityEditor.Recorder.RecordingSession.EndRecording () [0x0000d] in D:\Jenkins\workspace\MyProject_FTESTs\Library\PackageCache\com.unity.recorder@3.0.3\Editor\Sources\RecordingSession.cs:146
    Would you by any chance have an idea of how this could be solved? It would be magnificent to record Play Mode Tests on build machines in batch mode with graphics enabled and retrieve the associated recordings.

    Thank you!
     
    Last edited: Nov 2, 2022
  36. GeniusKoala

    GeniusKoala

    Joined:
    Oct 13, 2017
    Posts:
    97

    I have no answer for you problem but do you any source on how to use Unity in batch mode? I am very interested in offline recording for animation purposes. Thank you!
     
  37. Haze-Games

    Haze-Games

    Joined:
    Mar 1, 2015
    Posts:
    189
    OK!

    Yes you can see more about command line arguments here: https://docs.unity3d.com/Manual/EditorCommandLineArguments.html

    You need to launch Unity with that
    -batchmode
    command line argument (either a shortcut to Unity editor .exe with that argument, or via command line, or in Jenkins).

    I also tried with recording Camera instead of Game view, similar error. It seems like Unity Recorder does not work in Unity while in batchmode :(
     
  38. Matsu0010

    Matsu0010

    Joined:
    Dec 22, 2018
    Posts:
    1
    Hello.
    I would like to record and save a series of videos using UnityRecoder as a script control.
    However, there is a problem when StartRecording is performed immediately after StopRecording.
    Is there any way to have StopRecording respond that it has finished successfully?
     
  39. Ubrano

    Ubrano

    Joined:
    Jul 23, 2017
    Posts:
    16
    It seems one should be able to get the status of the recorder (re: https://docs.unity.cn/Packages/com.unity.recorder@2.1/api/UnityEditor.Recorder.html);

    Enabled
    Indicates if this Recorder is active when starting the recording. If false, the Recorder is ignored and generates no output.
    Declaration
    public bool Enabled { get; set; }

    Methods
    IsRecording()
    Use this method to know if all recorders are done recording. A recording stops:
    1. The settings is set to a time (or frame) interval and the end time (or last frame) was reached.
    2. Calling the StopRecording method.
    3. Exiting Playmode.
    Declaration
    public bool IsRecording()

    What kind of a problem?
    What exactly do you mean by finished successfully?

    The Recorder is a great tool, but – depending on what one is trying to achieve – has limitations. One should consider stuff like physics and fx may act out of sync when a game is recorded. And sound effect syncing might prove a nightmare, too. It was a couple of years ago, but recording an 8K 120fps video of a game, I had to implement various workarounds to get the proper effects in the final product. Something that was otherwise available in the game as intended without any problems.

    If said trouble is indeed as simple as it looks, perhaps the easiest way would be to not stop recording at all, just carry on and split the recording in postproduction. On a similar note, one should consider multiple recorders might run at the same time, therefore having a second recorder start independently of the first should not be an issue. Or – to stay on the safe side – just use an array of recorders, so each part is recorded by an individual recorder.
     
  40. ignarmezh

    ignarmezh

    Joined:
    Mar 23, 2017
    Posts:
    56
    Is it possible to record a video with delay between frames?
    I want to control which shots I want to receive.
    For example I have some logic between target frames and I know a place in code where I can call a function like controller.CaptureOneFrame().

    Example:
    Code (CSharp):
    1. private float _delayInSec = 1f;
    2. private float _currentTime = 0f;
    3.  
    4. private void Update()
    5. {
    6.    _currentTime += Time.deltaTime;
    7.    if (_currentTime >= _delayInSec)
    8.    {
    9.         controller.CaptureOneFrame();
    10.         _currentTime = 0f;
    11.    }
    12. }
    And after all captured frames end recording.
     
  41. fwalker

    fwalker

    Joined:
    Feb 5, 2013
    Posts:
    255
    I got the recorder working with script. I use it to generate several AOV Outputs. For example Normals and Depth. Does anyone know of a way to synchronize these outputs so the frame outputs match? I tried StartFrame = Time.frameCount+ (framdelay to allow for sync) but that did not do what I expected (I don't even know what it does). And all the doc I can find is that StartFrame is the "The start frame of the recording"
    Any clever ideas?
     
  42. khanfawaz1110

    khanfawaz1110

    Joined:
    Sep 21, 2023
    Posts:
    1
    hello i am making a AR app in unity and i wanted to implement the functionality of screen recording does anyone know how to do that i have researched on the internet about it but there's no solution in in game screen recording. i bought a plugin for screen recording but as it records the whole screen there's always a popup due to the android security issue which is quite tiring. if you have any solution or any way please let me know
     

    Attached Files: