+
+
Create PowerShell script to batch process with ffmpeg
+
As of Windows 10, it is possible to run Bash via Bash on Ubuntu on Windows, allowing you to use bash scripting. To enable Bash on Windows, see these instructions.
+
On Windows, the primary native command line programme is PowerShell. PowerShell scripts are plain text files saved with a .ps1 extension. This entry explains how they work with the example of a PowerShell script named “rewrap-mp4.ps1”, which rewraps .mp4 files in a given directory to .mkv files.
+
“rewrap-mp4.ps1” contains the following text:
+
$inputfiles = ls *.mp4
+foreach ($file in $inputfiles) {
+ $output = [io.path]::ChangeExtension($file, '.mkv')
+ ffmpeg -i $file -map 0 -c copy $output
+}
+
+ - $inputfiles = ls *.mp4
- Creates the variable
$inputfiles
, which is a list of all the .mp4 files in the current folder.
+ In PowerShell, all variable names start with the dollar-sign character.
+ - foreach ($file in $inputfiles)
- Creates a loop and states the subsequent code block will be applied to each file listed in
$inputfiles
.
+ $file
is an arbitrary variable which will represent each .mp4 file in turn as it is looped over.
+ - {
- Opens the code block.
+ - $output = [io.path]::ChangeExtension($file, '.mkv')
- Sets up the output file: it will be located in the current folder and keep the same filename, but will have an .mkv extension instead of .mp4.
+ - ffmpeg -i $file
- Carry out the following ffmpeg command for each input file.
+ Note: To call ffmpeg here as just ‘ffmpeg’ (rather than entering the full path to ffmpeg.exe), you must make sure that it's correctly configured. See this article, especially the section ‘Add to Path’.
+ - -map 0
- retain all streams
+ - -c copy
- enable stream copy (no re-encode)
+ - $output
- The output file is set to the value of the
$output
variable declared above: i.e., the current file name with an .mkv extension.
+ - }
- Closes the code block.
+
+
Note: the PowerShell script (.ps1 file) and all .mp4 files to be rewrapped must be contained within the same directory, and the script must be run from that directory.
+
Execute the .ps1 file by typing .\rewrap-mp4.ps1
in PowerShell.
+
Modify the script as needed to perform different transcodes, or to use with ffprobe. :)
+
+
+