How to iterate over a certain pattern of file names to use it on FFMPEG and PowerShell

I'm fairly inexperienced with PowerShell and I use it in certain occasions when I need to have total control of the conversion needed. So, I am facing a problem where I have a collection of 20 videos. All share the same number pattern from 1300 to 1319, and I want to convert the whole collection of "MOV" files into "MP4" files using this command:
ffmpeg -i my-video-1300.mov -vcodec h264 -acodec mp2 my-video-1300.mp4

Is there a way to use a Regular Expression, wildcard, or whatever name is given to simply create a single line conversion command.
My idea of what it "should look like" is:
ffmpeg -i my-video-13[00-19].mov -vcodec h264 -acodec mp2 my-video-13[00-19].mp4

Of course this last line doesn't work, but I would like to learn the correct way of doing it.
Thanks.

Upvotes: 1

Views: 741

Answers (1)

mklement0
mklement0

Reputation: 437663

Get-ChildItem -Filter my-video-*.mov |
Where-Object {
  if ($_.BaseName -match '-(\d+)$') {     
    $number = [int] $Matches[1]
    $number -ge 1300 -and $number -le 1319    
  }
} | 
ForEach-Object { 
  ffmpeg -i $_.FullName -vcodec h264 -acodec mp2 ($_.FullName -replace '\.mov$', '.mp4')
}
  • Get-ChildItem -Filter my-video-*.mov pre-filters candidate files efficiently using a simple wildcard pattern.

  • The Where-Object script block then extracts the number from the base file name via a regex (regular expression) and the -match operator, and checks if the number is in the range of interest.

    • Outputting $true from the script block (implicitly from the Boolean range-checking expression, $number -ge ...) causes the file at hand to be included and passed down the pipeline.
    • Note that producing no output is implicitly interpreted as $false, based on PowerShell's too-Boolean conversion logic.
  • The ForEach-Object script block is then only executed for the files of interest.

    • ($_.FullName -replace '\.mov$', '.mp4') derives the output file name from the input file name by replacing the .mov extension with .mp4, using a regex and the -replace operator.

Upvotes: 2

Related Questions