Skip to content

Commit

Permalink
Merge pull request #98 from ReiFan49/fix/record-patch
Browse files Browse the repository at this point in the history
Supplement Screen Recorder Stoppers
  • Loading branch information
LingFeng-bbben authored Mar 29, 2023
2 parents 0b05692 + 5a78914 commit f10a0ff
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 29 deletions.
25 changes: 23 additions & 2 deletions Assets/Scripts/HttpHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Threading;
Expand Down Expand Up @@ -102,12 +103,15 @@ private void Update()
request = "";
var maidataPath = new FileInfo(data.jsonPath).DirectoryName;
timeProvider.SetStartTime(data.startAt, data.startTime, data.audioSpeed,true);
screenRecorder.StartRecording(maidataPath);
loader.noteSpeed = (float)(107.25 / (71.4184491 * Mathf.Pow(data.noteSpeed + 0.9975f, -0.985558604f)));
loader.touchSpeed = data.touchSpeed;
objectCounter.ComboSetActive(data.comboStatusType);
loader.LoadJson(File.ReadAllText(data.jsonPath), data.startTime);
multTouchHandler.clearSlots();

screenRecorder.CutoffTime = getChartLength();
screenRecorder.CutoffTime += 10f;
screenRecorder.StartRecording(maidataPath);

bgManager.LoadBGFromPath(maidataPath, data.audioSpeed);
bgCover.color = new Color(0f, 0f, 0f, data.backgroundCover);
Expand All @@ -132,4 +136,21 @@ private void Update()
}
request = "";
}

private float getChartLength()
{
float length = 0f;
foreach (NoteDrop noteData in GameObject.Find("Notes").GetComponentsInChildren<NoteDrop>())
{
length = Math.Max(length, noteData.time);

NoteLongDrop longData = noteData as NoteLongDrop;
if (longData != null)
{
length = Math.Max(length, noteData.time + longData.LastFor);
continue;
}
}
return length;
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/JsonDataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void LoadJson(string json, float ignoreOffset)

if (timing.noteList.Count > 1) NDCompo.isEach = true;
NDCompo.time = (float)timing.time;
NDCompo.lastFor = (float)note.holdTime;
NDCompo.LastFor = (float)note.holdTime;
NDCompo.startPosition = note.startPosition;
NDCompo.speed = noteSpeed * timing.HSpeed;
NDCompo.isEX = note.isEx;
Expand All @@ -127,7 +127,7 @@ public void LoadJson(string json, float ignoreOffset)
var GOnote = Instantiate(touchHoldPrefab, notes.transform);
var NDCompo = GOnote.GetComponent<TouchHoldDrop>();
NDCompo.time = (float)timing.time;
NDCompo.lastFor = (float)note.holdTime;
NDCompo.LastFor = (float)note.holdTime;
NDCompo.speed = touchSpeed * timing.HSpeed;
NDCompo.isFirework = note.isHanabi;

Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/Notes/HoldDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using UnityEngine;

public class HoldDrop : MonoBehaviour
public class HoldDrop : NoteLongDrop
{
public float time;
public float lastFor = 1f;
// public float time;
// public float LastFor = 1f;
public int startPosition = 1;
public float speed = 1;

Expand Down Expand Up @@ -126,7 +126,7 @@ void Update()

spriteRenderer.size = new Vector2(1.22f, 1.4f);

var holdTime = timing - lastFor;
var holdTime = timing - LastFor;
var holdDistance = holdTime * speed + 4.8f;
if (holdTime > 0) {
GameObject.Find("NoteEffects").GetComponent<NoteEffectManager>().PlayEffect(startPosition, isBreak);
Expand Down
10 changes: 10 additions & 0 deletions Assets/Scripts/Notes/NoteDrop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;
public class NoteDrop : MonoBehaviour
{
public float time;
}

public class NoteLongDrop : NoteDrop
{
public float LastFor = 1f;
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Notes/NoteDrop.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Scripts/Notes/SlideDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class SlideDrop : MonoBehaviour
public class SlideDrop : NoteLongDrop
{
// Start is called before the first frame update
public GameObject star_slide;
Expand All @@ -21,9 +21,9 @@ public class SlideDrop : MonoBehaviour
public bool isBreak;
public bool isGroupPart;
public bool isGroupPartEnd;
public float time;
// public float time;
public float timeStar;
public float LastFor = 1f;
// public float LastFor = 1f;
public float speed;

public int startPosition =1;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Notes/StarDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using UnityEngine;

public class StarDrop : MonoBehaviour
public class StarDrop : NoteDrop
{
// Start is called before the first frame update
public float time;
// public float time;
public int startPosition = 1;
public float speed = 1;
public float rotateSpeed = 1f;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Notes/TapDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using UnityEngine;

public class TapDrop : MonoBehaviour
public class TapDrop : NoteDrop
{
// Start is called before the first frame update
public float time;
// public float time;
public int startPosition = 1;
public float speed = 1;

Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Notes/TouchDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using UnityEngine;

public class TouchDrop : MonoBehaviour
public class TouchDrop : NoteDrop
{
public float time;
// public float time;
public float speed = 7;
public char areaPosition;
public bool isEach;
Expand Down
10 changes: 5 additions & 5 deletions Assets/Scripts/Notes/TouchHoldDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using UnityEngine;

public class TouchHoldDrop : MonoBehaviour
public class TouchHoldDrop : NoteLongDrop
{
public float time;
public float lastFor = 1f;
// public float time;
// public float LastFor = 1f;
public float speed = 1;
public bool isFirework;

Expand Down Expand Up @@ -68,7 +68,7 @@ void Update()
var pow = -Mathf.Exp(8 * (timing * 0.4f / moveDuration) - 0.85f) + 0.42f;
var distance = Mathf.Clamp(pow, 0f, 0.4f);

if (timing > lastFor)
if (timing > LastFor)
{
Instantiate(tapEffect, transform.position, transform.rotation);
GameObject.Find("ObjectCounter").GetComponent<ObjectCounter>().holdCount++;
Expand All @@ -92,7 +92,7 @@ void Update()
fans[5].SetActive(true);
mask.enabled = true;
SetfanColor(Color.white);
mask.alphaCutoff = Mathf.Clamp(0.91f * (1 - ((lastFor - timing) / lastFor)), 0f, 1f);
mask.alphaCutoff = Mathf.Clamp(0.91f * (1 - ((LastFor - timing) / LastFor)), 0f, 1f);
}

if (float.IsNaN(distance)) distance = 0f;
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Notes/WifiDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;

public class WifiDrop : MonoBehaviour
public class WifiDrop : NoteLongDrop
{
// Start is called before the first frame update
public GameObject star_slidePrefab;
Expand All @@ -20,9 +20,9 @@ public class WifiDrop : MonoBehaviour
public RuntimeAnimatorController slideShine;

public bool isJustR;
public float time;
// public float time;
public float timeStart;
public float LastFor = 1f;
// public float LastFor = 1f;
public float speed;
public bool isEach;
public bool isBreak;
Expand Down
25 changes: 22 additions & 3 deletions Assets/Scripts/ScreenRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class ScreenRecorder : MonoBehaviour
{
public float CutoffTime;

public void StartRecording(string maidata_path)
{
StartCoroutine(CaptureScreen(maidata_path));
Expand All @@ -27,12 +29,16 @@ public void StopRecording()
bool isRecording = false;
IEnumerator CaptureScreen(string maidata_path)
{
var timeProvider = GameObject.Find("AudioTimeProvider").GetComponent<AudioTimeProvider>();
var bgManager = GameObject.Find("Background").GetComponent<BGManager>();
if (Screen.width % 2 != 0 || Screen.height % 2 != 0)
{
GameObject.Find("ErrText").GetComponent<Text>().text = "无法开始编码,因为分辨率宽度或高度不是偶数。\nCan not start render because the width/height is not even.\n当前分辨率:"+Screen.width+"x"+Screen.height+"\n";
yield break;
}
if (File.Exists(maidata_path + "\\out.mp4"))
File.Delete(maidata_path + "\\out.mp4");

byte[] data;
var texture = new Texture2D(0,0);
using (NamedPipeServerStream pipeServer =
Expand All @@ -41,10 +47,17 @@ IEnumerator CaptureScreen(string maidata_path)
var wavpath = "out.wav";
var outputfile = "out.mp4";

var arguments = string.Format(File.ReadAllText(Application.streamingAssetsPath+ "\\ffarguments.txt"),
Screen.width, Screen.height, wavpath, outputfile );
var arguments = string.Format(
File.ReadAllText(Application.streamingAssetsPath+ "\\ffarguments.txt").Trim(),
Screen.width, Screen.height,
wavpath, outputfile,
CutoffTime
);
var startinfo = new ProcessStartInfo(Application.streamingAssetsPath + "\\ffmpeg.exe", arguments);
startinfo.UseShellExecute = false;
startinfo.CreateNoWindow = true;
startinfo.WorkingDirectory = maidata_path;
startinfo.EnvironmentVariables.Add("FFREPORT", "file=out.log:level=24");
print(arguments);

var p = Process.Start(startinfo);
Expand All @@ -70,7 +83,11 @@ IEnumerator CaptureScreen(string maidata_path)
}
catch { }
}
while (pipeServer.IsConnected && isRecording && !p.HasExited);
while (
pipeServer.IsConnected &&
isRecording &&
!p.HasExited
);
}
p.WaitForExit();

Expand All @@ -82,6 +99,8 @@ IEnumerator CaptureScreen(string maidata_path)
else
GameObject.Find("ErrText").GetComponent<Text>().text += "编码器已退出\nFFmpeg Exited.\nExitCode:" + p.ExitCode;
}
timeProvider.isStart = false;
bgManager.PauseVideo();
}

// Start is called before the first frame update
Expand Down
4 changes: 4 additions & 0 deletions Assets/StreamingAssets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### ffarguments guide

prepend `-report` flag to write the log out to `out.log`.
further configuration of ffmpeg related arguments can refer to it's manual.
7 changes: 7 additions & 0 deletions Assets/StreamingAssets/README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/StreamingAssets/ffarguments.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-y -f rawvideo -vcodec rawvideo -pix_fmt rgba -s {0}x{1} -r 60 -i \\.\pipe\majdataRec -i {2} -vf "vflip" -c:v libx264 -preset fast -pix_fmt yuv420p -b:a 320k -c:a aac {3}
-hide_banner -y -f rawvideo -vcodec rawvideo -pix_fmt rgba -s "{0}x{1}" -r 60 -i \\.\pipe\majdataRec -i "{2}" -vf "vflip" -c:v libx264 -preset fast -pix_fmt yuv420p -t "{4:0.0000}" -b:a 320k -c:a aac "{3}"

0 comments on commit f10a0ff

Please sign in to comment.