一、導(dǎo)入AVProMovieCapture包
在Asset Store資源商店里面可以下載這個插件:
https://assetstore./packages/tools/video/avpro-movie-capture-desktop-edition-221914
目前這個插件比較貴,以下提供了老一點的版本,也是可以用的。 https://download.csdn.net/download/caiprogram123/89517225
下載之后解壓,并把這個包導(dǎo)入到Unity中,可以看到RenderHeads/AVProMovieCapture
二、屏幕捕捉基本設(shè)置
在開始錄制Game視圖時要對錄制做一些基本的設(shè)置:
-
輸出文件夾類型:輸出視頻到哪個位置 -
輸出子文件夾:輸出視頻位置的子文件夾 -
分辨率:設(shè)置視頻的分辨率 -
幀率:設(shè)置視頻的幀率 -
視頻前綴:設(shè)置視頻名稱的前綴
//視頻輸出路徑
public string videoOutFolder;
//視頻輸出路徑類型
public OutputPath outputFolderType = defaultOutputFolderType;
//視頻輸出文件夾
public string outputFolderPath = defaultOutputFolderPath;
//視頻降分辨率
public CaptureBase.DownScale downScale = CaptureBase.DownScale.Original;
//視頻幀率
public float frame = 60;
//視頻前綴
public string prefix = "ScreenCapture";
/// <summary>
/// 設(shè)置視頻輸出路徑
/// </summary>
private void SetOutFolderPath()
{
videoOutFolder = GetFolder(outputFolderType, outputFolderPath);
if (!Directory.Exists(videoOutFolder))
Directory.CreateDirectory(videoOutFolder);
_movieCapture.OutputFolderPath = videoOutFolder;
}
/// <summary>
/// 設(shè)置視頻畫質(zhì)
/// </summary>
private void SetDownScale()
{
_movieCapture.ResolutionDownScale = downScale;
}
/// <summary>
/// 設(shè)置幀率
/// </summary>
private void SetFrameRate()
{
_movieCapture.FrameRate = frame;
}
/// <summary>
/// 設(shè)置文件前綴
/// </summary>
private void SetPrefix()
{
_movieCapture.FilenamePrefix = prefix;
}
三、實現(xiàn)Game視圖屏幕捕捉功能
1、屏幕捕捉功能
在導(dǎo)入包后就可以進(jìn)行屏幕錄制功能開發(fā),包含開始錄頻、結(jié)束錄頻、查看最近錄制,同時在錄制的過程中也能夠看到當(dāng)前當(dāng)前錄制視頻的大小和時長。
private long _lastFileSize;
private uint _lastEncodedMinutes;
private uint _lastEncodedSeconds;
private uint _lastEncodedFrame;
private StringBuilder _fileSize = new StringBuilder();
private StringBuilder _videoLength = new StringBuilder();
/// <summary>
/// 開始錄制
/// </summary>
private void StartCapture()
{
_lastFileSize = 0;
_lastEncodedMinutes = _lastEncodedSeconds = _lastEncodedFrame = 0;
if (_movieCapture != null)
{
_movieCapture.StartCapture();
startRecord.interactable = false;
stopRecord.interactable = true;
}
}
/// <summary>
/// 停止錄制
/// </summary>
private void StopCapture()
{
if (_movieCapture != null)
{
if (_movieCapture.IsCapturing())
{
_movieCapture.StopCapture();
startRecord.interactable = true;
stopRecord.interactable = false;
}
}
}
/// <summary>
/// 文件大小設(shè)置
/// </summary>
/// <param name="lastFileSize"></param>
private void FileSize()
{
_lastFileSize = _movieCapture.GetCaptureFileSize();
_fileSize.Clear();
_fileSize.Append((_lastFileSize / (1024f * 1024f)).ToString("F1"));
_fileSize.Append("MB");
}
/// <summary>
/// 視頻長度設(shè)置
/// </summary>
private void VideoLength()
{
if (!_movieCapture.IsRealTime)
{
_lastEncodedSeconds = (uint)Mathf.FloorToInt(_movieCapture.CaptureStats.NumEncodedFrames / _movieCapture.FrameRate);
}
else
{
_lastEncodedSeconds = _movieCapture.CaptureStats.TotalEncodedSeconds;
}
_lastEncodedMinutes = _lastEncodedSeconds / 60;
_lastEncodedSeconds = _lastEncodedSeconds % 60;
_lastEncodedFrame = _movieCapture.CaptureStats.NumEncodedFrames % (uint)_movieCapture.FrameRate;
_videoLength.Clear();
_videoLength.Append(_lastEncodedMinutes.ToString("00"));
_videoLength.Append(":");
_videoLength.Append(_lastEncodedSeconds.ToString("00"));
_videoLength.Append(".");
_videoLength.Append(_lastEncodedFrame.ToString("000"));
}
- 開始錄制: 開始進(jìn)行Game視圖錄制
- 結(jié)束錄制: 結(jié)束錄制,錄制結(jié)束后,視頻會進(jìn)進(jìn)行保存
- 最新錄制: 顯示最近錄制的視頻記錄
- File Size: 當(dāng)前錄制視頻的文件大小
- Video Length: 當(dāng)前錄制視頻的時長
2、視頻列表功能
在視頻錄制完成后,會將視頻信息進(jìn)行保存,當(dāng)點擊最新錄制會顯示視頻列表窗口,在這里你可以找到所有的最近錄制的視頻記錄。
public class RecordList : MonoBehaviour
{
public RecordItem recordItemPre;
private List<RecordItem> records = new List<RecordItem>();
/// <summary>
/// 初始化記錄表
/// </summary>
/// <param name="saveInfos"></param>
public void InitRecordList(List<VideoInfo> videoList)
{
gameObject.SetActive(true);
for (int i = 0; i < records.Count; i++)
{
Destroy(records[i].gameObject);
}
records.Clear();
for (int i = 0; i < videoList.Count; i++)
{
RecordItem recordItem = Instantiate(recordItemPre, recordItemPre.transform.parent);
recordItem.InitRecordItem(videoList[i]);
records.Add(recordItem);
recordItem.gameObject.SetActive(true);
}
}
}
public class RecordItem : MonoBehaviour
{
public Text videoName;
public Text videoSize;
public Text videoLength;
public Text lastModifyTime;
public Button view;
public VideoControl videoControl;
private VideoInfo videoInfo;
// Start is called before the first frame update
void Start()
{
view.onClick.AddListener(() => {
videoControl.VideoControlInit(videoInfo.videoPath);
});
}
/// <summary>
/// 初始化記錄表
/// </summary>
/// <param name="saveInfo"></param>
public void InitRecordItem(VideoInfo videoInfo)
{
this.videoInfo = videoInfo;
videoName.text = videoInfo.videoName;
videoSize.text = (videoInfo.videoSize / (1024f * 1024f)).ToString("F1") + "MB";
videoLength.text = (videoInfo.videoLength / 60).ToString("00") + ":" + (videoInfo.videoLength % 60).ToString("00");
lastModifyTime.text = videoInfo.lastModifyTime;
}
}
- 名稱: 視頻名稱
- 大?。?/strong> 視頻文件大小
- 時長: 視頻時長
- 最后修改: 視頻保存時間
- 查看: 播放視頻
3、視頻播放功能
在視頻列表中,點擊查看按鈕就可以播放該視頻。
public class VideoControl : MonoBehaviour
{
public Text title;
public VideoPlayer player;
public Slider videoSlider;
public Button close;
public Text videotime;
public Text videoLength;
string videoPath;
private void Update()
{
if (player.isPlaying)
{
SetCurPlayTime();
SetPlaySchedule();
}
}
private void Awake()
{
player.prepareCompleted += OnPrepareCompleted;
}
private void OnDestroy()
{
player.prepareCompleted -= OnPrepareCompleted;
}
public void VideoControlInit(string videoPath)
{
this.videoPath = videoPath;
gameObject.SetActive(true);
SetVideoPlayPath(videoPath);
PlayVideo();
}
/// <summary>
/// 視頻準(zhǔn)備
/// </summary>
/// <param name="videoSource"></param>
private void OnPrepareCompleted(VideoPlayer videoSource)
{
SetVideoName();
SetVideoLength();
}
/// <summary>
/// 設(shè)置視頻名稱
/// </summary>
private void SetVideoName()
{
title.text = Path.GetFileNameWithoutExtension(videoPath);
}
/// <summary>
/// 設(shè)置播放器路徑
/// </summary>
/// <param name="path"></param>
public void SetVideoPlayPath(string path)
{
player.url = path;
}
/// <summary>
/// 當(dāng)前視頻播放時間
/// </summary>
private void SetCurPlayTime()
{
videotime.text = TimeProcess(player.time);
}
/// <summary>
/// 設(shè)置視頻時長
/// </summary>
/// <param name="length"></param>
private void SetVideoLength()
{
videoLength.text = TimeProcess(player.length);
}
/// <summary>
/// 設(shè)置播放進(jìn)度
/// </summary>
private void SetPlaySchedule()
{
videoSlider.value = (float)player.frame / player.frameCount;
}
/// <summary>
/// 時間處理
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
private string TimeProcess(double time)
{
int minutes = (int)time / 60;
int seconds = (int)time % 60;
return minutes.ToString("00") + ":" + seconds.ToString("00");
}
/// <summary>
/// 視頻播放
/// </summary>
public void PlayVideo()
{
player.Play();
}
}
視頻播放時,會顯示視頻名稱、時長、當(dāng)前時間和播放進(jìn)度
四、MovieCapture項目地址
項目完整地址:
https:///CTLittleNewbie/ScreenRecord_240520/
|