Skip to main content
watanabed
Participant
April 23, 2020
Answered

How to get current preview time with ExtendScript

  • April 23, 2020
  • 4 replies
  • 1800 views

Hi.

 

How to get current preview time with ExtendScript.

I want to get the current time of the preview like the expression "time," not time of the Current Time Indicator.

 

Thank you.

This topic has been closed for replies.
Correct answer Mathias Moehl

An expression is evaluated at every frame and each time the expression is evaluated, the variable time gives you this point in time. There is no such thing for scripts, since they are not evaluated at each frame, but only once, when you execute them.

If you want to execute a script while a preview is playing, there is no way to know which frame is playing while the script is running (and if it is a long running script I am not even sure if playback pauses or not).

4 replies

Electric_Marvel5FEC
Known Participant
May 15, 2024

Since there isn't a proper way to get the time during playback, I found this interesting workaround:

 

function getPlaybackTime () {
    // 1. Get the active comp
    var comp = app.project.activeItem;

    // 2. Read all markers in the composition (if there are any)
    function getCompMarkers () {
      var output = [];
      var markProp = comp.markerProperty;
      for (var i = 1; i <= markProp.numKeys; i++) {
        var marker = markProp.keyValue(i);
        marker['time'] = markProp.keyTime(i);
        output.push(marker);
      }

      return output;
    };

    // 3. Store the markers in a variable
    var origCompMarkers = getCompMarkers();

    // 4. Add a new marker (same as pressing the * key)
    var id = app.findMenuCommandId('Add Marker');
    app.executeCommand(id);

    // 5. Find the new marker we added and get its time and index
    function getNewMarker (origCompMarkers) {
        var newCompMarkers = getCompMarkers();
        var oldTimes = [];
        var newTimes = [];
        var diffTime = null;
        var diffIndex = null;
        for (var i = 0; i < origCompMarkers.length; i++) {
            oldTimes.push(origCompMarkers[i]['time']);
        }
        for (var j = 0; j < newCompMarkers.length; j++) {
            newTimes.push(newCompMarkers[j]['time']);
        }
        for (var h = 0; h < newTimes.length; h++) {
            if (oldTimes.indexOf(newTimes[h]) < 0) {
                diffTime = newTimes[h];
                diffIndex = h + 1;
            }
        }
        return {time: diffTime, index: diffIndex};
    };

    // 6. Store the new marker in a variable
    var newMarker = getNewMarker(origCompMarkers);

    // 7. Remove the new marker
    comp.markerProperty.removeKey(newMarker.index);

    // 8. Return the time of the new marker
    return newMarker.time;    
}

// 9. Enjoy :)
var previewTime = getPlaybackTime();
alert(previewTime);

I don't know why exactly, but it doesn't work if you preview only audio without video (if someone can figure it out it'll be awesome)

Mylenium
Legend
April 23, 2020

Agree with Mathias. Your idea doesn't make much sense to begin with, given how this stuff works. If you want to execute scripts and be aware of specific timing, you have to give them something to latch on to like markers, keyframes or the layer in- and out-points.

 

Mylenium

Mathias Moehl
Community Expert
Mathias MoehlCommunity ExpertCorrect answer
Community Expert
April 23, 2020

An expression is evaluated at every frame and each time the expression is evaluated, the variable time gives you this point in time. There is no such thing for scripts, since they are not evaluated at each frame, but only once, when you execute them.

If you want to execute a script while a preview is playing, there is no way to know which frame is playing while the script is running (and if it is a long running script I am not even sure if playback pauses or not).

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Tomas Sinkunas
Legend
April 23, 2020

Not quite sure what you mean by preview time, but if you're looking work area, then look at three 2 properties:

http://docs.aenhancers.com/items/compitem/#compitem-workareaduration

http://docs.aenhancers.com/items/compitem/#compitem-workareastart