bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.55k stars 1.58k forks source link

Correct way to decode video file and show it with FFmpegFrameGrabber - Android #100

Open ShihabSoft opened 9 years ago

ShihabSoft commented 9 years ago

Iam querying around the internet for a week.For a correct way to implement a video playback using FFmpegFrameGrabber.

I have tried using all code snippets.

i know when each and every frame the frame number is increased and timestamp also incremented.

But for each and every video file the fps rate may vary.

So in the loop

I used Thread.sleep(1000/fps) for making the video frames play as a smooth movie.

But it lags so much between each and every frame.

And when i used a static millisecond like 10 or 20 or 30 its goes smooth for one video file.But after changing the video it goes so fast or so lags.

Can u please mention the correct way to grab frame and set it to the ImageView.

And one more request.

Can anyone please mention what is the correct way to play the audio samples from the grabbed frame.

saudet commented 9 years ago

sleep() isn't usually precise enough for smooth video playback. We usually need to do some amount of busy waiting in a tight loop.

If someone has more specific information about the best way to go about this on Android, please let us know! Thanks

saudet commented 9 years ago

Let me know when you have a sample with audio and everything ready, it will be of great help. Thanks for everything!

leofarage commented 9 years ago

I'm doing something like what you're trying to do. I based my code on the Lunar Lander sample code from google which uses a SurfaceView for drawing the frames. The video is pretty smooth doesn't matter how many times I play it.

My problem is with playing the audio. I'm using this tutorial for playing the audio from FrameGrabber -- which plays fine but the audio and frames are not synced, having the audio ending before the frames.

I noticed that the FrameGrabber returns a bunch of audio before the first frame and the last second returns only frames but no audio. I noticed that a Frame never has audio and video -- just audio or video.

As soon as I can resolve this issue I can send it to github to be used as a sample.

Cheers.

saudet commented 9 years ago

Each frame comes with a timestamp, so we need to play them back, based on that info. I don't know what would be the best way to achieve synchronization on Android, but I suspect we can find a lot of info about that from the indie games community...

ShihabSoft commented 9 years ago

@leofarage Thanks for using my code.