Created
April 4, 2025 17:29
-
-
Save ratozumbi/468afcdff2077bab3e227589f8844338 to your computer and use it in GitHub Desktop.
Create a thumbnail from a video os reuse an existing thumbnail
This file contains hidden or 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
#region VideoThumbFlow | |
/// <summary> | |
/// Must be called upon instantiating this prefab | |
/// </summary> | |
/// <param name="preview">The video related to the ID</param> | |
/// <param name="id">The index on hte GalleryData for the content</param> | |
/// <param name="title">Short description or name</param> | |
private void InitializeVideo(String videoUrl, int id, string title) | |
{ | |
gameObject.SetActive(true); | |
var _videoPlayer = GetComponent<VideoPlayer>(); | |
_id = id; | |
GetComponentInChildren<TMP_Text>().text = title; | |
_videoPlayer.url = videoUrl; | |
string pathThumb = Application.streamingAssetsPath +"/Thumbs/"+GetComponentInChildren<TMP_Text>().text+".jpg"; | |
if (File.Exists(pathThumb)) | |
{ | |
Texture2D media; | |
//save texture2d | |
byte[] bytes = File.ReadAllBytes(pathThumb); | |
media = new Texture2D(1, 1); | |
media.LoadImage(bytes); | |
targetImagePreview.texture = media; | |
Debug.Log("Load thumb from cache " + targetImagePreview); | |
targetImagePreview.texture = media;//Sprite.Create(media,new Rect(0, 0, media.width, media.height),Vector2.zero); | |
RenderTexture.active = null; | |
float aspect; | |
aspect = media.width / (float)media.height; | |
var asp = GetComponentInChildren<AspectRatioFitter>(); | |
asp.aspectRatio = aspect; | |
} | |
else | |
{ | |
_videoPlayer.prepareCompleted += VideoPlayerOnprepareCompleted; | |
_videoPlayer.Prepare(); | |
} | |
} | |
private void VideoPlayerOnprepareCompleted(VideoPlayer source) | |
{ | |
source.prepareCompleted -= VideoPlayerOnprepareCompleted; | |
source.frameReady += VideoPlayerOnframeReady; | |
source.sendFrameReadyEvents = true; | |
source.seekCompleted += VideoPlayerOnseekCompleted; | |
var middleFrame = (int) (source.frameCount / 2); | |
if (middleFrame > 0) source.frame = middleFrame; | |
// source.frame = (long)source.frameCount - 1; | |
} | |
private void VideoPlayerOnseekCompleted(VideoPlayer source) | |
{ | |
source.seekCompleted -= VideoPlayerOnseekCompleted; | |
source.frameReady += VideoPlayerOnframeReady; | |
source.Play(); | |
} | |
private void VideoPlayerOnframeReady(VideoPlayer source, long frameidx) | |
{ | |
source.frameReady -= VideoPlayerOnframeReady; | |
source.Pause(); | |
source.sendFrameReadyEvents = false; | |
ExtractVideoPreview(source); | |
Destroy(source); | |
} | |
void ExtractVideoPreview(VideoPlayer source) { | |
RenderTexture renderTexture = source.texture as RenderTexture; | |
if (renderTexture == null) return; //IDK why, but this may happen sometimes | |
var media = new Texture2D(renderTexture.width, renderTexture.height); | |
RenderTexture.active = renderTexture; | |
media.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); | |
media.Apply(); | |
Debug.Log("targetImagePreview is " + targetImagePreview); | |
targetImagePreview.texture = media;//Sprite.Create(media,new Rect(0, 0, media.width, media.height),Vector2.zero); | |
RenderTexture.active = null; | |
string pathThumb = Application.streamingAssetsPath +"/Thumbs/"+GetComponentInChildren<TMP_Text>().text+".jpg"; | |
//check if folder exists or create it | |
if (!Directory.Exists(Application.streamingAssetsPath + "/Thumbs")) | |
{ | |
Directory.CreateDirectory(Application.streamingAssetsPath + "/Thumbs"); | |
} | |
using (FileStream targetFile = File.Open(pathThumb, FileMode.OpenOrCreate)) | |
{ | |
//save texture2d | |
byte[] bytes = media.EncodeToJPG(); | |
targetFile.Write(bytes, 0, bytes.Length); | |
} | |
float aspect; | |
aspect = media.width / (float)media.height; | |
var asp = GetComponentInChildren<AspectRatioFitter>(); | |
asp.aspectRatio = aspect; | |
} | |
#endregion VideoThumbFlow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment