Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync animation to video #41

Closed
SpecularTheoryBen opened this issue Apr 8, 2019 · 6 comments
Closed

Sync animation to video #41

SpecularTheoryBen opened this issue Apr 8, 2019 · 6 comments
Labels
Android Android platform enhancement 🚀 New feature or request

Comments

@SpecularTheoryBen
Copy link

I am trying to sync an animation or timeline to a video, but it is jittering due to inconsistent frame reads.

I need a way to ensure an animation stays frame locked to the video that is playing. Playable director has allows DSP control, so that could be one way to keep it locked.

We have attempted to drive an animator by getting the current milliseconds and converting it to a frame number, but that seems to be inconsistent. It will be off by a frame for long enough to see it jump.

@SpecularTheoryBen SpecularTheoryBen added the enhancement 🚀 New feature or request label Apr 8, 2019
@AndrewRH
Copy link
Contributor

Hi,

Currently we don't really have a system for this. There might be a few ways you can try though.

On some platforms (like Windows) you can retrieve the timestamp of the texture. This is more accurate than querying the mediaPlayer for it's time, as the mediaPlayer time is the time is decoding but not the frame you're about to display.

long time = mediaPlayer.TextureProducer.GetTextureTimeStamp();

This 64-bit value is returned in HNS format (hundreds of nanoseconds), so you might want to convert it to seconds or frames:

double seconds = time / 10000000.0;

float frameRate = mediaPlayer.Info.GetVideoFrameRate();
int frame = Helper.ConvertTimeSecondsToFrame((float)seconds, frameRate);

You then have an accurate way to update your animation to the correct frame.

Also a good idea when you need to sync a video to something is for the video to be encoded in a way that is very fast to seek randomly. For this you need a video with lots of key-frames. Ideally each frame should be a key-frame. See the PDF documentation where we have a whole section on seeking and key-frames.

@jsd-within
Copy link

The technique works perfectly on Windows with GetTextureTimeStamp(). Unfortunately, on Android the results remain too inconsistent to be useable.

Part of the problem is that on Android the timestamp does not start at 0 for frame 0. Instead it seems to be time of day or some such. I'm not choosing the exactly correct moment to store the timestamp. It's not entirely clear how to go about it; I tried storing the first reported number above zero, but that was off by 35-37 frames. Then I tried storing it based on when GetCurrentTimeMs() is zero, which ends up being off by 0-2 frames. I also tried using MediaPlayerEvent.EventType.FirstFrameReady to set the initial timestep, which again was off by 35-37 frames. And that range changes as the video plays so I go out of sync. Generally it does not seem to be more reliable timing than just using GetCurrentTimeMs().

On Android you can change the video API, but results were the same with MediaPlayer and ExoPlayer, except that with MediaPlayer the 35-37 frame gap became 4-5 frames.

@stale
Copy link

stale bot commented Aug 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Stale label Aug 26, 2020
@stale
Copy link

stale bot commented Sep 2, 2020

This issue has been automatically closed because it has not had recent activity. If you wish to continue this issue, then please create a new issue as we do not monitor closed issues.

@stale stale bot closed this as completed Sep 2, 2020
@AndrewRH AndrewRH removed the stale Stale label Sep 8, 2020
@AndrewRH AndrewRH reopened this Sep 8, 2020
@jsd-within
Copy link

I found a workaround by storing frame information in the video and reading it out of a shader, but it's inconvenient and leaves artifacts in the video. So I'm hoping the new Android version will work better for this use case.

@AndrewRH
Copy link
Contributor

AndrewRH commented Dec 8, 2021

In version 2.x we have improved the time precision and frames have an accurate timestamp. There is always latency though when asking for a video frame, so to keep them in sync you could either buffer decoded frames and then select the one with the matching timestamp, or you could wait/block while the decoder is processing the requested frame and poll for it in a loop with some sane timeout value.

Closing this as there's nothing we can do to improve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Android platform enhancement 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants