aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Play video file frame by frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Play video file frame by frame" Watch "Play video file frame by frame" New topic
Author

Play video file frame by frame

Sanjeev Mehta
Greenhorn

Joined: Feb 13, 2009
Posts: 12
Hi,

I am trying to read and play a video file frame by frame. I got some starter code which I am trying to understand. The code takes in a video file and displays one frame.
From the code I understand that the file is being read into the byte array.

I am unable to figure out what this part of the code does



I get that the image is being formed pixel by pixel but dont understand the loop completely.
Also, how do I read the byte array to get pixel values for the other frames. I want to display the complete video frame by frame.


Here is the code displaying a single frame from the video.



Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42611
    
  65
I am unable to figure out what this part of the code does

BufferedImages need the red, green and blue components of a pixel arranged within an int in a specific way; that's what line 5 does. If you check out the "Field Summary" of the javadocs: java.awt.image.BufferedImage you'll see the various ways in which images can be constructed from RGB values.

how do I read the byte array to get pixel values for the other frames. I want to display the complete video frame by frame.

You'd create a new BufferedImage object for each frame, and then use "label.setImage(img))" to change the image that's displayed (you may have to update the label to see the change).


Ping & DNS - my free Android networking tools app
Sanjeev Mehta
Greenhorn

Joined: Feb 13, 2009
Posts: 12
The video file I am trying to render is in the standard rgb format. The video file contains a frame by frame dump, each frame is containing width*height red pixels, width*height green pixels and width*height blue pixels

So I think 0 to width*height elements from bytes array are the red pixels for each frame, width*height to 2*width*height from the bytes array are the green pixels and the remaining width*height are the red pixels.

So each frame uses 3*height*width elements from the bytes array. So number of frames are bytes.length/(3*height*width). However when I run the loop and display these many frames the whole video is not played. So is the way in which i calculate the number of frames incorrect?

Also there is a noticeable delay between display of 2 frames and the 20 seconds video takes a long time to run.

Here is what I tried to display the video,


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42611
    
  65
However when I run the loop and display these many frames the whole video is not played.

Also there is a noticeable delay between display of 2 frames and the 20 seconds video takes a long time to run.

These two statements seem to contradict each other. Are you seeing all frames, just very slowly, or not?

One thing you should definitely do is to wrap a BufferedInputStream around the FileInputStream, with a buffer large enough to hold at least a few images. That'll cut way down on I/O time.
Sanjeev Mehta
Greenhorn

Joined: Feb 13, 2009
Posts: 12
I once ran the 2 loops inside a for loop as there are (bytes.length)/(height*width*3) frames.


This run of the code displays only a few frames.


And I ran it once inside a while loop as

This displays all the frames but also give an ArrayIndexOutOfBounds exception.


Both the runs have a noticeable display between 2 frames. I will check out BufferedInputStream.

I think I have no got the condition to terminate the main loop correct.
 
 
subject: Play video file frame by frame