Skip to main content
Participant
July 31, 2019
Answered

Script writing to setup graphic layer's length to be the same as an audio file's length

  • July 31, 2019
  • 1 reply
  • 366 views

Hello everyone, I'm new to scripting and new to this forum, so my apologies if this has been asked somewhere- I tried to search but my inquiry is quite specific and i couldn't find anything (feel free to direct me to any related resources though!)

What i'm trying to do is have a script setup graphic layers (PNGs) to lay out in the timeline to be the same length as audio files of the same name.


Anyone have any ideas, or is this not possible? Thanks in advance!

This topic has been closed for replies.
Correct answer migueld83371718

Depending on exactly what you need, it can be a more or less trivial thing. Here is the essential idea of ​​how you can proceed:

1- Find the items on the project.

2- Add them to a comp(if they are not in a comp);

3-Match the duration of the image layer to the audio duration.

Here is a pretty basic example:

function findItem(name, filter, items) {

  var item;

  var filters = (!filter) ? FootageItem : filter;

  var items = items || app.project.items;

  for (var i = 1, l = items.length; i <= l; i++) {

    item = items;

    if (name == item.name && item instanceof filter) {

      return item

    }

    if (item instanceof FolderItem) {

      findItem(name, filters, item.items);

      continue

    }

  }

}

function matchAudioDuration(itemName, imgExtension, audioExtension){

  var img, audio, comp;

  img = itemName + '.' + imgExtension;    //Image name

  audio = itemName + '.' + audioExtension;//Audio name

  img = findItem( img, FootageItem);      //The mage itself

  audio = findItem(audio, FootageItem);  //The audio itself

  comp = app.project.activeItem;  //Or any comp, ie: findItem('myComp', CompItem);

  img = comp.layers.add(img);    //Add the image to the comp

  img.outPoint = audio.duration;  //Set the duration of the image layer to fit audio

  comp.layers.add(audio);        //Add the audio to the comp

}

matchAudioDuration('myItem', 'png', 'mp3');

1 reply

migueld83371718
migueld83371718Correct answer
Inspiring
July 31, 2019

Depending on exactly what you need, it can be a more or less trivial thing. Here is the essential idea of ​​how you can proceed:

1- Find the items on the project.

2- Add them to a comp(if they are not in a comp);

3-Match the duration of the image layer to the audio duration.

Here is a pretty basic example:

function findItem(name, filter, items) {

  var item;

  var filters = (!filter) ? FootageItem : filter;

  var items = items || app.project.items;

  for (var i = 1, l = items.length; i <= l; i++) {

    item = items;

    if (name == item.name && item instanceof filter) {

      return item

    }

    if (item instanceof FolderItem) {

      findItem(name, filters, item.items);

      continue

    }

  }

}

function matchAudioDuration(itemName, imgExtension, audioExtension){

  var img, audio, comp;

  img = itemName + '.' + imgExtension;    //Image name

  audio = itemName + '.' + audioExtension;//Audio name

  img = findItem( img, FootageItem);      //The mage itself

  audio = findItem(audio, FootageItem);  //The audio itself

  comp = app.project.activeItem;  //Or any comp, ie: findItem('myComp', CompItem);

  img = comp.layers.add(img);    //Add the image to the comp

  img.outPoint = audio.duration;  //Set the duration of the image layer to fit audio

  comp.layers.add(audio);        //Add the audio to the comp

}

matchAudioDuration('myItem', 'png', 'mp3');