save video stream to file without losing performance - c#

I'm working with kinect and I want to save the video and audio stream do a file (it doesn't matter if I can play it or not, I want to save the raw data).
My question is, if I'm constantly writing to disk (25fps) the computer may lag right? so what I'm trying to do is save to file in an efficient way. I thought of having like a list of images (like 5 seconds) and then write it all to file. What do you think? Is this a correct way?
Or is there another way to do this without losing performance?
Thank you

Writing to disk is a low intensitivity task for the CPU, it mostly just uses a memory buffer and some memory bandwidth. However, if you have to access the disk while you are writing to it, you will experience an increased delay.
As for how to do it; I have never worked with video before but I am thinking it is most easily done by using a buffer to hold the captured frames, and then writing from that buffer to the disk.
Saving the frames into arrays of 125 images (5s*25fps) sounds like an inefficient way to buffer the frames.
As for avoiding losing performance there really is no a way to do this; however I cannot see you losing much performance as the bitrate of the captured video and audio is comparatively low.

Related

Faster saving method of bitmap C# wpf

I'm currently working on machine vision project. The issue is saving all of the images fast enough so that the queue of images doesn't build up in RAM and drain the user's memory. Is there any other method available for fast image saving?
This method helps the CPU issue, but because its not fast enough. The queue of images builds up and overloads the ram so I don't know what else I can do to solve both issues.
The fastest way for writing images in Halcon is using their proprietary format .hobj. It is much faster than any other lossless compression:
You can see the benchmark shown above in example write_image_benchmark.hdev
The only disadvantage is that you cannot open this format without the Halcon license.

Save/Use images without allocating 300mb memory

I am building a windows project in .net 4.0 c#.
I am now saving images to hard drive, and that's not taking any memory at all because i am only loading the image once i want to view it. But now i need to remove the creating of images in hard drive and create it some other way. Like creating a memory stream and save it to an object and serialize it down to hard drive. The important part is that i cant have the images visible in hard drive, they must be encrypted or in an object or something.
So....When i tried to put it in a memory stream and save it to a list and then serialize it down to drive, i got a HUGE program memory allocation because for every image i create,and save as memory stream in my list, i allocate that memory and my program gets over 2-300 mb big.
I really don't have any idea of how to do this, can i somehow save it to memory stream and not allocate that memory in the program? Or can save it some other way without having the pictures totally visible as images in hard drive?
Main thing is as i said, i cant have the images as regular images on hard drive, they must not be able to be viewed by the user without the application. And i need to find a way that don't allocate all computers memory.
Thank you in advance!
save it to memory stream and not allocate that memory in the program
No. If it's in a memory stream, it is obviously in RAM.
So you need to either store the image entirely in RAM, or save it to disk.
If you don't want it to be viewable on the disk, then you need to encrypt the file so that the user can't view it outside of your application. How heavy the encryption is depends on how hard you want it to be to crack. If you don't care that much, then very basic XOR encryption will be very fast and not increase the size of the file. If you do care, then you want to use something like 3DES.
The file access is built on the principle of streams, which can be plug together in a chain. What you can do is, instead of directly reading/writing the images from/to disk through a filestream, you plug a CryptoStream between it.
You can use a GZIPStream and CryptoStream to make the pictures both smaller and encrypted.
This article shows you exactly how;
http://www.liensberger.it/web/blog/?p=33

c# .net, loading images

I have to combine lets say about 100 images (png-files).
The problem is not combining them, that runs quick enough.
But loading them from storage takes up to 4sec.
That is too much time.
So I can use TPL or multiple threads but it is still too slow.
How can I speed it up? To hold all the images in main storage is not an option unfortunately.
The images are quite small: from 4KByte to 10KByte
I'm loading the images that way:
Image img = Image.FromFile(file);
Creating multiple threads does not improve I/O speed in your case. That is about your harddisk's read-write speed.
Loading 100 high quality images in 4 seconds seems normal.
Two ideas:
If the bulk of the lag really is from IO, compress the files. Depending on their contents, even a simple ZIP compression could reduce their size, thus less bytes to read. The work will be to decompress them in memory. I don't know if that's applicable for your case
Lazy load them. Do you need all 100 images loaded all the time? Perhaps you can just load the first ones, or the most important ones first, let the software do the other stuff while it finishes loading the remaining images in the background.
How do you load your image files? Please share a piece of your code. My guess is that you read not whole file at once but something like byte by byte until the EOF... One of ways to optimize file loading is to load whole file into preallocated memory buffer.

Uploading video content whilst still recording

This is a bit of a weird question but, with the functionalities of C++, c# and objective C as we speak is there any possible way for video content to be uploaded whilst its recording. So as you record the video it would be being compressed and uploaded to a website.
Would this involve cutting the video into small parts as you record, hardly noticeable stops and starts during the recording?
If anyone knows if this is at all possible, please let me know.
Sorry for the odd question.
You've just asked for streaming media -- something that's been done for over a decade (and, if you overlook "television", something that's probably been underway in research settings for several decades).
Typically, the video recorder will feed the raw data through filters of some sort -- correct white balance, sharpen or soften the video, image stabilize, and then compress the raw data using a codec. Most codec designs will happily take a block of input, work on it, and then produce a block of encoded data ready for writing. Instead of writing to disk, you could "write" to a socket opened to a remote machine.
Or, if you're working with an API that only writes to disk, you could easily re-read the data off disk as it is being written and send the data to a remote site. You'd have to "follow" the writing using something like tail -f's magic ability to follow the file as it is written. (Heck, if you're just bodging something together for a one-off, I'd even recommend using tail -f as part of your system.)
It depends on if the application recording to disk is locking the file. My guess is that, unless you wrote the recording software, the application locks the file(or doesn't even create the real file) until it stops recording. If you are writing the recording software as well, then yes, you can do this. you would just use sychronized threads.

Working with large bitmaps causes Out of Memory Exception

I need to edit(Increase the height) the Image on the fly.
The file is mostly 5000*4000 in dimension. I see the memory shoots up to peak level when I create a bmp of large dimensions and call Graphics.DrawImage method on the bmp instance.
How do I get rid of the Out Of Memory exception? Is there a way to work with large bitmaps in c# ?
The problem is the Huge amount of Memory required for the operation. Yours is taking about some GigaBytes, so the solution could be to use a Stream and process the file in chunks.
Or the the best option would be to use some Third party library for it. Below are some for .Net
AForge
Image Resizer
Also have a look at this SO question.
https://stackoverflow.com/questions/158756/what-is-the-best-image-manipulation-library
It's depends on you application specific requeirements, it's not very clear from yuor post, but generaly, working with big media files (images, sounds, videos) I think really good solution is
Memory Mapped Files
Save yuor image on the disk in memory mapped file and resize it having on disk, by free yuor RAM as much as possible from a lot of data that you, probably, don't need to have a fast access(in that moment at least)
Hope this helps.
Regards.

Categories

Resources