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

Fix VOD issues with longer keyframe intervals #3671

Conversation

uvjustin
Copy link
Contributor

When using keyframe intervals of a few seconds or longer, and especially when using the H264+/H265+ codec variants with very long keyframe intervals, the VOD segmentation breaks. There are 2 main issues:

  1. The start of the event generally does not line up with a keyframe. The Kaltura VOD module starts from the next keyframe, effectively shortening the clip, and making the duration calculation incorrect. This can be seen when comparing the VOD clip length (from vod_ts()) with the downloaded clip length (from recording_clip())
  2. When combining multiple clips, setting vod_manifest_segment_durations_mode to accurate does not work. This results in the manifest not matching the media. Also, the segmenting can often result in segments with no keyframes. These two problems will generally cause playback to fail.

This PR uses ffprobe to get the keyframe durations, fixing 1) by adjusting the offset to the previous keyframe and 2) by using the keyFrameDurations field to give the VOD module enough information to create working manifests/segments.

frigate/http.py Outdated Show resolved Hide resolved
@uvjustin uvjustin force-pushed the fix-vod-longer-keyframe-intervals branch from 1b140b3 to 6168884 Compare August 18, 2022 13:59
@uvjustin uvjustin marked this pull request as draft August 19, 2022 04:25
@uvjustin uvjustin marked this pull request as ready for review August 19, 2022 05:29
@blakeblackshear
Copy link
Owner

I think calling ffprobe for every segment on each api call is going to introduce too much latency.

Given that ffmpeg is writing the segments to mp4 files itself, don't we know that each segment must start with a keyframe? If so, can we just pull back the start time ahead of the current segment to ensure the first keyframe is prior to the start of the event?

This makes me want to drop the VOD module even more.

@uvjustin
Copy link
Contributor Author

ffprobe on keyframes only should be pretty quick - not more than a few seconds for an hour of video. But yeah, I think we should try to avoid it actually. When I did this for some reason I thought the recordings were stored in longer files - I didn't realize they were segmented into ~ 10s clips. In that case probing is pretty unnecessary as most of these segments will actually just have 1 keyframe (when using the long I-frame intervals). We still need to pass some keyframe durations to the VOD module to have the HLS segmented properly, but trivially using the recording duration may work fine.
The ffprobe should still probably be retained for the first segment, as this would still allow clips with short I-frame intervals to be clipped properly.
Let me redo this and test locally.

@uvjustin uvjustin marked this pull request as draft August 24, 2022 13:09
@uvjustin uvjustin force-pushed the fix-vod-longer-keyframe-intervals branch from 737ace4 to 314e05b Compare August 24, 2022 15:41
@uvjustin uvjustin marked this pull request as ready for review August 24, 2022 15:41
@uvjustin
Copy link
Contributor Author

Just passing the recording duration as a single keyFrameDuration seems to work. However, I don't have any cameras set up with I-frame intervals less than 4 seconds - would be good if someone else could test to make sure that things still work fine with those camera settings.

@uvjustin uvjustin requested a review from NickM-27 August 24, 2022 15:46
@NickM-27
Copy link
Sponsor Collaborator

I tested on one of my cams that has keyframes every 2 seconds:

0.000000
1.999414
3.997982
5.997201
8.019857

seems to continue working fine in that case as well.

@blakeblackshear
Copy link
Owner

I would still prefer to avoid the call to ffprobe altogether. For now, we should just assume that the keyframe is at the beginning of the first segment and seek from there. The vast majority of users won't benefit from VOD cutting that segment into smaller parts. In the future, we should piggyback on the existing ffprobe call to check the duration of the segment here and add this to the database.

@uvjustin
Copy link
Contributor Author

uvjustin commented Aug 25, 2022

Sure, I can remove it. To be clear, this means not attempting to clip the first clip which actually affects cameras with short I-frame intervals the most - they may get an extra few seconds at the beginning of their clips.
I was coming across some interesting behavior with one particular clip where there was brief freezing between segments, and oddly this didn't happen if a few seconds was cut off the front of the clip. But I think this is an unrelated problem (one of the VOD module "features" it would seem). Edit: #3710 should fix that issue.

@blakeblackshear blakeblackshear merged commit 9df415b into blakeblackshear:release-0.11.0 Aug 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants