Playing Video (incl. Audio) using JavaCV on Android

617 views
Skip to first unread message

bwa

unread,
Dec 4, 2014, 7:05:45 AM12/4/14
to jav...@googlegroups.com
Hi there is a nice example how to record a video from the camera using JavaCV and FFMpegFrameRecorder.
Is there anywhere a simple example which shows the other way around: 

Playing a Videofile (possibly including audio) using FFMpegFrameGrabber on the Android Platform?

kind regards

Mihael Francekovic

unread,
Dec 5, 2014, 4:18:12 AM12/5/14
to jav...@googlegroups.com

Hi,
Maybe you could grab each frame and convert it to bitmap and then show it in ImageView.. but then you would have to care about timing frames and i think that android devices may be too slow for this approach.
Synchronising sound would also be very impracticall.

Why don't you just use android MediaPlayer and VideoView?

BR,
Mihael

bwa

unread,
Dec 5, 2014, 4:48:56 AM12/5/14
to jav...@googlegroups.com
Hi, thank you for your reply.

Maybe you could grab each frame and convert it to bitmap and then show it in ImageView.. but then you would have to care about timing frames and i think that android devices may be too slow for this approach.
Synchronising sound would also be very impracticall.

Yeah, I also thought of kind of a similar approach. And my fear was that this is not really possible in an efficient way on Android.

Why don't you just use android MediaPlayer and VideoView?

Because I need some way to know the current frame number in the video. When I record the video from the camera using FFMpegFrameRecorder I collect corresponding data for each frame.
And in the video playback I want to display the data alongside the video frame per frame. And I thought using JavaCV would help me here (FrameGrabber).
Android MediaPlayer and VideoView are very restrictive. Maybe I am overlooked something but I think it is not possible to count each frame while playing the video?!?

Mihael Francekovic

unread,
Dec 5, 2014, 5:12:06 AM12/5/14
to jav...@googlegroups.com
Does MediaPlayer have a method that returns current time in video playback?
I belive that it does, and if it returns time in mili/nano seconds.. you can calculate which frame is currently displayed via timeStamp and/or fps :)

--

---
You received this message because you are subscribed to the Google Groups "javacv" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacv+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bwa

unread,
Dec 5, 2014, 5:58:55 AM12/5/14
to jav...@googlegroups.com
Oh, looks like I have found a pretty usefull library (maybe it's also worth it to include this in JavaCV? :) ) :


This library provides a method: getFrameAtTime(long time)

However, what I really nead is a method like getFrame(int frameNumber)
But I am wondering if this is even possible, without "playing" the whole video video in before...

Mihael Francekovic

unread,
Dec 5, 2014, 6:57:11 AM12/5/14
to jav...@googlegroups.com
Very nice,

You can use that method to get Frame with index that you want.
Try to make method like.. getIndex(VideoView.getCurrentPosition())

getCurrentPosition returns time in milliseconds.
Calculate duration of 1 frame by dividing video duration in milliseconds / fps
Since frameGrabber doesn't return fps reliably, i would suggest you to use library Mp4Parser,

and determine fps like : 

        Movie video = MovieCreator.build(filname);

        long[] sampleTimes = video.getTracks().get(0).getSampleDurations();
        long duration = 0;
        for (long sampleTime : sampleTimes) {
            duration += sampleTime;
        }

        long durPerSample =  duration /  video.getTracks().get(0).getSamples().size();
        double fps = (double)video.getTracks().get(0).getTrackMetaData().getTimescale() / durPerSample;

        return fps;

Br,
Mihael

Samuel Audet

unread,
Dec 6, 2014, 4:49:06 AM12/6/14
to jav...@googlegroups.com
On 12/05/2014 08:57 PM, Mihael Francekovic wrote:
> getCurrentPosition returns time in milliseconds.
> Calculate duration of 1 frame by dividing video duration in milliseconds / fps
> Since frameGrabber doesn't return fps reliably, i would suggest you to
> use library Mp4Parser,

Would you know how to fix that BTW? Let me know, it would be nice to
have that work properly somehow, thanks!

Samuel

Samuel Audet

unread,
Dec 6, 2014, 4:52:50 AM12/6/14
to jav...@googlegroups.com
On 12/05/2014 07:58 PM, bwa wrote:
> Oh, looks like I have found a pretty usefull library (maybe it's also
> worth it to include this in JavaCV? :) ) :
>
> https://github.com/wseemann/FFmpegMediaMetadataRetriever

Yes, it would be very nice to integrate that with FFmpegFrameGrabber. Is
this something you would like to contribute yourself? If so, please send
me a pull request of your modifications!! Thanks

Samuel
Reply all
Reply to author
Forward
0 new messages