Reputation: 485
How to process videos in Java without any external API?
I have a mpeg4 or mpeg2 video which I want to view in a JPanel
reading each frame one after another and then displaying the frames with paintComponent()
. Displaying each file as BufferedImage
in Graphics g
.
My question is how to get an array of BufferedImage
class from the video files?
Upvotes: 0
Views: 201
Reputation: 13256
You certainly can process videos in Java without relying on external libraries. It will take a fair amount of coding effort on your part, so be forewarned.
If you're decoding an MPEG-2 file, that will probably be a program stream, so you'll need to write the code to take that apart. MPEG-4 part 2 video will probably arrive in an MP4 container which will require a lot of code to take apart (not too hard, just a lot of details). So this is just the container; inside will be chunks of compressed video and audio.
Now you will need to decode either the MPEG-2 or MPEG-4 video. This will entail parsing variable length codes from the bitstream and recovering syntax elements. For intraframes, this will give you reconstruction data to apply to a stream of macroblocks. You will combine things like DCT coefficients, differential coding, dequantization factors, zigzag scan patterns, discrete cosine transforms, and possibly post-processing filters in order to recover the original image. That's just for intraframes; then there are the interframes which also apply motion vectors and copy data from previously decoded frames.
After getting a frame, you will find that it is in a YUV colorspace. You will probably need to manually convert it to an RGB colorspace in order to plot it onto a BufferedImage.
It's entirely possible that you could implement all of this on your own. Or you could find an appropriate Java-friendly multimedia library, complete with its own API, to do the heavy lifting for you.
Upvotes: 1