Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Get dynamically the duration of ProjectItem or TrackItem

New Here ,
Nov 15, 2021 Nov 15, 2021

Copy link to clipboard

Copied

Hey, 

 

i am working with a csv to auto import videos, paste them into a bin, cut them and then send the final sequence to media encoder.

The problem i am facing right now is to get the duration of the imported clips and dynamically put the second video on the end of the first.

I tried this piece of code and sometimes it gives me the duration of the two clips. But sometimes it doesnt even give me any value.

When it had worked, it only reads the value for the first two videos. It alway keeps the value of the first two Videos.

 

Example: 

  • 1.Video 20s
  • 2.Video 30s
  • 3.Video: 10s
  • 4. Video: 15s

 

I always get only the value of 1. and 2.

 

var activeSeq = app.project.activeSequence;
var durationIntro = activeSeq.videoTracks[0].clips[intro].duration.seconds;
var durationThema = activeSeq.videoTracks[0].clips[thema].duration.seconds;

 

This is the CSV:

Chen_intro_1;Thema 1;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Chen_intro_1.mp4;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Thema 1.mp4;
Sassanelli_intro_1;Thema 2;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Sassanelli_intro_2.mp4;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Thema 2.mp4;
Wosilat_intro_1;Thema 3;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Wosilat_intro_1.mp4;/Users/mac_nonprint/Desktop/PR-Automatisierung/Videos/Thema 3.mp4;

 

 

This is my complete Script

var csvFile = "/Users/mac_nonprint/Desktop/PR-Automatisierung/CSV/haefele_video_liste1.csv"; // PROMPT FOR CSV FILE

var importCount = 0;
var subCounter = 0;
var clipCounter = 0;
var bufferTime = 0; // HOW MUCH TIME (IN SECONDS) BETWEEN CLIPS WHEN PLACED IN TIMELINE

var infoArray;


// Following opens the text file and stores it in var CSVFILE. Then splits it by every new line, and COMA into a multi-tiered array, INFOARRAY.
if(csvFile){
    var file = File(csvFile) //OPEN, READ, AND CLOSE THE CSV FILE
    file.open("r");
    var fullText = file.read();
    file.close();
    infoArray = fullText.split("\n"); // SPLIT THE CSV FILE AT EVERY NEW LINE
}
 
if(infoArray[infoArray.length -1] == ""){ //SOMETIMES WHEN SPLITTING UP THE ARRAY, THE PROCESS CREATES AN EXTRA, EMPTY LINE. tHIS WILL JUST TEST AND REMOVE THAT IF IT HAPPENS
    infoArray.splice(infoArray.length-1, 1)    
}


/* ================================================= THEMA A ! ===============================================================*/



// IMPORTARY TO LOAD FILES. THIS IS WHERE YOU ADJUST FILEPATHS IF YOU NEED TO 
var importAry1 = [];

// LOOP THROUGH INFOARRAY

if (infoArray){
    
    for (var i = 0; i < infoArray.length;  i++ ) { // START WITH i AS 1 BECAUSE THE FIRST LINE OF OUR CSV IS HEADERS
        
        infoArray[i] = infoArray[i].split(";");    

        // Sequenz 
        var sequenzName = (importCount + 1 + "-") + infoArray[i][0]  + "_" +  infoArray[i][1];
        var sequenzPreset = "/Users/mac_nonprint/Documents/Adobe/Premiere\ Pro/22.0/Profile-mac_nonprint/Settings/Benutzerdefiniert/Test_Import_Settings.sqpreset";
        app.project.newSequence(sequenzName, sequenzPreset); // CREATE A NEW SEQUENCE
        app.project.rootItem.createBin(sequenzName + " - Clips"); // CREATE BIN FOR ORGANIZATION
        var importBin = findBinIndex(app.project.rootItem,sequenzName + " - Clips");  // STORE THE INDEX PATH TO THAT BIN
        

        // Import Intro & Thema File into importBin
        importAry1[0] = infoArray[i][2];
        app.project.importFiles(importAry1,1,importBin,0);
        importAry1[0] = infoArray[i][3];
        app.project.importFiles(importAry1,1,importBin,0);  
        importCount++;


        var intro = 0;
        var thema = 1;
        
        for (var a = 0; a < importBin.children.numItems; a++ ){ // LOOP THROUGH THE IMPORT BIN
            if( importBin.children[intro].name.indexOf(" - ")==-1){
                importBin.children[intro].name = "1" + " - " + importBin.children[intro].name; // RENAME WITH A THE NUMBER IT WAS IMPORTED, THIS HOLDS THE ORDER OF THE CSV
                infoArray[i][infoArray[i].length] = importBin.children[intro].intro; // STORE THE ITEMS NODEID IN THE CSV ARRAY
            }    
            if( importBin.children[thema].name.indexOf(" - ")==-1){
                importBin.children[thema].name = "2" + " - " + importBin.children[thema].name;
                infoArray[i][infoArray[i].length] = importBin.children[thema].thema;
            }    
        }

        app.project.rootItem.createBin(sequenzName + " - Subclips"); // CREATE BIN FOR ORGANIZATION
        var moveTo = findBinIndex(app.project.rootItem,sequenzName + " - Subclips");

        for( a = 0; a < app.project.sequences.numSequences; a++ ){ // LOOP THROUGH ALL SEQS  
            if(app.project.sequences[a].name==sequenzName){ // FIND OUR CREATED SEQUENCE
                app.project.activeSequence = app.project.sequences[a]; // SET THE SEQUENCE TO BE OUR ACTIVE SEQ
            }
        }



        
        var anzahlClips = importBin.children.length;
        
        // LOOP THROUGH OUR IMPORTED CLIPS TO CREATE THE EDITED SUBCLIPS. 
        for( var r = 0; r < 1; r++ ){
            
            var activeSeq = app.project.activeSequence;
            var durationIntro = activeSeq.videoTracks[0].clips[intro].duration.seconds;
            var durationThema = activeSeq.videoTracks[0].clips[thema].duration.seconds;
    
            // var durationIntro = parseInt(activeSeq.videoTracks[0].clips[intro].duration.seconds);
            // var durationThema = parseInt(activeSeq.videoTracks[0].clips[thema].duration.seconds);

            if(importBin.children[intro]) {
                var currentItem = importBin.children[intro];
                
                if(currentItem.intro == infoArray[ i ][ infoArray[i].length-1 ]) {
     
                    // var durationIntro = parseInt(infoArray[i][4]);
                    // var durationIntro = parseInt(activeSeq.videoTracks[0].clips[intro].duration.seconds);

                    var in1 = 0;
                    var out1 = durationIntro;

                    // GATHER TIMECODE INFORMATION INTO A FLAT SECONDS NUMBER
                    var inPoint = in1; 
                    var outPoint = out1;
                    
                    // CREATE THE SUBCLIPS ( NAME , IN , OUT , BOUNDARIES (BINARY) , TAKE VIDEO , TAKE AUDIO)
                    var newSub = currentItem.createSubClip(currentItem.name, inPoint , outPoint , 0 , 1 , 1 ); 
                    newSub.moveBin(moveTo); // MOVE INTO THE SUBCLIPS BIN
                    placeClip(activeSeq, newSub , bufferTime);
                    // alert('Intro platziert!');
                }
                r++;
            }            
            if(importBin.children[thema]) {
                var currentItem = importBin.children[thema];
                if(currentItem.thema == infoArray[ i ][ infoArray[i].length-1 ]) {

                    // var durationThema = parseInt(infoArray[i][5]);
                    // var durationThema = parseInt(activeSeq.videoTracks[0].clips[thema].duration.seconds);
                    
                    var in2 = 0;
                    var out2 = durationThema;

                    // GATHER TIMECODE INFORMATION INTO A FLAT SECONDS NUMBER
                    var inPoint = in2; 
                    var outPoint = out2;
                    
                    // CREATE THE SUBCLIPS ( NAME , IN , OUT , BOUNDARIES (BINARY) , TAKE VIDEO , TAKE AUDIO)
                    var newSub = currentItem.createSubClip(currentItem.name, inPoint , outPoint , 0 , 1 , 1 ); 
                    newSub.moveBin(moveTo); // MOVE INTO THE SUBCLIPS BIN
                    placeClip(activeSeq, newSub , bufferTime);
                    // alert('Thema platziert!');
                }
            }
        }

        // Sequenz Übergabe an Media Encoder
        var project = app.project;
        var sequence = project.activeSequence;

        // app.encoder.launchEncoder();
        $.writeln(sequence.name);
        
        var rootPath = "/Users/mac_nonprint/";
        var Dateiname = rootPath + "Desktop/PR-Automatisierung/render/" + sequenzName + ".mp4";
        var exportPreset = rootPath +  "Documents/Adobe/Adobe\ Media\ Encoder/22.0/Presets/Test\ Vorgabe.epr";

        app.encoder.encodeSequence(sequence, Dateiname, exportPreset, 0, true);
        // app.encoder.startBatch();

        if ((durationIntro < 1) && (durationThema < 1)) {
            var durationIntro = '';
            var durationThema = '';
        }

        /* project.rootItem.children[0].deleteBin()
        project.rootItem.children[0].deleteBin()
        project.rootItem.children[0].deleteBin()
        project.rootItem.children[0].deleteBin()
        project.rootItem.children[0].deleteBin()
        project.rootItem.children[0].deleteBin()
        project.rootItem.children[1].deleteBin()
        project.deleteSequence(sequence); */
    }

    // alert(numItems);
}

// FUNCTION LIST
function findBinIndex(currentItem, nameToFind){  
    if(nameToFind){
        for (var j = 0; j < currentItem.children.numItems; j++){  
            var currentChild = currentItem.children[j];  
            
            if (currentChild.type == ProjectItemType.BIN && currentChild.name.toUpperCase() == nameToFind.toUpperCase() ){
                globalBind = currentChild;
                return currentChild;
            }
            
            if (currentChild.type == ProjectItemType.BIN){      
                findBinIndex(currentChild, nameToFind);  
            }   
        }  
        
    } else {
        alert("No bin was targeted");
    }
}
function timecodeToSeconds(arrayObject){
    var timeCodeArray = [];
    timeCodeArray = arrayObject.split(":");
    var timeCode = (Number(timeCodeArray[0])*60) + Number((timeCodeArray[1]));
    return timeCode;   
}
function placeClip(activeSeq , subClip , buffer){
    subClip.setScaleToFrameSize();// SET SCALE TO FRAME SIZE 
    
    if(activeSeq.videoTracks[0].clips.numItems == 0){ // IF THERE ARE NOT CLIPS IN THE SEQUENCE, PLACE FRIST CLIP AT TIME ZERO
        activeSeq.videoTracks[0].insertClip(subClip,0) 
        //clipCounter++;
    } else { // IF THERE ARE CLIPS IN THE SEQUENCE, PLACE AT THE TIMECODE OF END OF THE LAS CLIP + THE BUFFER TIME
        var numClips = activeSeq.videoTracks[0].clips.numItems;
        var insertTime = activeSeq.videoTracks[0].clips[numClips - 1].end.seconds + buffer;
        activeSeq.videoTracks[0].insertClip(subClip,insertTime);
        //clipCounter++;
    }
}

 

TOPICS
Editing, Error or problem, How to, SDK

Views

86

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Employee , Nov 15, 2021 Nov 15, 2021
>The problem i am facing right now is to get the duration of the imported clips and dynamically >put the second video on the end of the first. After calling insertClip(), doesn't PPro position the CTI (Current Time Indicator) at the end of the previously inserted trackItem? Isn't that exactly where you want the CTI?  You would get the duration of the inserted clip by getting the sequence in and out points, of the newly-added trackItem.

Likes

Translate

Translate
Adobe Employee ,
Nov 15, 2021 Nov 15, 2021

Copy link to clipboard

Copied

>The problem i am facing right now is to get the duration of the imported clips and dynamically
>put the second video on the end of the first.

 

After calling insertClip(), doesn't PPro position the CTI (Current Time Indicator) at the end of the previously inserted trackItem? Isn't that exactly where you want the CTI? 🙂

 

You would get the duration of the inserted clip by getting the sequence in and out points, of the newly-added trackItem.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 16, 2021 Nov 16, 2021

Copy link to clipboard

Copied

LATEST

Made it. Thanks. 

 

This worked for me perfectly. It think thats what you meant. 

 

 

var durationIntro = parseInt(importBin.children[intro].getOutPoint(1).seconds);
var durationThema = parseInt(importBin.children[thema].getOutPoint(1).seconds);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines