2016-08-30 11 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