We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm trying to play some movies and sound in a processing sketch; there are 10 movies and 1 sound file - all the movies are fairly small [<=5MB]; I create them in setup and call .play() on them, and then in draw() I use image() on them, and I also use movieEvent() function to call .read() on them; whenever I run this the videos lag and my CPU spikes to 100%
Surely playing 10 concurrent movies isn't that onerous?
Does anyone have any ideas for how to improve the performance or can tell me if I'm doing something wrong or missing something?
Any help would be much appreciated, thanks
Thanks
Answers
Asking for help w/o providing the defective code is like expecting us being clairvoyants!
Are they displayed at the same time as well?
Yes they are all displayed concurrently.
Here is an example of my code.
Btw I am running a quad core i5 @ 3.3GHz and 8GB RAM.
The videos are all <1 min, <=~5MB (h264 mp4s) and 720x480
reading a frame from a video is hideously complex because of the compression algorithm. look up the h264 spec sometime.
you're then redrawing them, and rescaling them, 30 times a second even if they haven't changed
also, that's 12 movies, not 10. and look into using arrays.
Can I use a less compressed video format then? Would that be faster?
With regards to them changing, I've tried calling .available() on the Movie in the draw method and only calling image() if that's true, but it hasn't changed the performance - that's only drawing it if there's a new frame right?
I've tried reducing the number of movies playing; when it's one or two it's fine, but anything like 4 or above (along with playing sound as well which I haven't even included in the sample code) has the same outcome, so whether it's 9, 10, 11 or 12 movies, it's still maxing out on CPU.
Arrays will make the code nicer, but won't be affecting the fundamental performance.
it should help. as would resizing the videos to the target size (480x360) to skip the scaling.
but given that 4 or above videos are maxing out your processor and you want 300% of that then you may be SOL.
Ok thanks - I'll try to change the videos to be the target size and use a less compressed format.
I was hoping there would be something more obvious like I was using the library in the wrong way! (though maybe the above counts lol)
If you can, I'd suggest you to re-encode your vids to MPEG-2, the same used by the old DVD movies.
It generates much bigger files than MPEG-4 and everything else, but it's much lighter to decompress.
Also try to re-scale them all to fixed 480x360 rather than asking image() to do it on-the-fly.
Replacing image() w/ set() also helps a lot. Except when using an OpenGL-based renderer.
Also your movieEvent() is completely wrong! We only invoke read() over the passed Movie argument!
Anyways, here's my attempt. It's assuming that all videos are 480x360 & ".mp4"
And all of your vid files are named from "movie0" to "movie11" too.
Of course, change those constants in order to match your current configuration.
It's also daring to use the newest P3's renderer FX2D. Change it back to JAVA2D if you got any problems.
Last warning: I haven't tested this sketch, since I don't have those videos nor CPU power for it: :-\"
sigh
here's mine.
the transcoding to mpg and the rescaling helped, it is a lot smoother than the original, but it's still ~100%. and the mpg is 5 times the size of the mp4.
(mjpeg format is older and even less compressed than mpeg2 and helps a tiny bit but is bigger still)
laptop is a dual core centrino, 1368x768, 2009 vintage...
Hi guys,
Thanks so much - I changed the format of the videos to mpeg2 and resized them to 480x360 which is how they are being displayed on the sketch. I am also using the .set() method as well.
It consumes about 30-50% CPU and they videos and sound play as I expect them to now.
I did try using the FX2D renderer but I get an Exception at start up which says: "You need to use "Import Library" to add FX2D to your sketch" - I've tried searching for this and also how to use the FX2D renderer, but nothing meaningful has come up on it. The Exception messages suggests I would be able to add it from Sketch -> Import Library but when searching for FX2D or anything to do with renderers nothing comes up. I also wasn't able to find an example of someone importing the library/class in their sketch. If you could tell me what I'm missing with that it would be much appreciated.
However your solutions have solved my problem - my thanks for your speedy and informative answers!
Nice performance ideas from @koogs. Forgotten about the
%
and integer/
trick. =P~An extra
boolean[]
can spare the CPU a little by avoiding unnecessary rendering updates.Still, changing set() back to image() was a bad move! 8-|
Upgraded my own version to use those performance tricks as well: :ar!
Very strange error message! FX2D comes bundled w/ Processing 3. 8-X
Perhaps you're still using P2??? :-t
That's what I gathered from your comment - I am definitely on 3! [3.0.1 to be exact]
Although I'm still @ P v3.0, I can't see why v3.0.1 would fail to recognize FX2D renderer! :-O
Copy & paste my last code w/o any modifications and check whether the error shows up again. ~O)
https://raw.GitHubUserContent.com/processing/processing/master/build/shared/revisions.txt
Hi
I've now run into a similar problem. I'm trying to make a "moving font", where each letter of the alphabet is represented by a video, while the videos loop and start and different spots.
Once I'm writing a sentence longer than 30-45 the frame rate drops from 29-30 to 4-5.
What is the video content? You should probably use sprites for this, not movies. I would expect a major performance difference.
You can also optimize lookups by doing only 26 (or however many characters) passes. So, load the current sprite sheet state for the a animation, then draw every a on the screen (they are all synchronized, right?) then b, then c... now no matter how many characters you type, you are only doing 26 lookups. This is particularly easy if you use a fixed-width "font".
there's a resize involved as well
decoding n videos and then downsizing them is going to be terrible even for small values of n.
but this piggyback question already has it's own thread. i'm closing this one.
https://forum.processing.org/two/discussion/25858/associating-videos-to-characters-generatively