Automating ADOBE AE
Added 2023-06-01 13:25:40 +0000 UTCCode is a script written in Adobe After Effects that performs random audio randomization based on the brightness values of a video layer. Here's a breakdown of what the code does:
- It starts by checking if a composition is selected in Adobe After Effects.
- If a composition is selected, it creates a new composition named "Final Video" with the same dimensions, pixel aspect ratio, duration, and frame rate as the selected composition.
- It retrieves the video layer and audio layer from the selected composition.
- The audio layer is duplicated and its starting time is set to 0.
- A null layer named "Audio Control" is created in the final composition to control the audio levels.
- The duplicated audio layer is parented to the audio control layer.
- The code then calls the "getBrightnessValues" function to obtain an array of brightness values for each frame of the video layer.
- It iterates over each layer in the selected composition and checks if it is a footage layer with audio level control.
- For each valid layer, it loops through each frame and sets the audio level based on the corresponding brightness value.
- The output file path is set to the user's desktop with the name "FinalVideo.mp4".
- The final composition is exported using Adobe Media Encoder with default settings.
- If no composition is selected, an alert is shown to the user requesting them to select a composition.
The "getBrightnessValues" function is responsible for extracting the brightness values of each frame of a video layer. It does so by creating a temporary solid layer, setting its transfer mode to "Difference," applying the current frame of the video layer to the solid layer, applying the "Brightness & Contrast" effect to the solid layer, and retrieving the brightness value from the effect. The brightness values are stored in an array and returned by the function.
In summary, this script randomizes the audio levels of footage layers in a selected composition based on the brightness values of a video layer, and then exports the modified composition as a video file.
// Random Audio Randomization Script for Adobe After Effects
// Get the selected composition
var composition = app.project.activeItem;
// Check if a composition is selected
if (composition && composition instanceof CompItem) {
// Create a new comp for the final video
var finalComp = app.project.items.addComp("Final Video", composition.width, composition.height, composition.pixelAspect, composition.duration, composition.frameRate);
// Get the video layer from the composition
var videoLayer = composition.layer(1); // Assuming the video layer is the first layer
// Get the audio layer from the composition
var audioLayer = composition.layer(2); // Assuming the audio layer is the second layer
// Duplicate the audio layer
var duplicatedAudioLayer = audioLayer.duplicate();
// Set the starting time of the duplicated audio layer
duplicatedAudioLayer.startTime = 0;
// Create a null layer for controlling the audio level
var audioControlLayer = finalComp.layers.addNull();
audioControlLayer.name = "Audio Control";
// Connect the duplicated audio layer to the audio control layer
duplicatedAudioLayer.parent = audioControlLayer;
// Get the brightness values for each frame
var brightnessValues = getBrightnessValues(videoLayer);
// Set the random audio levels based on brightness
for (var i = 1; i <= composition.numLayers; i++) {
var layer = composition.layer(i);
// Check if the layer is a footage layer
if (layer instanceof AVLayer && layer.canSetAudioLevels) {
for (var j = 1; j <= layer.source.duration * composition.frameRate; j++) {
var brightness = brightnessValues[j - 1];
// Set the audio level based on brightness
var randomAudioLevel = brightness * 100; // Multiply brightness by 100 to get a value between 0 and 100
layer.audio.audioLevels.setValueAtTime(j / composition.frameRate, [randomAudioLevel, randomAudioLevel]);
}
}
}
// Set the output file path
var outputPath = Folder.desktop.fsName + "/FinalVideo.mp4";
// Export the final composition with default settings
finalComp.exportFile(outputPath, "Adobe Media Encoder");
alert("Video exported successfully!");
} else {
alert("Please select a composition.");
}
// Function to get the brightness values for each frame of a video layer
function getBrightnessValues(layer) {
var brightnessValues = [];
var frameDuration = 1 / layer.source.frameRate;
// Iterate through each frame of the layer
for (var i = 1; i <= layer.source.duration * layer.source.frameRate; i++) {
// Set the time of the layer to the current frame
layer.time = (i - 1) * frameDuration;
// Create a temporary solid layer to extract the brightness information
var tempSolidLayer = layer.containingComp.layers.addSolid([1, 1, 1], "Temp Solid", layer.width, layer.height, layer.pixelAspect, layer.source.duration);
// Set the transfer mode to "Difference"
tempSolidLayer.blendingMode = BlendingMode.DIFFERENCE;
// Apply the current frame of the layer to the solid layer
tempSolidLayer.source = layer.source;
// Apply the "Brightness & Contrast" effect to the solid layer
var brightnessContrastEffect = tempSolidLayer.Effects.addProperty("ADBE Brightness & Contrast");
brightnessContrastEffect.property("Brightness").setValue(0);
brightnessContrastEffect.property("Contrast").setValue(0);
// Get the brightness value of the solid layer
var brightnessValue = brightnessContrastEffect.property("Brightness").value;
// Store the brightness value in the array
brightnessValues.push(brightnessValue);
// Remove the temporary solid layer
tempSolidLayer.remove();
}
return brightnessValues;
}