Skip to main content
.Ava.
Known Participant
June 30, 2023
Answered

How can I find the lightest and darkest frame of a video?

  • June 30, 2023
  • 1 reply
  • 1506 views

I've been trying to create a script with ChatGPT and it's very buggy. What I've tried to do is render a video in black and white, at 1/4 resolution, cropped so that it's just the center, and only rendering 1 frame every 300 frames, in order for me to get a rapid render so I can roughly know which part of the video is the darkest.

 

Not only have I not been able to get that script to work the way I want it to, but I have no clue how to get it to scan the video for the lightest and darkest frame after render.

 

Any ideas?

This topic has been closed for replies.
Correct answer Dan Ebberts

Something like this, basically:

function getLightestAndDarkest(){
	var fO = 300; // frame offset
	var sR = 25; // sample radius
	var comp = app.project.activeItem;
	if (comp && comp instanceof CompItem){
		var layer = comp.layer(1);
		var pt = [layer.width/2,layer.height/2];
		var lightExpr = "s = sampleImage([" + pt[0] + "," + pt[1] + "],[" + sR + "," + sR + "],false,time);\rrgbToHsl(s)[2];";
		var slider = layer.property("Effects").addProperty("ADBE Slider Control");
		slider.property("ADBE Slider Control-0001").expression = lightExpr;
		var sliderMin = 1;
		var sliderMax = 0;
		var frameMin = 0;
		var frameMax = 0;
		var f = 0;
		var t = 0;
		var curVal;
		while (t < comp.duration){
			curVal = slider.property("ADBE Slider Control-0001").valueAtTime(t,false);
			if (curVal > sliderMax){
				sliderMax = curVal;
				frameMax = f;
			}
			if (curVal < sliderMin){
				sliderMin = curVal;
				frameMin = f;
			}
			f += fO;
			t = f*comp.frameDuration;
		}
		slider.remove();
		alert ("Lightest frame = " + frameMax + "\nDarkest frame = " + frameMin);
	}else{
		alert("No comp active.");	
	}
}
getLightestAndDarkest();

1 reply

Dan Ebberts
Community Expert
Community Expert
June 30, 2023

Scripting doesn't have the tools to evaluate luminance like that. Your script would have to apply a sampleImage() expression and harvest the result. And if you're going to do that, there's no need to render anything out--you could just put your comp in another comp and apply sampleImage() to the comp layer and harvest the result at whatever interval you want.

.Ava.
.Ava.Author
Known Participant
July 1, 2023

I'm new to After Effects. How would I go about doing that?

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
July 1, 2023

Something like this, basically:

function getLightestAndDarkest(){
	var fO = 300; // frame offset
	var sR = 25; // sample radius
	var comp = app.project.activeItem;
	if (comp && comp instanceof CompItem){
		var layer = comp.layer(1);
		var pt = [layer.width/2,layer.height/2];
		var lightExpr = "s = sampleImage([" + pt[0] + "," + pt[1] + "],[" + sR + "," + sR + "],false,time);\rrgbToHsl(s)[2];";
		var slider = layer.property("Effects").addProperty("ADBE Slider Control");
		slider.property("ADBE Slider Control-0001").expression = lightExpr;
		var sliderMin = 1;
		var sliderMax = 0;
		var frameMin = 0;
		var frameMax = 0;
		var f = 0;
		var t = 0;
		var curVal;
		while (t < comp.duration){
			curVal = slider.property("ADBE Slider Control-0001").valueAtTime(t,false);
			if (curVal > sliderMax){
				sliderMax = curVal;
				frameMax = f;
			}
			if (curVal < sliderMin){
				sliderMin = curVal;
				frameMin = f;
			}
			f += fO;
			t = f*comp.frameDuration;
		}
		slider.remove();
		alert ("Lightest frame = " + frameMax + "\nDarkest frame = " + frameMin);
	}else{
		alert("No comp active.");	
	}
}
getLightestAndDarkest();