Created
May 30, 2016 04:30
-
-
Save tuti107/4b9ea7a85dc70d8b1d0eff515a7168c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Diagnostics; | |
using System.IO; | |
using UnityEngine.UI; | |
public class FrameEncoder : MonoBehaviour { | |
public string videoDir; | |
public int FPS = 30; | |
public int width = 1280; | |
public int height = 1440; | |
public Camera dispCamera; | |
private Texture2D targetTexture; | |
private bool isRecording; | |
private Process ffmpegProcess; | |
private BinaryWriter ffmpegInput; | |
private float frameInterval; | |
private float prevTime; | |
public void ToggleRecording(Text text) | |
{ | |
var message = "Start"; | |
if (!isRecording) { | |
StartRecording(); | |
message = "Stop"; | |
} | |
else | |
{ | |
StopRecordnig(); | |
} | |
if (text != null) | |
{ | |
text.text = message; | |
} | |
} | |
public void StartRecording() | |
{ | |
if (!isRecording) | |
{ | |
isRecording = true; | |
ffmpegProcess = new Process(); | |
string filePath = videoDir + "\\" + System.DateTime.Now.ToString("yyyyMMdd-hhmmss") +".mp4"; | |
ffmpegProcess.StartInfo.FileName = "ffmpeg"; | |
ffmpegProcess.StartInfo.Arguments = string.Format("-framerate {1} -i - -r 20 -s {2}x{3} -an -vcodec nvenc -pix_fmt yuv420p -b:v 100M {0}", | |
filePath, FPS, width, height); | |
UnityEngine.Debug.Log(ffmpegProcess.StartInfo.Arguments); | |
ffmpegProcess.StartInfo.UseShellExecute = false; | |
ffmpegProcess.StartInfo.RedirectStandardInput = true; | |
ffmpegProcess.Start(); | |
ffmpegInput = new BinaryWriter(ffmpegProcess.StandardInput.BaseStream); | |
var tex = dispCamera.targetTexture; | |
targetTexture = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false); | |
} | |
} | |
public void StopRecordnig() { | |
if (isRecording) | |
{ | |
ffmpegInput.Close(); | |
ffmpegInput = null; | |
ffmpegProcess.Close(); | |
ffmpegProcess = null; | |
isRecording = false; | |
} | |
} | |
// Use this for initialization | |
void Start () { | |
isRecording = false; | |
frameInterval = 1.0f / FPS; | |
prevTime = 0; | |
} | |
// Update is called once per frame | |
void Update () { | |
if (isRecording) | |
{ | |
var T = Time.time; | |
var t = T - prevTime; | |
if (t >= frameInterval) | |
{ | |
if (!ffmpegProcess.HasExited) | |
{ | |
var tex = dispCamera.targetTexture; | |
RenderTexture.active = dispCamera.targetTexture; | |
targetTexture.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); | |
targetTexture.Apply(); | |
byte[] imgData = targetTexture.EncodeToJPG(); | |
ffmpegProcess.Refresh(); | |
ffmpegInput.Write(imgData); | |
ffmpegInput.Flush(); | |
} | |
prevTime = T; | |
} | |
} | |
else if (Input.GetKeyUp(KeyCode.R)) | |
{ | |
StartRecording(); | |
} | |
if (Input.GetKeyUp(KeyCode.E)) | |
{ | |
StopRecordnig(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment