0

I've did a lot of research, but I can't find a suitable solution that works with Unity3d/c#. I'm using a Fove-HMD and would like to record/make a video of the integrated camera. So far I managed every update to take a snapshot of the camera, but I can't find a way to merge this snapshots into a video. Does someone know a way of converting them? Or can someone point me in the right direction, in which I could continue my research?

public class FoveCamera : SingletonBase<FoveCamera>{

 private bool camAvailable;
 private WebCamTexture foveCamera;
 private List<Texture2D> snapshots;

 void Start ()
 {
     //-------------just checking if webcam is available
     WebCamDevice[] devices = WebCamTexture.devices;
     if (devices.Length == 0)
     {
         Debug.LogError("FoveCamera could not be found.");
         camAvailable = false;
         return;
     }

     foreach (WebCamDevice device in devices)
     {
         if (device.name.Equals("FOVE Eyes"))
             foveCamera = new WebCamTexture(device.name);//screen.width and screen.height 
     }

     if (foveCamera == null)
     {
         Debug.LogError("FoveCamera could not be found.");
         return;
     }
     //-------------camera found, start with the video

     foveCamera.Play();
     camAvailable = true;
 }

 void Update () {
     if (!camAvailable)
     {
         return;
     }

     //loading snap from camera
     Texture2D snap = new Texture2D(foveCamera.width,foveCamera.height);
     snap.SetPixels(foveCamera.GetPixels());
     snapshots.Add(snap);
 }
}

The code works so far. The first part of the Start-Method is just for finding and enabling the camera. In the Update-Method I'm taking every update a snapshot of the video.

After I "stop" the Update-Method, I would like to convert the gathered Texture2D object into a video.

Thanks in advance

Pezl
  • 3
  • 1
  • 4

1 Answers1

0

I see two methods here, one is fast to implement, dirty and not for all platforms, second one harder but pretty. Both rely on FFMPEG.

1) Save every frame into image file (snap.EncodeToPNG()) and then call FFMPEG to create video from images (FFmpeg create video from images) - slow due to many disk operations.

2) Use FFMPEG via wrapper implemented in AForge and supply its VideoFileWriter class with images that you have. Image sequence to video stream?

Problem here is it uses System.Bitmap, so in order to convert Texture2D to Bitmap you can use: How to create bitmap from byte array?

So you end up with something like:

Bitmap bmp;
Texture2D snap;
using (var ms = new MemoryStream(snap.EncodeToPNG()))
{
    bmp = new Bitmap(ms);
}
vFWriter.WriteVideoFrame(bmp);

Both methods are not the fastest ones though, so if performance is an issue here you might want to operate on lower level data like DirectX or OpenGL textures.

R2RT
  • 1,822
  • 11
  • 22
  • your solution sounds good. However, FFMPEG doesn't seem to work with unity. I've already tried many version, but either unity doesn't support the c# version or the dlls aren't recognized. – Pezl Aug 16 '17 at 11:23
  • I've used FFMPEG with Unity, but by using own C++ DLL, never with pure C#. Have you checked if you are not mixing 32 and 64 bits architectures DLLs? AForge get shipped with 32-bit FFMPEG, but Unity Editor is usually 64-bit (and since Unity 2017 only 64-bit, Unity 5 can be still downloaded as 32-bit https://unity3d.com/get-unity/download/archive) – R2RT Aug 16 '17 at 13:59
  • I've played around with AForge and seems I forgot that Unity does not have reference to `System.Drawing`, where `Bitmap` is defined. Working it around is even more dirty (copying System.Drawing.dll to Plugins folder), so I'd rather skip that option – R2RT Aug 16 '17 at 14:27
  • Thanks, i'll keep that in mind. But how did you manage to get the Afroge.Video.FFMPEG working? I've downloaded the latest "libs only" version here: https://code.google.com/archive/p/aforge/downloads All the libs(Afroge.Video.Kinect,Afroge.Video.VFW,..) are working just fine, expect the Afroge.Video.FFMPEG. Somehow my code editor can't find this specific lib. All the other libs are working, so I don't think, that I have a problem with 32/64 architectures DLLs. Or am I wrong? – Pezl Aug 16 '17 at 14:58
  • I didn't get it working as well... I wanted to check sources before mentioning it and seems it was made in managed C++ not C#, maybe it causes some problems. I found out that you could save AVIs without AForge (Video.VFW, http://www.aforgenet.com/framework/features/avi_files.html) but the `Bitmap` problem remains. Anyway, if you want we should continue discussion on chat, not in comments here. – R2RT Aug 16 '17 at 17:36