none
Creating a loop with pattern matching to concatenate audio files using ffmpeg

    Question

  • I have a folder full of 44.1/16 Wave files like this:

    WNYE-NYAC-2001-04-05-1234-A.wav
    WNYE-NYAC-2001-04-05-1234-B.wav
    WNYE-NYAC-2001-04-06-3456-A.wav
    WNYE-NYAC-2001-04-06-3456-B.wav

    I want to use ffmpeg to concatenate the A and B tracks with the same prefixes into single Wave files named WNYE-NYAC-2001-04-05-1234.wav, and WNYE-NYAC-2001-04-06-3456.wav etc.  

    I have a bash script that works but obviously not on my Windows XP machine. Is there a way to translate the below script into the windows command line syntax? 

    for i in NYAC-*-A.wav; do echo "file $i">temp.txt echo "file ${i/-A/-B}">>temp.txt ffmpeg -f concat -i temp.txt -codec copy "${i/-A/}" done rm temp.txt
    The first line creates a loop that matches all file that look like NYAC-anything-A.wav, substituting the name of the file for the variable $i wherever it appears inside the loop.

    The second line creates a text file "temp.txt" containing the line
    file NYAC-1234-A.wavand the third appendsfile NYAC-1234-B.wavto the file

    This third line uses parameter expansion to substitute -B for -A in the file names.  Then it does the ffmpeg-ing using the temp.txt file, and lastly removes the temp file. Obviously if you have a file called temp.txt it would be destroyed by this script.



    Thursday, September 26, 2013 8:50 PM

Answers

  • Here's a PowerShell equivalent of what it looks to me like you're trying to do:


    get-childitem *-a.wav | foreach-object {
      $fileA = $_.Name
      $fileB = $fileA -replace '-A\.wav','-B.wav'
      "file $fileA" | out-file temp.txt -encoding ASCII
      "file $fileB" | out-file temp.txt -encoding ASCII -append
      ffmpeg -f concat -i temp.txt -codec copy ($fileA -replace '-A', '')
    }
    remove-item temp.txt
    

    I used out-file with ASCII encoding rather than > and >> because I don't know if your 'ffmpeg' utility works with Unicode text files.

    Bill

    Thursday, September 26, 2013 10:02 PM
    Moderator

All replies

  • Hi,

    Please use the "insert code" button for your code, because the "first", "second", etc. lines you're referencing have been run together, as you can see.

    Bill

    Thursday, September 26, 2013 9:38 PM
    Moderator
  • Here's a PowerShell equivalent of what it looks to me like you're trying to do:


    get-childitem *-a.wav | foreach-object {
      $fileA = $_.Name
      $fileB = $fileA -replace '-A\.wav','-B.wav'
      "file $fileA" | out-file temp.txt -encoding ASCII
      "file $fileB" | out-file temp.txt -encoding ASCII -append
      ffmpeg -f concat -i temp.txt -codec copy ($fileA -replace '-A', '')
    }
    remove-item temp.txt
    

    I used out-file with ASCII encoding rather than > and >> because I don't know if your 'ffmpeg' utility works with Unicode text files.

    Bill

    Thursday, September 26, 2013 10:02 PM
    Moderator
  • Something like this:

    <#
    bash

    for i in NYAC-*-A.wav; do echo "file $i">temp.txt echo "file ${i/-A/-B}">>temp.txt ffmpeg -f concat -i temp.txt -codec copy "${i/-A/}" done rm temp.txt #>
    #powershell $files=dir NYAC-*-A.wav foreach($f in $files){ echo $f.Name >temp.txt echo $f.Name.Replace('-A','-B') >> temp.txt ffmpeg -f concat -i temp.txt -codec copy $f.Name.Replace('-A','') rm temp.txt }



    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, September 26, 2013 10:06 PM
    Thursday, September 26, 2013 10:05 PM
  • Here is the easier way to cat WAV files.

    $files=dir NYAC-*-A.wav
    foreach($f in $files){
        $name=$f.Name.Replace('-A','')
        sox "$name-A.wav" $name-B.wav" $name
    }
    
         
    


    ¯\_(ツ)_/¯

    Thursday, September 26, 2013 10:20 PM