Skip to main content
Known Participant
October 10, 2023
Question

PS script – Render Video

  • October 10, 2023
  • 0 replies
  • 416 views

Hello, I have created a script that replaces graphics in the mockup, downloaded one by one from a given folder, and renders the video by saving it to an .mp4 file. I can't handle just one thing. Namely, to add a sequence of numbers from the lowest to the beginning of the name of the exported files: 01, 02, 03, etc. Separated from the rest of the name by the "_" character.

 

Could someone help improve this script?

 

// Photoshop Script

// Description: This script automates the process of embedding .png files from the "01_InputFiles" folder into a PSD file and then exports the video.

// Capture the start time
var startTime = new Date();

// 1. Check the folder called "01_InputFiles" which is saved in the root of the active psd file.
var psdPath = app.activeDocument.path;
var inputFilesFolder = new Folder(psdPath + "/01_InputFiles");
var pngFiles = inputFilesFolder.getFiles(function(file) {
    return (file instanceof File && file.name.match(/\.(png)$/i));
});

var shouldContinue = true; // Flag to determine if the script should continue processing
var videoCounter = 1; // Counter for video numbering

// Custom function to pad number with leading zeros
function padNumber(num, size) {
    var s = "00" + num;
    return s.substr(s.length - size);
}

// 2. Check if there are any .png files in the "01_InputFiles" folder.
if (pngFiles.length > 0 && shouldContinue) {
    pngFiles.sort(); // Sort files by name

    // 3. Loop through each .png file in turn.
    for (var i = 0; i < pngFiles.length && shouldContinue; i++) {
        var pngFile = pngFiles[i];

        // 4. Look for a layer named "Nowy Mockup" in groups and subgroups.
        var nowyMockupLayer = findLayerByName(app.activeDocument, "Nowy Mockup");

        // 5. Select the layer.
        app.activeDocument.activeLayer = nowyMockupLayer;

        // 6. Execute the "Edit Contents" function for the Smart Object.
        var idplacedLayerEditContents = stringIDToTypeID("placedLayerEditContents");
        executeAction(idplacedLayerEditContents, undefined, DialogModes.NO);

        // 7. Look for a layer named "1" in groups and subgroups.
        var layerOne = findLayerByName(app.activeDocument, "1");

        // 8. Select the layer.
        app.activeDocument.activeLayer = layerOne;

        // 9. Execute the "Edit Contents" function for the Smart Object.
        executeAction(idplacedLayerEditContents, undefined, DialogModes.NO);

        // 10. Use the "Place Embedded..." function.
        placeFile(pngFile);

        // 11. Transform this layer to 1167.98x1029.83 px.
        var placedLayer = app.activeDocument.activeLayer;
        placedLayer.resize(174.4, 174.4);

        // 12. Use the "Flatten Image" function.
        app.activeDocument.flatten();

        // 13. Save the file and close it.
        app.activeDocument.save();
        app.activeDocument.close();

        // 14. Save and close the next active file.
        app.activeDocument.save();
        app.activeDocument.close();

        // 15. Video Export functionality
        var idexport = stringIDToTypeID("export");
        var desc233 = new ActionDescriptor();
        var idusing = stringIDToTypeID("using");
        var desc234 = new ActionDescriptor();
        var iddirectory = stringIDToTypeID("directory");
        desc234.putPath(iddirectory, new File(psdPath + "/02_Export/"));
        var idameFormatName = stringIDToTypeID("ameFormatName");
        desc234.putString(idameFormatName, """H.264""");
        var idamePresetName = stringIDToTypeID("amePresetName");
        desc234.putString(idamePresetName, """1_High Quality.epr""");
        var idbaseName = stringIDToTypeID("baseName");
        desc234.putString(idbaseName, padNumber(videoCounter, 2) + "_animation"); // Set the filename based on the counter using the custom pad function
        var iduseDocumentSize = stringIDToTypeID("useDocumentSize");
        desc234.putBoolean(iduseDocumentSize, true);
        var iduseDocumentFrameRate = stringIDToTypeID("useDocumentFrameRate");
        desc234.putBoolean(iduseDocumentFrameRate, true);
        var idpixelAspectRatio = stringIDToTypeID("pixelAspectRatio");
        var iddocument = stringIDToTypeID("document");
        desc234.putEnumerated(idpixelAspectRatio, idpixelAspectRatio, iddocument);
        var idfieldOrder = stringIDToTypeID("fieldOrder");
        var idvideoField = stringIDToTypeID("videoField");
        var idpreset = stringIDToTypeID("preset");
        desc234.putEnumerated(idfieldOrder, idvideoField, idpreset);
        var idmanage = stringIDToTypeID("manage");
        desc234.putBoolean(idmanage, true);
        var idselectedFrames = stringIDToTypeID("selectedFrames");
        desc234.putBoolean(idselectedFrames, true);
        var idrenderAlpha = stringIDToTypeID("renderAlpha");
        var idalphaRendering = stringIDToTypeID("alphaRendering");
        var idnone = stringIDToTypeID("none");
        desc234.putEnumerated(idrenderAlpha, idalphaRendering, idnone);
        var idquality = stringIDToTypeID("quality");
        desc234.putInteger(idquality, 1);
        var idZthreeDPrefHighQualityErrorThreshold = stringIDToTypeID("Z3DPrefHighQualityErrorThreshold");
        desc234.putInteger(idZthreeDPrefHighQualityErrorThreshold, 5);
        var idvideoExport = stringIDToTypeID("videoExport");
        desc233.putObject(idusing, idvideoExport, desc234);
        executeAction(idexport, desc233, DialogModes.NO);

        videoCounter++; // Increment the counter for the next video
    }
}

// Helper function to find a layer by its name.
function findLayerByName(doc, name) {
    for (var i = 0; i < doc.layers.length; i++) {
        var layer = doc.layers[i];
        if (layer.name === name) {
            return layer;
        }
        if (layer.typename === "LayerSet") {
            var foundLayer = findLayerByName(layer, name);
            if (foundLayer) return foundLayer;
        }
    }
    return null;
}

// Helper function to place a file using Photoshop's action.
function placeFile(file) {
    var desc = new ActionDescriptor();
    desc.putPath(charIDToTypeID("null"), new File(file));
    executeAction(charIDToTypeID("Plc "), desc, DialogModes.NO);
}

// Capture the end time
var endTime = new Date();

// Calculate the difference in seconds and round to one decimal place
var timeDiff = Math.round(((endTime - startTime) / 1000) * 10) / 10; // in seconds

// Display the time taken
alert("Time taken: " + timeDiff + " seconds");

 

This topic has been closed for replies.