Android - Play Only Specific Range of Frames in VideoView? - java

Is there a way to only play a specific range of frames in an Android VideoView?
I'm trying to avoid creating a separate clip if it's possible to reuse the same clip for multiple use cases.
In certain situations, I only want to play frames 1-30, for example.
_videoView.seekTo(0);
_videoView.start();
// Stop at frame 30

Related

How do I analyze golf shot using cameraX api in android

I am creating an android app which will be used to analyze golf shots. I am using cameraX api to get the frames into surfaceview and analyze callback to further processing.
Initially I am using Google Ml-kit object detection to detect the ball but video lags too much that most of the frames are skipped.
How can I use frames in realtime as there maybe 2,3 frames having the ball because the shot will be too fast?
I have tried to gather the frames as bitmaps into an ArrayList and then use that list for further processing but the number of frames vary for a 5 second video.
Should I record the video first for a constant time 5s and then extract frames from it?
Note: The above implementation is for starting because later I need to detect shot in realtime which I tried to detect the ball + detecting the specific sound but in vien.
Furthermore, I need to calculate framrate also which will later be used to calculate the speed of the ball.
Please suggest of any better implementation.
Thanks

Skip frames in video playback with MediaExtractor and MediaCodec

I'm using MediaExtractor/MediaCodec to decode a video and render it to a TextureView. As a template, I've used the code from: https://github.com/vecio/MediaCodecDemo/blob/master/src/io/vec/demo/mediacodec/DecodeActivity.java
I'd like to be able to playback the video at 2x speed. Luckily, the media encoding/decoding is fast enough so that I can accomplish this by letting the MediaCodec decode every frame, and then only render every other frame to the screen. However this doesn't feel like a great solution, particularly if you want to increase playback by an arbitrary value. For example, at 10x speeds, the Codec is not able to decode frames fast enough to playback every 10th frame at 30 fps.
So instead I'd like control playback by calling MediaExtractor.advance() multiple times to skip frames that don't need to be decoded. For example:
...
mDecoder.queueInputBuffer(inIndex, 0, sampleSize, mExtractor.getSampleTime(), 0);
for (i = 0; i < playbackSpeedIncrease; i++) {
mExtractor.advance();
}
...
With this code, in theory the extractor should only extract every nth frame, where n is defined by the variable 'playbackSpeedIncrease'. For example, if n = 5, this should advance past frames 1-4 and only extract frame 5.
However, this does not work in practice. When I run this code, the image rendered to the screen is distorted:
Does anyone know why this is? Any suggestions for better ways to playback videos at an arbitrary speed increase?
You can't generally do this with AVC video.
The encoded video has "key" (or "sync" or "I") frames that hold full images, but the frames between key frames hold "diffs" from the previous frames. You can find some articles at wikipedia about video encoding methods, e.g. this one. You're getting nasty video because you skipped a key frame and now the diffs are being computed against the wrong image.
If you've ever seen video fast-forward smoothly but fast-reverse clunkily, e.g. on a TiVo, this is why: the video decoder plays forward quickly, but in reverse it just plays the I-frames, holding them on screen long enough to get the desired rate. At "faster" forward/reverse it evens out because the device is just playing I-frames. You can do something similar by watching for the SAMPLE_FLAG_SYNC flag on the frames you get from MediaExtractor.
In general you're limited to either playing the video as fast as the device can decode it, or playing just the key frames. (If you know enough about the layout of a specific video in a specific encoding you may be able to do better, e.g. play the I and P but not the B, but I'm not sure that's even possible in AVC.)
The frequency of I frames is determined by the video encoder. It tends to be one or two per second, but if you get videos from different sources then you can expect the GOP size (group-of-pictures, i.e. how many frames there are between I-frames) to vary.

Why doesn't MediaPlayer.seekTo(t) go to the exact specified instant "t"?

I'm trying to get the media player to play a specific range in a locally stored video. It doesn't seem to start at the specified time I tell it to.
Example: when I seek to 1000, it works. But when I seek to 1500, it goes to 2000 instead.
I also tried pausing seeking then starting on seek completion, it doesn't make any difference.
This is the code:
mediaPlayer.start();
mediaPlayer.seekTo(time);
Is this normal? Or am I using the media player the wrong way?
This is an encoding issue. Videos have keyframes (i-frames) that store more information than other frames. A non keyframe can be built only given the previous keyframe. Trying to display a non keyframe will show green spots and pixelated jittery screen.
Now, on some android devices there's no workaround implemented for this so you get this weird behavior. On a Nexus S for example seekTo() doesn't necessarily go to the specified frame. I tried other devices with the same android version and they seek just fine.

Rendering video on Swing

I am developing application to monitor 20 video streams at a time. I will have JFrame, and 20 boxes (e.g JPanel) inside JFrame to display 20 streams. I am able to load stream and decode using xuggler, but now how can I display this over Swing JPanel?
I am able to play sound on SourceDataLine, my problem is only, how to display 20 * 30 = 600 video frames in second on Swing component?
Also xuggler outputs YUV420P pixel format decoded frames, is there overhead in converting this to RGB, create BufferedImage and display on Swing Component?
Please guide me on this. I want to display 20 video streams at a time in swing components.
Here's some code I Googled that will convert a YUV420 file to BufferedImage frames. You can use this as a base for what you want.
You probably won't be able to process 600 video frames a second on a PC either. You'll have to see how many video frames you can convert per second, and drop the rest of the frames.
Probably, the best way to process 20 video feeds is to have 20 threads grabbing a video frame, converting the video frame to a BufferedImage and passing the BufferedImage to the Event Dispatch Thread (EDT) for Swing to draw on the corresponding JPanel. When the thread comes back to grab the next video frame, you'll have automatically dropped the frames that the PC didn't have time to process.

how to embed a movie clip in a SWF using transform jar (flagstone)

I have been stuck since past 2 days in one small issue. I'm trying to create a movie from images & the movie will have separate starting & ending images. The sound will start after the first image and will continue before the end image. According to the length of the sound the intermediate images (total number not fixed) are set to a framerate. I want to have the first & last image of specific fixed duration and the intermediate images depending on the length of the audio. Now since the frame-rate is decided by the length of the sound, larger the length implies smaller framerate and hence large time for the first & last time. In order to solve this issue, I thought that I should create a separate movie clip (for the starting & end image) and add to the original movie. But I'm unable to do so ? Please let me know how to add a movie clip to the main movie. I'm using transform api from flagstone.

Categories