Replace audio paths premiere pro scripting
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!