zoukankan      html  css  js  c++  java
  • unity 录制游戏内视频(1)

    1,调用的api可以制作任何类型的视频类型(包含快照,and VR视频)

      1 using System.Collections;
      2 using System.Collections.Generic;
      3 using System.IO;
      4 using UnityEngine;
      5 
      6 public class ShotVideo : MonoBehaviour
      7 {
      8     public float ScreenShotHeight = 500;
      9     public float ScreenShotWith = 500;
     10     public string PicSavePath = "MyPhoto";
     11 
     12 
     13     [SerializeField]
     14     private Texture2D picture;
     15     private RenderTexture renderBuffer;
     16     private string Path = "";
     17     private bool IsShot = false;
     18     private static int count = 0;
     19 
     20 
     21     public string RecordPath = "";
     22     private bool IsRecord = false;
     23     private static int RecordCount= 0;
     24 
     25     // Use this for initialization
     26     void Start()
     27     {
     28         picture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGBA32, false, true);
     29         Path = Application.dataPath + "/" + PicSavePath;
     30         //Debug.LogError(Path);
     31         SetPath(Path);
     32         SetPath(RecordPath);
     33 
     34     }
     35 
     36     public void SetPath(string _s)
     37     {
     38         //path
     39         if (Directory.Exists(_s))
     40         {
     41             FileAttributes attr = File.GetAttributes(_s);
     42             if (attr == FileAttributes.Directory)
     43                 Directory.Delete(_s, true);
     44             else
     45                 File.Delete(_s);
     46         }
     47         if (!Directory.Exists(_s))
     48             Directory.CreateDirectory(_s);
     49     }
     50 
     51     // Update is called once per frame
     52     void Update()
     53     {
     54         if (Input.GetKeyDown(KeyCode.Space) && !IsShot)
     55             ShotScreen();
     56         if (Input.GetKeyDown(KeyCode.R))
     57             Record();
     58         if (Input.GetKeyDown(KeyCode.S))
     59             StopRecord();
     60 
     61     }
     62 
     63     public bool StartRecord()
     64     {
     65         IsRecord = true;
     66         return IsRecord;
     67     }
     68     public bool StopRecord()
     69     {
     70         IsRecord = false;
     71         return IsRecord;
     72     }
     73 
     74     public void Record()
     75     {
     76         StartRecord();
     77         StartCoroutine(CaptureVideo());
     78     }
     79 
     80     public void ShotScreen()
     81     {
     82         IsShot = true;
     83         StartCoroutine(CaptureScreen());
     84     }
     85     
     86 
     87     public IEnumerator CaptureScreen()
     88     {
     89         yield return new WaitForEndOfFrame();
     90         picture.ReadPixels(
     91             new Rect(0, 0, Screen.width, Screen.height), 0, 0
     92             );
     93 
     94         picture.Apply();
     95         var bytes = picture.EncodeToPNG();
     96         File.WriteAllBytes(Path + "/" + count.ToString() + ".png", bytes);
     97         count++;
     98         Debug.Log("Fin the work gender texture");
     99 
    100         yield return new WaitForSeconds(0.1f);
    101         IsShot = false;
    102     }
    103 
    104     public IEnumerator CaptureVideo()
    105     {
    106         while (IsRecord)
    107         {
    108             picture.ReadPixels(
    109           new Rect(0, 0, Screen.width, Screen.height), 0, 0
    110           );
    111 
    112             picture.Apply();
    113             var bytes = picture.EncodeToPNG();
    114             File.WriteAllBytes(Path + "/" + RecordCount.ToString() + ".png", bytes);
    115             RecordCount++;
    116             Debug.Log("Fin the work CaptureVideo texture "+ RecordCount);
    117             yield return new WaitForEndOfFrame();
    118         }
    119       
    120     }
    121 
    122 }

    一个视频的组成可以看成 每秒24 帧的连续图像组成。

    后期将调用ffmpeg的库将图片拼接成视频

  • 相关阅读:
    Go_海量用户即时通讯系统
    Golang redis学习指南
    Golang 操作_Redis
    十七、Redis
    十六、网络编程-tcp socket编程
    十五、反射
    十四、goroutine(协程)和channel(管道)
    Jmeter笔记(9)Jmeter 性能测试资源监控方法(本地与服务器)(转)
    Fiddler笔记(8)重装时清除已有证书及解决tunnel to 443问题
    Jmeter笔记(8)Jmeter与MySql连接
  • 原文地址:https://www.cnblogs.com/yangxiaohang/p/9148931.html
Copyright © 2011-2022 走看看