Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // After Effects Script
- // After Effects Script
- // Function to cut the video layer at each marker, within sequence boundaries
- function cutVideoLayer(videoLayer, audioMarkers, minDuration) {
- app.beginUndoGroup("Cut Video Layer");
- var cutLayers = [];
- var sequenceMarkers = videoLayer.property("Marker"); // Get markers from the video layer
- var sequenceStart = 0;
- var sequenceEnd = videoLayer.outPoint;
- for (var s = 1; s <= sequenceMarkers.numKeys; s++) {
- // Define sequence boundaries
- sequenceStart = sequenceMarkers.keyTime(s);
- sequenceEnd = s < sequenceMarkers.numKeys ? sequenceMarkers.keyTime(s + 1) : videoLayer.outPoint;
- // Cut within sequence boundaries
- var prevInPoint = sequenceStart;
- for (var i = 1; i <= audioMarkers.numKeys; i++) {
- var markerTime = audioMarkers.keyTime(i);
- if (markerTime > sequenceEnd) {
- break; // Don't cut beyond the end of the current sequence
- }
- if (markerTime > sequenceStart) {
- var duplicateLayer = videoLayer.duplicate();
- duplicateLayer.name = videoLayer.name + " Seq " + s + " Cut " + i;
- duplicateLayer.inPoint = prevInPoint;
- duplicateLayer.outPoint = markerTime;
- duplicateLayer.comment = "Rating: "; // Initialize comment
- cutLayers.push(duplicateLayer);
- prevInPoint = markerTime;
- }
- }
- // Handle the last segment in the sequence
- if (sequenceEnd - prevInPoint >= minDuration) { // Only keep if the last piece is long enough
- var lastLayer = videoLayer.duplicate();
- lastLayer.name = videoLayer.name + " Seq " + s + " End";
- lastLayer.inPoint = prevInPoint;
- lastLayer.outPoint = sequenceEnd;
- lastLayer.comment = "Rating: "; // Initialize comment
- cutLayers.push(lastLayer);
- }
- }
- // Turn off the original layer since we have duplicated and split it into segments
- videoLayer.enabled = false;
- app.endUndoGroup();
- return cutLayers;
- }
- // ... (rest of the script remains unchanged)
- // Function to shuffle array (Fisher-Yates shuffle algorithm)
- function shuffleArray(array) {
- for (var i = array.length - 1; i > 0; i--) {
- var j = Math.floor(Math.random() * (i + 1));
- var temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- // Function to prompt for rating and add it as a comment to the clip
- function rateClips(cutLayers) {
- app.beginUndoGroup("Rate Clips");
- // Loop over each cut layer to prompt for a rating
- for (var i = 0; i < cutLayers.length; i++) {
- var layer = cutLayers[i];
- var frameDuration = 1 / comp.frameRate;
- var numFramesToShow = 5;
- // Move the CTI to the start of the clip and then every 5 frames, and pause
- for (var t = layer.inPoint; t < layer.outPoint; t += numFramesToShow * frameDuration) {
- app.project.activeItem.time = t; // Set the comp time for user to preview the clip
- $.sleep(200); // Pause for 200 milliseconds at each step for preview
- }
- // After the preview loop, move the CTI to the middle of the clip for final rating
- var cti = (layer.inPoint + layer.outPoint) / 2;
- app.project.activeItem.time = cti;
- var rating = parseInt(prompt("Rate the clip '" + layer.name + "' from 1 to 10:", ""), 10);
- layer.comment = "Rating: " + (isNaN(rating) ? 0 : rating);
- }
- app.endUndoGroup();
- }
- function reorderClips(comp) {
- app.beginUndoGroup("Reorder Clips");
- // Create an array to hold layers and their ratings
- var layersWithRatings = [];
- for (var i = 1; i <= comp.numLayers; i++) {
- var layer = comp.layer(i);
- if (layer.comment.indexOf("Rating:") !== -1) { // Make sure the layer has a rating comment
- var rating = getRatingFromComment(layer);
- layersWithRatings.push({ layer: layer, rating: rating });
- }
- }
- // Sort the array based on ratings
- layersWithRatings.sort(compareRatings);
- // Position clips next to each other based on the sorted order
- var currentTime = 0;
- for (var j = 0; j < layersWithRatings.length; j++) {
- var item = layersWithRatings[j];
- var layer = item.layer;
- layer.startTime = currentTime - layer.inPoint; // Adjust start time based on inPoint
- currentTime += layer.outPoint - layer.inPoint; // Update current time
- }
- app.endUndoGroup();
- }
- // Function to extract rating from a clip's comment
- function getRatingFromComment(clip) {
- var ratingMatch = clip.comment.match(/Rating: (\d+)/);
- return ratingMatch ? parseInt(ratingMatch[1], 10) : 0;
- }
- // Function to compare ratings from comments for sorting
- function compareRatings(a, b) {
- return b.rating - a.rating;
- }
- // Main script execution
- var comp = app.project.activeItem;
- if (comp && comp instanceof CompItem) {
- var videoLayer = comp.layer(1); // Assuming the video layer is the first layer
- var audioLayer = comp.layer(2); // Assuming the audio layer is the second layer
- var audioMarkers = audioLayer.property("Marker");
- // Calculate the minimum duration between two audio markers
- var minDuration = audioMarkers.numKeys >= 2 ?
- audioMarkers.keyTime(2) - audioMarkers.keyTime(1) :
- comp.duration; // Default to comp duration if not enough markers
- // Define a tolerance for the minimum duration (for example, 95% of the calculated minDuration)
- var tolerance = 0.05 * minDuration;
- var minDurationWithTolerance = minDuration - tolerance;
- var cutLayers = cutVideoLayer(videoLayer, audioMarkers, minDurationWithTolerance);
- // Remove cut layers shorter than the duration between two audio markers with tolerance
- for (var i = cutLayers.length - 1; i >= 0; i--) {
- var layer = cutLayers[i];
- var duration = layer.outPoint - layer.inPoint;
- // If the layer is too short, even after considering the tolerance, delete it
- if (duration < minDurationWithTolerance) {
- layer.remove();
- cutLayers.splice(i, 1); // Remove the layer from the array
- }
- }
- // Proceed with rating and reordering the remaining layers
- rateClips(cutLayers);
- reorderClips(comp, cutLayers);
- } else {
- alert("Please select a composition with a video and an audio layer.");
- }
Advertisement
Add Comment
Please, Sign In to add comment