Guest User

Untitled

a guest
Jan 18th, 2024
271
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 1 0
  1. // After Effects Script
  2.  
  3. // After Effects Script
  4.  
  5. // Function to cut the video layer at each marker, within sequence boundaries
  6. function cutVideoLayer(videoLayer, audioMarkers, minDuration) {
  7. app.beginUndoGroup("Cut Video Layer");
  8. var cutLayers = [];
  9.  
  10. var sequenceMarkers = videoLayer.property("Marker"); // Get markers from the video layer
  11. var sequenceStart = 0;
  12. var sequenceEnd = videoLayer.outPoint;
  13.  
  14. for (var s = 1; s <= sequenceMarkers.numKeys; s++) {
  15. // Define sequence boundaries
  16. sequenceStart = sequenceMarkers.keyTime(s);
  17. sequenceEnd = s < sequenceMarkers.numKeys ? sequenceMarkers.keyTime(s + 1) : videoLayer.outPoint;
  18.  
  19. // Cut within sequence boundaries
  20. var prevInPoint = sequenceStart;
  21.  
  22. for (var i = 1; i <= audioMarkers.numKeys; i++) {
  23. var markerTime = audioMarkers.keyTime(i);
  24.  
  25. if (markerTime > sequenceEnd) {
  26. break; // Don't cut beyond the end of the current sequence
  27. }
  28.  
  29. if (markerTime > sequenceStart) {
  30. var duplicateLayer = videoLayer.duplicate();
  31. duplicateLayer.name = videoLayer.name + " Seq " + s + " Cut " + i;
  32. duplicateLayer.inPoint = prevInPoint;
  33. duplicateLayer.outPoint = markerTime;
  34. duplicateLayer.comment = "Rating: "; // Initialize comment
  35.  
  36. cutLayers.push(duplicateLayer);
  37. prevInPoint = markerTime;
  38. }
  39. }
  40.  
  41. // Handle the last segment in the sequence
  42. if (sequenceEnd - prevInPoint >= minDuration) { // Only keep if the last piece is long enough
  43. var lastLayer = videoLayer.duplicate();
  44. lastLayer.name = videoLayer.name + " Seq " + s + " End";
  45. lastLayer.inPoint = prevInPoint;
  46. lastLayer.outPoint = sequenceEnd;
  47. lastLayer.comment = "Rating: "; // Initialize comment
  48.  
  49. cutLayers.push(lastLayer);
  50. }
  51. }
  52.  
  53. // Turn off the original layer since we have duplicated and split it into segments
  54. videoLayer.enabled = false;
  55.  
  56. app.endUndoGroup();
  57. return cutLayers;
  58. }
  59.  
  60. // ... (rest of the script remains unchanged)
  61.  
  62. // Function to shuffle array (Fisher-Yates shuffle algorithm)
  63. function shuffleArray(array) {
  64. for (var i = array.length - 1; i > 0; i--) {
  65. var j = Math.floor(Math.random() * (i + 1));
  66. var temp = array[i];
  67. array[i] = array[j];
  68. array[j] = temp;
  69. }
  70. }
  71.  
  72. // Function to prompt for rating and add it as a comment to the clip
  73. function rateClips(cutLayers) {
  74. app.beginUndoGroup("Rate Clips");
  75.  
  76. // Loop over each cut layer to prompt for a rating
  77. for (var i = 0; i < cutLayers.length; i++) {
  78. var layer = cutLayers[i];
  79. var frameDuration = 1 / comp.frameRate;
  80. var numFramesToShow = 5;
  81.  
  82. // Move the CTI to the start of the clip and then every 5 frames, and pause
  83. for (var t = layer.inPoint; t < layer.outPoint; t += numFramesToShow * frameDuration) {
  84. app.project.activeItem.time = t; // Set the comp time for user to preview the clip
  85. $.sleep(200); // Pause for 200 milliseconds at each step for preview
  86. }
  87.  
  88. // After the preview loop, move the CTI to the middle of the clip for final rating
  89. var cti = (layer.inPoint + layer.outPoint) / 2;
  90. app.project.activeItem.time = cti;
  91.  
  92. var rating = parseInt(prompt("Rate the clip '" + layer.name + "' from 1 to 10:", ""), 10);
  93. layer.comment = "Rating: " + (isNaN(rating) ? 0 : rating);
  94. }
  95.  
  96. app.endUndoGroup();
  97. }
  98.  
  99. function reorderClips(comp) {
  100. app.beginUndoGroup("Reorder Clips");
  101.  
  102. // Create an array to hold layers and their ratings
  103. var layersWithRatings = [];
  104. for (var i = 1; i <= comp.numLayers; i++) {
  105. var layer = comp.layer(i);
  106. if (layer.comment.indexOf("Rating:") !== -1) { // Make sure the layer has a rating comment
  107. var rating = getRatingFromComment(layer);
  108. layersWithRatings.push({ layer: layer, rating: rating });
  109. }
  110. }
  111.  
  112. // Sort the array based on ratings
  113. layersWithRatings.sort(compareRatings);
  114.  
  115. // Position clips next to each other based on the sorted order
  116. var currentTime = 0;
  117. for (var j = 0; j < layersWithRatings.length; j++) {
  118. var item = layersWithRatings[j];
  119. var layer = item.layer;
  120. layer.startTime = currentTime - layer.inPoint; // Adjust start time based on inPoint
  121. currentTime += layer.outPoint - layer.inPoint; // Update current time
  122. }
  123.  
  124. app.endUndoGroup();
  125. }
  126.  
  127. // Function to extract rating from a clip's comment
  128. function getRatingFromComment(clip) {
  129. var ratingMatch = clip.comment.match(/Rating: (\d+)/);
  130. return ratingMatch ? parseInt(ratingMatch[1], 10) : 0;
  131. }
  132.  
  133. // Function to compare ratings from comments for sorting
  134. function compareRatings(a, b) {
  135. return b.rating - a.rating;
  136. }
  137.  
  138. // Main script execution
  139. var comp = app.project.activeItem;
  140. if (comp && comp instanceof CompItem) {
  141. var videoLayer = comp.layer(1); // Assuming the video layer is the first layer
  142. var audioLayer = comp.layer(2); // Assuming the audio layer is the second layer
  143. var audioMarkers = audioLayer.property("Marker");
  144.  
  145. // Calculate the minimum duration between two audio markers
  146. var minDuration = audioMarkers.numKeys >= 2 ?
  147. audioMarkers.keyTime(2) - audioMarkers.keyTime(1) :
  148. comp.duration; // Default to comp duration if not enough markers
  149.  
  150. // Define a tolerance for the minimum duration (for example, 95% of the calculated minDuration)
  151. var tolerance = 0.05 * minDuration;
  152. var minDurationWithTolerance = minDuration - tolerance;
  153.  
  154. var cutLayers = cutVideoLayer(videoLayer, audioMarkers, minDurationWithTolerance);
  155.  
  156. // Remove cut layers shorter than the duration between two audio markers with tolerance
  157. for (var i = cutLayers.length - 1; i >= 0; i--) {
  158. var layer = cutLayers[i];
  159. var duration = layer.outPoint - layer.inPoint;
  160. // If the layer is too short, even after considering the tolerance, delete it
  161. if (duration < minDurationWithTolerance) {
  162. layer.remove();
  163. cutLayers.splice(i, 1); // Remove the layer from the array
  164. }
  165. }
  166.  
  167. // Proceed with rating and reordering the remaining layers
  168. rateClips(cutLayers);
  169. reorderClips(comp, cutLayers);
  170. } else {
  171. alert("Please select a composition with a video and an audio layer.");
  172. }
  173.  
  174.  
  175.  
Advertisement
Add Comment
Please, Sign In to add comment