Unity obtains a frame of the video, generates a thumbnail image

Source: Internet
Author: User

Unity does not have an API to directly capture a frame image of a video, so it's up to you to write a thumbnail.

Pictures and videos can be used in this way to generate thumbnails, the other, reproduced please indicate the source, thank you.

1 usingSystem.Collections;2 usingSystem.Collections.Generic;3 usingUnityengine;4 usingUnityengine.video;5 usingSystem.IO;6 7 8  Public classNewbehaviourscript:monobehaviour {9 Ten videoplayer VP; One texture2d videoframetexture; A rendertexture rendertexture; -     voidStart () -     { theVideoframetexture =NewTexture2d (2,2); -VP = getcomponent<videoplayer>(); -Vp.playonawake =false; -Vp.waitforfirstframe =true; +  -Vp.sendframereadyevents =true; +Vp.frameready + =Onnewframe; A VP. Play (); at  -  -     } -     intFramesvalue=0;//get a picture of the first few frames of video -     voidOnnewframe (Videoplayer source,Longframeidx) -     { inframesvalue++; -         if(framesvalue== -) { toRendertexture = Source.texture asrendertexture; +             if(videoframetexture.width! = Rendertexture.width | | Videoframetexture.height! =rendertexture.height) { - videoframetexture.resize (Rendertexture.width, rendertexture.height); the             } *Rendertexture.active =rendertexture; $Videoframetexture.readpixels (NewRect (0,0, Rendertexture.width, Rendertexture.height),0,0);Panax Notoginseng videoframetexture.apply (); -Rendertexture.active =NULL; theVp.frameready-=Onnewframe; +Vp.sendframereadyevents =false; A         } the     } +          -     voidondisable () $     { $         if(! File.exists (application.persistentdatapath+"/temp.jpg")) { -Scaletexture (Videoframetexture, -, -, (application.persistentdatapath+"/temp.jpg")); -         } the     } -     //generate thumbnail imagesWuyi     voidScaletexture (texture2d source,intTargetwidth,intTargetheight,stringSavepath) the     { -          Wutexture2d result =NewTexture2d (Targetwidth, Targetheight,textureformat.argb32,false); -  About          for(inti =0; i < result.height; ++i) $         { -              for(intj =0; J < Result.width; ++j) -             { -Color Newcolor = source. Getpixelbilinear ((float) J/(float) Result.width, (float) I/(float) result.height); A result. SetPixel (J, I, Newcolor); +             } the         } - result. Apply (); $ file.writeallbytes (Savepath, result. Encodetojpg ()); the     } the  the}

Unity obtains a frame of the video, generates a thumbnail image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.