feat: Get claude AI to generate and debug FPS measurement functionality

This commit is contained in:
aria 2025-06-14 01:10:00 +10:00
parent f4d7a1a04c
commit 4699841f1e
Signed by: aria
SSH key fingerprint: SHA256:WqtcVnDMrv1lnUlNah5k31iywFUI/DV+5yHzCTO4Vds
2 changed files with 212 additions and 0 deletions

134
main.go
View file

@ -83,6 +83,26 @@ func main() {
return countUniqueVideoFrames(cmd.StringArg("video1"), cmd.StringArg("video2"), 1, false)
},
},
{
Name: "analyze-frame-persistence",
Usage: "Analyze frame persistence in a single video",
Arguments: []cli.Argument{
&cli.StringArg{
Name: "video",
},
},
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "tolerance",
Usage: "Pixel difference tolerance (0-255)",
Value: 0,
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
tolerance := uint64(cmd.Float64("tolerance"))
return analyzeFramePersistence(cmd.StringArg("video"), tolerance)
},
},
},
}
@ -219,3 +239,117 @@ func getImageFromFilePath(filePath string) (image.Image, error) {
image, _, err := image.Decode(f)
return image, err
}
func analyzeFramePersistence(videoPath string, tolerance uint64) error {
video, err := vidio.NewVideo(videoPath)
if err != nil {
return err
}
defer video.Close()
fps := video.FPS()
frameTimeMs := 1000.0 / fps
log.Default().Printf("Video FPS: %.2f, Frame time: %.2f ms", fps, frameTimeMs)
currentFrame := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
previousFrame := image.NewRGBA(image.Rect(0, 0, video.Width(), video.Height()))
video.SetFrameBuffer(currentFrame.Pix)
var frameNumber int
var uniqueFramesPerSecond []int
var framePersistenceDurations []float64
currentSecond := 0
uniqueFramesInCurrentSecond := 0
consecutiveDuplicateCount := 0
hasFirstFrame := false
for video.Read() {
frameNumber++
if !hasFirstFrame {
copy(previousFrame.Pix, currentFrame.Pix)
hasFirstFrame = true
uniqueFramesInCurrentSecond = 1
continue
}
isFrameDifferent := false
pixelDifferences := uint64(0)
for i := 0; i < len(currentFrame.Pix); i++ {
if isDiffUInt8WithTolerance(currentFrame.Pix[i], previousFrame.Pix[i], tolerance) {
pixelDifferences++
}
}
if pixelDifferences > 0 {
isFrameDifferent = true
}
if !isFrameDifferent {
consecutiveDuplicateCount++
} else {
if consecutiveDuplicateCount > 1 {
persistenceMs := float64(consecutiveDuplicateCount+1) * frameTimeMs
framePersistenceDurations = append(framePersistenceDurations, persistenceMs)
log.Default().Printf("Frame persisted for %.2f ms (%d consecutive duplicates)", persistenceMs, consecutiveDuplicateCount)
}
consecutiveDuplicateCount = 0
uniqueFramesInCurrentSecond++
copy(previousFrame.Pix, currentFrame.Pix)
}
newSecond := int(float64(frameNumber-1) / fps)
if newSecond > currentSecond {
uniqueFramesPerSecond = append(uniqueFramesPerSecond, uniqueFramesInCurrentSecond)
log.Default().Printf("Second %d: %d unique frames", currentSecond+1, uniqueFramesInCurrentSecond)
currentSecond = newSecond
uniqueFramesInCurrentSecond = 0
}
}
if consecutiveDuplicateCount > 1 {
persistenceMs := float64(consecutiveDuplicateCount+1) * frameTimeMs
framePersistenceDurations = append(framePersistenceDurations, persistenceMs)
log.Default().Printf("Final frame persisted for %.2f ms (%d consecutive duplicates)", persistenceMs, consecutiveDuplicateCount)
}
if uniqueFramesInCurrentSecond > 0 {
uniqueFramesPerSecond = append(uniqueFramesPerSecond, uniqueFramesInCurrentSecond)
log.Default().Printf("Second %d: %d unique frames", currentSecond+1, uniqueFramesInCurrentSecond)
}
log.Default().Printf("\n=== SUMMARY ===")
log.Default().Printf("Total frames analyzed: %d", frameNumber)
log.Default().Printf("Video duration: %.2f seconds", float64(frameNumber)/fps)
totalUniqueFrames := 0
for i, count := range uniqueFramesPerSecond {
totalUniqueFrames += count
log.Default().Printf("Second %d: %d unique frames", i+1, count)
}
log.Default().Printf("Total unique frames: %d", totalUniqueFrames)
if len(uniqueFramesPerSecond) > 0 {
log.Default().Printf("Average unique frames per second: %.2f", float64(totalUniqueFrames)/float64(len(uniqueFramesPerSecond)))
}
if len(framePersistenceDurations) > 0 {
log.Default().Printf("\nFrame persistence durations:")
totalPersistence := 0.0
for _, duration := range framePersistenceDurations {
totalPersistence += duration
}
avgPersistence := totalPersistence / float64(len(framePersistenceDurations))
log.Default().Printf("Average frame persistence: %.2f ms", avgPersistence)
log.Default().Printf("Number of persistence events: %d", len(framePersistenceDurations))
} else {
log.Default().Printf("No frame persistence detected (all frames are unique)")
}
return nil
}