Skip to main content
Participating Frequently
November 5, 2024
Answered

Issue with Duplicate Clips When Inserting from Tabulated File in Adobe Premiere Pro Script

  • November 5, 2024
  • 3 replies
  • 2418 views

The script is designed to facilitate the process of inserting selected clips from an active sequence in Adobe Premiere Pro into a new sequence. Despite the intended functionality, I am experiencing an issue where clips are being duplicated in the new sequence. The insertion logic seems to be adding clips multiple times, which is not the desired behavior. I would like to understand why this duplication is occurring and how to resolve it.

 

// Function to create the new sequence from the tabulated file
function createSequenceFromFile(filePath, project, newSequence) {
    $.writeln("Reading file information: " + filePath);
    var file = new File(filePath);
    if (!file.exists) {
        $.writeln("File not found: " + filePath);
        return;
    }
    
    file.open("r");
    var line;
    var newVideoTrack = newSequence.videoTracks[0];
    var insertPosition = 0;

    // Ignore the header
    file.readln();
    
    // Read each line of the file
    while (!file.eof) {
        line = file.readln();
        var parts = line.split("\t");
        if (parts.length === 4) {
            var clipName = parts[0];
            var inPoint = parseFloat(parts[1]);
            var outPoint = parseFloat(parts[2]);
            var duration = outPoint - inPoint;

            $.writeln("Inserting clip: " + clipName + " at position: " + insertPosition);
            var projectItem = findProjectItem(clipName); // Find the project item

            if (projectItem) {
                var newClip = newVideoTrack.insertClip(projectItem, insertPosition);
                if (newClip) {
                    newClip.end = newClip.start + duration;
                    insertPosition += duration;
                    $.writeln("Clip successfully inserted: " + clipName);
                } else {
                    $.writeln("Error inserting clip: " + clipName);
                }
            } else {
                $.writeln("Clip not found in project: " + clipName);
            }
        }
    }
    file.close();
}

// Function to find the project item by name (recursive search)
function findProjectItem(clipName) {
    return recursiveSearch(app.project.rootItem, clipName);
}

// Recursive function to search for the item
function recursiveSearch(folder, clipName) {
    for (var i = 0; i < folder.children.numItems; i++) {
        var item = folder.children[i];
        if (item.name === clipName) {
            return item; // Return the project item that matches the name
        }
        if (item.type === ProjectItemType.BIN) { // If it's a folder, search recursively
            var found = recursiveSearch(item, clipName);
            if (found) {
                return found; // Return if found in subfolders
            }
        }
    }
    return null; // Return null if the item is not found
}

 

Correct answer bbb_999
Bruce, got any ideas? 
@Bruce Bullis 

Yes, as stated above: Set a breakpoint before calling insertClip(), and determine why the insertPosition value is invalid.

3 replies

Inspiring
July 21, 2025

Only now, in the middle of 2025, did I finally understand Bruce, bbb_999 tip about the insertClip duplication bug in Premiere scripting. For years, I struggled with my scripts duplicating or overlapping clips, and I couldn’t figure out why. The key insight is: you must always clear and set the in/out points on the ProjectItem before each insert. If you don’t, Premiere may insert the wrong segment or duplicate the last one, because it caches the in/out points internally. This applies to both ExtendScript.

Here’s a minimal example of the correct approach:

for (const segment of segments) {
  // Always clear in/out points before setting new ones
  if (typeof projectItem.clearInPoint === "function") projectItem.clearInPoint();
  if (typeof projectItem.clearOutPoint === "function") projectItem.clearOutPoint();

  // Set the in/out points for this segment
  projectItem.setInPoint(segment.in, 4);
  projectItem.setOutPoint(segment.out, 4);

  // Insert at the correct position (update insertPosition after each insert)
  var newClip = videoTrack.insertClip(projectItem, insertPosition);

  // Advance insertPosition by the segment duration
  insertPosition += (segment.out - segment.in);
}

 The main points are: always clear the in/out points before setting new ones, always update insertPosition after each insert to avoid overlapping, and never assume Premiere will “remember” the correct in/out for each insert. Since I started doing this, my inserts are clean and bug-free. Thanks again to Bruce, bbb_999 and the Adobe forums for the tip!

Inspiring
November 7, 2024

Adobe Premiere pro 24.10
Vscode -> ExtendScrip Debugger

Why are the clips being duplicated in the new sequence when I read from a tabulated file and insert them into a newly created sequence?

 


The complete script should do:
read a tabbed file with a list of clips with information including their names, in points, out points, and durations.
The problem is that when he inserts the clips into the timeline, he duplicates the clips!

 

 

// Function to create the new sequence from the tabulated file
function createSequenceFromFile(filePath, project, newSequence) {
    $.writeln("Reading file information: " + filePath);
    var file = new File(filePath);
    if (!file.exists) {
        $.writeln("File not found: " + filePath);
        return;
    }
    
    file.open("r");
    var line;
    var newVideoTrack = newSequence.videoTracks[0];
    var insertPosition = 0;

    // Ignore the header
    file.readln();
    
    // Read each line of the file
    while (!file.eof) {
        line = file.readln();
        var parts = line.split("\t");
        if (parts.length === 4) {
            var clipName = parts[0];
            var inPoint = parseFloat(parts[1]);
            var outPoint = parseFloat(parts[2]);
            var duration = outPoint - inPoint;

            $.writeln("Inserting clip: " + clipName + " at position: " + insertPosition);
            var projectItem = findProjectItem(clipName); // Find the project item

            if (projectItem) {
                var newClip = newVideoTrack.insertClip(projectItem, insertPosition);
                if (newClip) {
                    newClip.end = newClip.start + duration;
                    insertPosition += duration;
                    $.writeln("Clip successfully inserted: " + clipName);
                } else {
                    $.writeln("Error inserting clip: " + clipName);
                }
            } else {
                $.writeln("Clip not found in project: " + clipName);
            }
        }
    }
    file.close();
}

// Function to find the project item by name (recursive search)
function findProjectItem(clipName) {
    return recursiveSearch(app.project.rootItem, clipName);
}

// Recursive function to search for the item
function recursiveSearch(folder, clipName) {
    for (var i = 0; i < folder.children.numItems; i++) {
        var item = folder.children[i];
        if (item.name === clipName) {
            return item; // Return the project item that matches the name
        }
        if (item.type === ProjectItemType.BIN) { // If it's a folder, search recursively
            var found = recursiveSearch(item, clipName);
            if (found) {
                return found; // Return if found in subfolders
            }
        }
    }
    return null; // Return null if the item is not found
}

 

 

Bruce Bullis
Community Manager
Community Manager
November 5, 2024



If you set breakpoints in your function, can you see when the second and subsequent trackItems, are created from your insert() call? 

 

When you say duplicates, do you actually mean both audio and video trackItems? Or do you actually mean "multiple trackItems are created from the same projectItem, on the destination track"?

Something else I noticed: projectItem.findItemsMatchingMediaPath() may help you, in your searching. 🙂

Participating Frequently
November 7, 2024
 

 Thanks for responding.
The ones marked in red are the "correct" ones, see the last one of these lost the original in%out, from this point on it (scrip) duplicates the same clip but with random in%out, see orange circle in the image. (top track is just a way to compare clips)

Bruce Bullis
Community Manager
Community Manager
November 7, 2024

I'm still curious:

If you set breakpoints in your function, can you see when the second and subsequent trackItems, are created from your insert() call?