Search Unity

Recording script freezes game on startup

Discussion in 'Scripting' started by Miclebrick, Aug 25, 2017.

  1. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38
    I have an old recording script I made a long time ago, and I wanted to try it with the new Unity Timeline and stuff. I tried it, but the Editor freezes when I press Play. Is there anything I'm doing wrong?

    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using UnityEngine;
    public class Recorder : MonoBehaviour
    {
    private bool _done;
    private int _frames = 1;
    public int Fps = 24;
    public int Frames = 100;
    public string Name = "Recording";
    public bool Enabled = false;
    // Use this for initialization
    public void Start()
    {
    Time.captureFramerate = Fps;
    Directory.CreateDirectory("Output/" + Name + "/");
    Directory.CreateDirectory("Output/" + Name + "/Video");
    }
    public void Update()
    {
    if (_done || !Enabled) return;
    var output = "Output/" + Name + "/Frame" + _frames + ".png";
    ScreenCapture.CaptureScreenshot(output);
    _frames++;
    if (_frames <= Frames) return;
    Debug.Log("Done recording.");
    _done = true;
    var oldDirectory = Directory.GetCurrentDirectory();
    Directory.SetCurrentDirectory("Output/" + Name);
    Debug.Log(system("ffmpeg -f Image2 -i Frame%d.png -vb 20M -start_number 1 Video/Output.avi"));
    Directory.SetCurrentDirectory(oldDirectory);
    }
    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
    private static extern int system(string command);
    }
    EDIT: Oh and no images actually save
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Which line of code causes the problem?
     
  3. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38
    Not entirely sure but removing the ScreenCapture.CaptureScreenshot() line makes it not freeze, although that doesn't necessarily mean it's because of that - maybe it's being used incorrectly/too much? But I guess that's it.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    And what is the value of "output" at that point?
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    And of course you are trying to write out the file every frame, like 24 times a second. Does it work correctly when it saves a single time, like in a button click on during Start ?
     
  6. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38
    I'll check the value of output, and see what happens if I only record one frame. I'd also like to point out that this script worked when I last used it, in Unity 5.
     
  7. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38

    Those are the frame names
     
  8. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38
    If I add this before the save screenshot thing:
    if (_frames > 2)
    then it doesn't freeze
     
  9. Miclebrick

    Miclebrick

    Joined:
    Dec 31, 2014
    Posts:
    38
    It works now, but it's very slow, is there a better way to record it?