Certainly! Let's address each of your issues one by one and modify your script accordingly.
1. Font Not Correctly Applied:
The font isn't being correctly set because After Effects requires the exact PostScript name of the font when setting it via scripting. The display name often differs from the PostScript name. You mentioned that the system name of the font is "MxPlus_IBM_XGA-AI_12x20". Let's use that as the font name in the script.
Modification:
Change the line where you set the font name in your script from:
textDocument.font = "MxPlus IBM XGA-AI 12x20"; // Ensure this font is installed
to:
textDocument.font = "MxPlus_IBM_XGA-AI_12x20"; // Use the PostScript name of the font
Note: If you're unsure about the exact PostScript name of the font, you can retrieve it by creating a text layer in After Effects manually with that font and then running a script to alert the font's PostScript name:
var selectedLayers = app.project.activeItem.selectedLayers;
if (selectedLayers.length > 0 && selectedLayers[0] instanceof TextLayer) {
var textProp = selectedLayers[0].property("Source Text");
var textDocument = textProp.value;
alert("The PostScript name of the font is: " + textDocument.font);
} else {
alert("Please select a text layer.");
}
Run this script while a text layer with your desired font is selected to get the correct font name.
2. Subtitle Comps Not Placed Correctly on the Timeline:
The issue arises from how inPoint and outPoint are being set. In After Effects, a layer's inPoint and outPoint are relative to the layer's start time, not the composition's time. Additionally, when you set inPoint and outPoint, you're effectively trimming the layer, which may not be necessary here.
Modification:
You should only set the startTime of the layer, and let the inPoint and outPoint default to the source's duration.
Change this:
compLayer.startTime = entry.startTime;
compLayer.inPoint = 0;
compLayer.outPoint = entry.duration;
To this:
compLayer.startTime = entry.startTime;
By setting only the startTime, the layer will automatically appear on the timeline at the correct time, and its duration will match the source's duration (which you've already set to entry.duration when creating subtitleComp).
3. Finding a Precomp Named "WHITEBACKGROUND" Instead of a Solid:
Your findFootageByName function is currently set up to find a FootageItem, but you need it to find a CompItem, which represents a composition (precomp) in After Effects.
Modification:
First, rename the function for clarity, and then adjust it to search for CompItem instead of FootageItem.
Change the function definition and implementation to:
// Find a composition item by name in the project panel
function findCompByName(name) {
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem && app.project.item(i).name === name) {
return app.project.item(i);
}
}
return null;
}
Update the code where you call findFootageByName:
// Locate the existing "WHITEBACKGROUND" composition in the project
var whiteBG = findCompByName("WHITEBACKGROUND");
if (whiteBG == null) {
alert('The "WHITEBACKGROUND" composition was not found in the project.');
}
Replace all references to findFootageByName with findCompByName.
Also, ensure when you add the background layer to the subtitle composition, you add it correctly:
// Add the "WHITEBACKGROUND" precomp beneath the text layer
if (whiteBG != null) {
var bgLayer = subtitleComp.layers.add(whiteBG);
bgLayer.moveBefore(textLayer);
}
Here is the modified script with these changes applied:
// Polyfill for String.trim() in ExtendScript
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
// ExtendScript for After Effects
// Begin undo group for script actions
app.beginUndoGroup("Create SubRip Comps");
// ... rest of your script ...
// Prompt user to select a SubRip (.srt) file
var srtFile = File.openDialog("Select a SubRip (.srt) file");
if (srtFile != null) {
srtFile.open("r");
var srtContent = srtFile.read();
srtFile.close();
// Parse the SubRip file content
var entries = parseSRT(srtContent);
if (entries.length > 0) {
// Create a main composition to hold all subtitle comps
var mainCompName = "Subtitles_Comp";
var mainCompDuration = entries[entries.length - 1].endTime;
var mainCompWidth = 1920;
var mainCompHeight = 1080;
var mainCompPixelAspect = 1.0;
var mainCompFrameRate = 30; // Adjust as needed
var mainComp = app.project.items.addComp(
mainCompName,
mainCompWidth,
mainCompHeight,
mainCompPixelAspect,
mainCompDuration,
mainCompFrameRate
);
// Locate the existing "WHITEBACKGROUND" composition in the project
var whiteBG = findCompByName("WHITEBACKGROUND");
if (whiteBG == null) {
alert('The "WHITEBACKGROUND" composition was not found in the project.');
}
// Process each subtitle entry
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
// Remove "{txt}" from the text for naming
var cleanText = entry.text.replace("{txt}", "").trim();
// Create a new composition for each subtitle
var subtitleComp = app.project.items.addComp(
cleanText,
mainCompWidth,
mainCompHeight,
mainCompPixelAspect,
entry.duration,
mainCompFrameRate
);
// Add the text layer
var textLayer = subtitleComp.layers.addText(cleanText);
var textProp = textLayer.property("Source Text");
var textDocument = textProp.value;
// Check for "{txt}" to set specific text properties
if (entry.text.indexOf("{txt}") !== -1) {
textDocument.font = "MxPlus_IBM_XGA-AI_12x20"; // Use the correct font name
textDocument.fontSize = 45;
textDocument.leading = 54;
textDocument.fillColor = hexToRgb("#24254D");
textDocument.tracking = 0;
textDocument.justification = ParagraphJustification.LEFT_JUSTIFY;
}
textProp.setValue(textDocument);
textLayer.property("Position").setValue([623, 706]);
// Add the "WHITEBACKGROUND" precomp beneath the text layer
if (whiteBG != null) {
var bgLayer = subtitleComp.layers.add(whiteBG);
bgLayer.moveBefore(textLayer);
}
// Place the subtitle comp into the main comp at the correct time
var compLayer = mainComp.layers.add(subtitleComp);
compLayer.startTime = entry.startTime;
// No need to set inPoint and outPoint
}
} else {
alert("No valid entries found in the SubRip file.");
}
}
// End undo group
app.endUndoGroup();
// Function to parse SRT content into entries
function parseSRT(data) {
var regex = /(\d+)\s+([\d:,]+)\s+-->\s+([\d:,]+)\s+([\s\S]*?)(?=\n{2,}|$)/g;
var entries = [];
var match;
while ((match = regex.exec(data)) !== null) {
var index = parseInt(match[1], 10);
var startTime = timeToSeconds(match[2]);
var endTime = timeToSeconds(match[3]);
var text = match[4].replace(/\r\n|\r|\n/g, "\n").trim();
entries.push({
index: index,
startTime: startTime,
endTime: endTime,
duration: endTime - startTime,
text: text
});
}
return entries;
}
// Convert time in "HH:MM:SS,ms" to seconds
function timeToSeconds(t) {
var parts = t.split(',');
var timeParts = parts[0].split(':');
var hours = parseFloat(timeParts[0]);
var minutes = parseFloat(timeParts[1]);
var seconds = parseFloat(timeParts[2]);
var milliseconds = parseFloat(parts[1]) / 1000;
return hours * 3600 + minutes * 60 + seconds + milliseconds;
}
// Convert hex color to RGB array (values between 0 and 1)
function hexToRgb(hex) {
var bigint = parseInt(hex.replace("#", ""), 16);
var r = ((bigint >> 16) & 255) / 255;
var g = ((bigint >> 8) & 255) / 255;
var b = (bigint & 255) / 255;
return [r, g, b];
}
// Find a composition item by name in the project panel
function findCompByName(name) {
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem && app.project.item(i).name === name) {
return app.project.item(i);
}
}
return null;
}
Summary of Changes:
- Font Fix: Changed the font name to
"MxPlus_IBM_XGA-AI_12x20" to use the correct PostScript name.
- Timeline Placement Fix: Removed unnecessary
inPoint and outPoint settings, leaving only compLayer.startTime = entry.startTime;.
- WHITEBACKGROUND Precomp Fix:
- Renamed
findFootageByName to findCompByName.
- Modified
findCompByName to search for CompItem instead of FootageItem.
- Adjusted references to use the updated function.
Additional Tips:
- Validating Font Names:
- Always confirm the exact font name by retrieving the
font property from an existing text layer.
- Layer Timing:
- Remember that
startTime positions the layer in the composition timeline.
inPoint and outPoint trim the layer's content and are relative to the layer's own time, starting from 0.
- Debugging:
- If something isn't working as expected, try isolating that part of the code and test it separately.
- Use
alert() statements or write to the console to check variable values at different stages.
With these changes, your script should now correctly:
- Apply the specified font to the text layers.
- Place the subtitle compositions on the main composition's timeline at the correct times based on the SRT file.
- Use the "WHITEBACKGROUND" precomp in each subtitle composition.
Let me know if you need further assistance!