• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Replace audio paths premiere pro scripting

Community Beginner ,
Jul 02, 2024 Jul 02, 2024

Copy link to clipboard

Copied

This should be simple but seems extremely difficult. All I want to do is replace the paths of some audio tracks referenced from a json file. So answer{n}Audio.wav and question{n}Audio.wav. I have 'placeholder' audio tracks set up in my sequence and am trying to create a script that loops through each set of question and answer tracks and replaces them with the new audio files at the exact start time they are currently located at. 

 

Because this is seeming so difficult I'm trying to do this for just 1 question to see if i can get the logic right but am failing miserably. Surely this should be simple?! This is my current code:

 

#include "json2.js"

// Function to read and parse JSON file
function readJSONFile(filePath) {
    var file = new File(filePath);
    file.open('r');
    var content = file.read();
    file.close();
    return JSON.parse(content);
}

// Function to find the project item by media path
function findProjectItemByName(name) {
    var items = app.project.rootItem.children;
    for (var i = 0; i < items.numItems; i++) {
        var item = items[i];
        if (item && item.name === name) {
            return item;
        }
    }
    return null;
}

// Function to replace a clip's media file with a new media file
function replaceClipMedia(sequence, trackIndex, clipName, newFilePath) {
    var track = sequence.audioTracks[trackIndex];
    var clipFound = false;
    $.writeln("Searching for clip: " + clipName + " on track: " + track.name);

    for (var clipIndex = 0; clipIndex < track.clips.numItems; clipIndex++) {
        var clip = track.clips[clipIndex];
        if (clip.name === clipName) {
            $.writeln("Clip found: " + clip.name + " at index: " + clipIndex);

            // Find the project item associated with the clip
            var projectItem = findProjectItemByName(clip.name);
            if (projectItem) {
                $.writeln("Changing media path for project item: " + projectItem.name);
                var result = projectItem.changeMediaPath(newFilePath, true);
                if (result === 0) {
                    $.writeln("Successfully changed media path for " + clipName + " to: " + newFilePath);
                    clipFound = true;
                } else {
                    $.writeln("Failed to change media path for " + clipName + " to: " + newFilePath);
                }
            } else {
                $.writeln("No project item found for clip: " + clipName);
            }
            break;
        }
    }

    if (!clipFound) {
        $.writeln("Clip " + clipName + " not found on the track.");
    }
}

// Function to process question 4 only
function processQuestion4(jsonFilePath) {
    var jsonContent = readJSONFile(jsonFilePath);
    var basePath = new File(jsonFilePath).parent.fsName;

    function getFullPath(relativePath) {
        if (relativePath.indexOf(basePath) === 0) {
            // If the path already starts with basePath, return it as is
            return relativePath;
        }
        // Otherwise, join basePath with relativePath
        return basePath + '/' + relativePath;
    }

    $.writeln("Base path: " + basePath);

    // Process question4 audio
    var questionAudioDetails = jsonContent.media.filter(function(m) {
        return m.mediaName === "question4Audio";
    })[0];

    if (questionAudioDetails) {
        $.writeln("Relative path from JSON: " + questionAudioDetails.filePath);
        var questionAudioFilePath = getFullPath(questionAudioDetails.filePath);
        $.writeln("Constructed full path: " + questionAudioFilePath);
        $.writeln("Found question4 audio file: " + questionAudioFilePath);
        replaceClipMedia(sequence, 3, "question4Audio.wav", questionAudioFilePath);
    } else {
        $.writeln("Question4 audio file not found in JSON: " + jsonFilePath);
    }

    // Process answer4 audio
    var answerAudioDetails = jsonContent.media.filter(function(m) {
        return m.mediaName === "answer4Audio";
    })[0];

    if (answerAudioDetails) {
        $.writeln("Relative path from JSON: " + answerAudioDetails.filePath);
        var answerAudioFilePath = getFullPath(answerAudioDetails.filePath);
        $.writeln("Constructed full path: " + answerAudioFilePath);
        $.writeln("Found answer4 audio file: " + answerAudioFilePath);
        replaceClipMedia(sequence, 3, "answer4Audio.wav", answerAudioFilePath);
    } else {
        $.writeln("Answer4 audio file not found in JSON: " + jsonFilePath);
    }
}

// Get the active sequence
var sequence = app.project.activeSequence;

// Process question 4
processQuestion4("/Users/edwardnewton/Downloads/quiz-buddha/question4.json");

 Any help would be hugely appreciated as this has taken me days!

TOPICS
Audio , Error or problem , How to

Views

294

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
Community Expert ,
Jul 02, 2024 Jul 02, 2024

Copy link to clipboard

Copied

To figure out what is going wrong, you best explain what exactly is not working. Best come up with a minimal example.

Or at least tell us what error message you get at which line of the code (or what behavior you see that you don't expect).

 

It is very unlikely that somebody will read your more than 100 lines of code, without knowing your json data and what issue you have, and then be able to spot the problem.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects

Votes

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
Community Beginner ,
Jul 03, 2024 Jul 03, 2024

Copy link to clipboard

Copied

LATEST

Thanks Mathias, I worked it out in the end. the issue was that I hadn;t realised duplicating and renaming the audio files meant that I'd ended up with the same nodeId meaning that I was unable to target individual audio clips.

Votes

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