2016-08-30 43 views
1

我正在研究Adobe After Effects的Extendscript工具,并试图弄清楚一件事。 Extendscript只是Javascript的顶部添加了一些Adobe绒毛,但几乎任何Javascript都不是特定于浏览器的作品。图像序列的Javascript数组 - 仅返回序列中的第一个图像

我有一段脚本我一直在使用/开发一段时间,这大大简化了在图像序列中导入数千幅图像的过程。一切工作都很好,但有一个特定区域运行速度非常缓慢,当你开始进入数千张图像时,我正试图弄清楚如何加快速度。

我想要优化的是经过一系列可能有数千个图像文件,并将其连接到序列中的第一个图像。例如,我的数组可能是:[img.001.png,img.002.png,img.003.png,img.004.png,img.005.png],我想要做的是将每个“ img。###。png“除了第一个。所以在我的功能后,我只会:[img.001.png]。

以下是我现在就做:
1)我拿阵列项目A,正则表达式出来的数字序列
2)然后阵列B项,正则表达式出来的数字序列
3),并比较两个以看看它们是否相同。如果他们是我拼接项目B

这适用于100%的时间真棒,但是当你获得一千或两个图像以上时相当慢...我想要做的是找到一种方法更快地做到“修剪”步骤。我的一个朋友告诉我,正则表达式非常慢,所以也许我需要没有正则表达式呢?我还发现了几种原生JavaScript数组方法,它们可能有助于forEach()every()和filter(),但我不明白这些会如何更快,因为我仍然需要做2次正则表达式评估,而且我仍然需要比较项目彼此。

任何帮助将非常感激!

  • 斯宾塞

这里是我现有的代码片段:

currentFolder = new Folder ("//12.34.5.67/my folder on the network/") 
var folderChildren = currentFolder.getFiles().sort(); 

var searcher = new RegExp("\\d{3,5}[.]"); 
for (var i = 0; i < folderChildren.length; i++) { 
    // Go through the array and strip out all elements that are the same once their numbers have been removed with a regex 
    if (i > 0) { 
     currentResult = searcher.exec(folderChildren[i].name); //check if a sequence 
     if (currentResult) { // it is a sequence 
      // first parse out the comparison strings - current item and item before 
      var testNameBefore = folderChildren[i-1].name; 

      //if we have a sequence before our current item, we need to delete the numbers. 
      var beforeNum = searcher.exec(testNameBefore); 
      if (beforeNum) { 
       testNameBefore = testNameBefore.substring(0, testNameBefore.length-8); 
      } 

      var testNameCurrent = folderChildren[i].name; 
      testNameCurrent = folderChildren[i].name.substring(0, testNameCurrent.length-8); 

      //compare to the element before it and delete if the same!! 
      if (testNameBefore == testNameCurrent) { 
       folderChildren.splice(i, 1); 
       i--; 
      } 
     } 
    } 
} 
+0

是文件格式始终* .Number.png? – Jecoms

+0

它总是以Number.png结尾,但并不总是在数字之前的一段时间。数字序列并不总是3位数字。 – Spencer

回答

0

你没有一个代码段来比较,但这种运行在短短几毫秒。

var files = getFiles(), 
 
    cleanedFiles = getDesequencedArray(files); 
 

 
console.log(`Parsing ${files.length} files.`); 
 
console.log(`Found ${cleanedFiles.length} unique sequences:`); 
 
console.log(cleanedFiles); 
 

 
function getDesequencedArray(files) { 
 
    return files.reduce(
 
    function(memo, file) { 
 
     var sequence = file.match(/(.*)\.?\d{3,5}(.*)/); 
 
     if (sequence) { 
 
     if (!memo.found[sequence[1] + sequence[2]]) { 
 
      memo.found[sequence[1] + sequence[2]] = 1; 
 
      memo.files.push(file); 
 
     } 
 
     } else { 
 
     //Did you want the other files included too? 
 
     //memo.files.push(file); 
 
     } 
 
     return memo; 
 
    }, { 
 
     found: {}, 
 
     files: [] 
 
    } 
 
).files; 
 
} 
 

 
function getFiles() { 
 
    var files = [ 
 
    "img1.001.jpg", 
 
    "img1.002.jpg", 
 

 
    "img1.001.png", 
 
    "img1.002.png", 
 
    "img1.003.png", 
 
    "img1.004.png", 
 

 
    "img2.011.jpg", 
 
    "img2.012.jpg", 
 
    "img2.013.jpg", 
 
    "img2.014.jpg", 
 

 
    "img300001.png", 
 
    "img300002.png", 
 
    "img300003.png", 
 
    "img300004.png", 
 

 
    "img300002.100.png", 
 
    "img300002.200.png", 
 

 
    "readme.md", 
 
    "index.html" 
 
    ]; 
 

 

 
    // Adding a bunch of records 
 
    for (let x = 0; x < 100; x++) 
 
    for (
 
     let i = 1; 
 
     i <= 1000; 
 
     files.push(`img-${x}.` + ("00" + i++).substr(-3) + '.png') 
 
    ); 
 
    return files; 
 
}

+0

看起来很有希望,我会尽快试用!谢谢回复。 – Spencer

+0

嘿马尔克,对不起,我还没有真正尝试过这一点(中间一堆的东西...),但我只是检查了这个参考减法方法:[链接](http://www.w3schools.com/jsref /jsref_reduce.asp),我认为它不会起作用。我意识到我没有指定数组可能一次包含_multiple_图像序列! – Spencer