Extract audio track from mp4 with PowerShell and ffmpeg in Windows

This short script intends to extract an (AAC) audio track from several mp4 videos.

Note: This works with PowerShell 3+.

FFmpeg is needed. Windows builds can be found here. Download the archive, extract it, put the ffmpeg folder somewhere (it's standalone) and then don't forget to change the path of $ffmpeg_bin in the script.

Then the script only needs to be executed as powershell in the directory where the video files are stored. It won't delete video files but will create a new file (containing only the audio track) with the same name and aac extension (can be rename to m4a) instead of mp4.

1
2
3
4
5
6
7
8
9
10
$ffmpeg_bin = "C:\path\example\ffmpeg-3.3.3-win64-static\bin\ffmpeg.exe"
Get-ChildItem "$PSScriptRoot" -Filter *.mp4 |
Foreach-Object {
$filename = $_.name
$aac_filename = $filename -replace '.mp4','.aac'
Write-Host $filename
$AllArgs = @('-i', "$filename", '-vn', '-acodec', 'copy', "$aac_filename")
& "$ffmpeg_bin" $AllArgs
}

PS: the script was tested only with video containing only one track.

Share