mpv icon indicating copy to clipboard operation
mpv copied to clipboard

loop-file: add seamless-looping option to ensure that there are no delayed frames at beginning of each loop

Open boxerab opened this issue 2 years ago • 4 comments

With these changes :

  1. lavf demuxer performs seek to 0 when encountering EOF and does not signal EOF to rest of library
  2. with these changes, there is a single frame at beginning of each loop with invalid frame time (equal to zero). When this frame is encountered, frame time is set to inverse of container fps
  3. tested with 30fps and 60 fps content - no delays detected at beginning of each loop

It can be difficult to detect the artifact this PR is designed to fix. The attached file, an MOV of moving text at 30fps, should reveal the problem.

To enable seamless looping, use --loop and --seamless-looping command line args.

https://user-images.githubusercontent.com/95393/195938081-e76a6126-be6a-454e-9e4a-c25e9f9dfedf.mov

boxerab avatar Oct 14 '22 20:10 boxerab

@Dudemanguy what are your thoughts on this PR ? small optional changes that fix a real problem with --loop functionality.

boxerab avatar Oct 27 '22 19:10 boxerab

Conceptually it seems fine, but I (or someone else) would still need to actually verify the demux internals and all that.

Dudemanguy avatar Oct 28 '22 02:10 Dudemanguy

Why would we not do this all the time and not bother adding a new option?

philipl avatar Nov 07 '22 22:11 philipl

Why would we not do this all the time and not bother adding a new option?

Good question - as the current looping functionality is sub-optimal, we could do this. As long as there are no other ramifications for this change.

boxerab avatar Nov 07 '22 23:11 boxerab

any updates on this PR? pretty excited about this one, hopefully it will get passed!

Edit: I have not tested this tho

NaiveInvestigator avatar Jan 20 '23 12:01 NaiveInvestigator

I've simplified this PR, and it seems to work quite well. The key change is to suppress EOF and pretend that packet PTS and DTS increase indefinitely.

@Dudemanguy @philipl

boxerab avatar Feb 03 '23 18:02 boxerab

Any updates on this PR? Perfect loop with no delay is really useful.

zhahouming avatar Apr 20 '23 16:04 zhahouming

Any interest in getting this merged ? It does solve a real problem with mpv loop mode....

@Dudemanguy @philipl

boxerab avatar May 05 '23 18:05 boxerab

Can this be merged please @Dudemanguy @philipl

Thanks for your work.

nialljawad96 avatar Jun 28 '23 11:06 nialljawad96

@boxerab I agree that the visual effect is what you want, but the management of timing leads to the report length of the clip to increase continuously, faster than the current time counter. This would be very confusing. Is there anything that can be done about that?

philipl avatar Jun 30 '23 20:06 philipl

@boxerab I agree that the visual effect is what you want, but the management of timing leads to the report length of the clip to increase continuously, faster than the current time counter. This would be very confusing. Is there anything that can be done about that?

Not that I'm aware of.

boxerab avatar Jul 06 '23 00:07 boxerab

Another thing to note is that we have other demuxers - most particularly demux_mkv.

philipl avatar Jul 06 '23 02:07 philipl

It seems like we seek to EOF a bunch of times as part of getting ready to play the clip - at least for an mp4 file - might be due to looking for atoms at the end of the file, so this pushes up the offset multiple times right off the bat. If that could be avoided, it would make the behaviour more intuitive.

philipl avatar Jul 06 '23 02:07 philipl

Yes, only pushing offset when stream is playing would be a good move. I'm afraid I won't have time in foreseeable future to work on this PR, so let's just keep it on the back burner until someone steps up .

boxerab avatar Jul 06 '23 15:07 boxerab

I tried this and still get a black flash between loop iterations, though it seems to sometimes work :slightly_frowning_face:
The black frame is reproducible by pressing . when on the last frame: the player freezes on the black screen.
Also, I can confirm the reporting playback position increases forever, and that is not just a cosmetic issue, it breaks all timing related functionality (seek, etc.), which might've been obvious to those familiar with mpv internals.

Experimenting a bit more, I managed to get the desired effect by manually setting an A/B loop on the first and last frame. I guess this might be doable on the CLI if you figure out the right values for your media.

So I started looking at the code (first time looking at mpv, and even anything video related) to see if I can do that programmatically but didn't manage so yet.
I found the seek_to_last_frame function so IIUC it's not possible to get the PTS (Wikipedia, for noobs like me) of the last frame without actually reading the file up to there, which means it's not possible to do setup an A/B loop in advance.

The current roadblock I'm at is that the handle_loop_file function (which also handles A/B loops) knows the file has ended only after the player has gone past the last frame. Hence the black frame is already displayed. Once I understood that I understood why this PR is implemented directly in the demuxer.
I guess next step is I'll have to read and understand some of the code in video.c to see if the "seek on loop end" logic can be moved there, where it could preemptively ask for another frame.

ThinkChaos avatar Jan 16 '24 04:01 ThinkChaos

I'm glad to report back with a patch, see #13296 :)
It works at a higher level than this PR, so it doesn't have the timeline related issues. I'd appreciate feedback and testing from anyone that's interested!

ThinkChaos avatar Jan 16 '24 22:01 ThinkChaos

Sorry anyone can help me?

  1. How can i use this PR in media kit lib for flutter

I'm using:

media_kit: ^1.1.10 # Primary package. media_kit_video: ^1.2.4 # For video rendering. media_kit_libs_video: ^1.0.4

johndavid92 avatar Jun 14 '24 09:06 johndavid92