I have created a Powershell script to help me extract the audio of a video file and output it to a mp3 file. The script worked but there is problem. The resulting output file has a closing bracket at the end of the file's name, that is the file's name is not "example_output_file.mp3" but "example_output_file.mp3}". I hope somebody can give the help.
Here is the script:
Code: Select all
$currentScriptPath = $PSScriptRoot
Write-Host "Please press enter and choose the video file you want to extract audio from below"
Pause
Add-Type -AssemblyName System.Windows.Forms
$mediaFileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
Multiselect = $false # Multiple files can be chosen
Filter = 'Videos (*.mkv,*.mp4)|*.mkv;*.mp4' # Specified file types
}
[void]$mediaFileBrowser.ShowDialog()
$mediaFilePath = $null;
If($mediaFileBrowser.FileNames -like "*\*") {
$mediaFilePath = $mediaFileBrowser.FileName;
$mediaFileBrowser.FileName #Lists selected files (optional)
}
Else {
Write-Host "Cancelled by user"
Exit
}
$mediaFileName = Split-Path -Path $mediaFilePath -leaf
$mediaFileLocation = Split-Path -Path $mediaFilePath
$mediaFileBaseName = (Get-Item -literalPath $mediaFilePath).BaseName
$newFileSaveDestination = "$mediaFileLocation" + "\" + "[audio] " + $mediaFileBaseName + ".mp3"
$inputMediaFileURI = ([system.uri]$mediaFilePath).AbsoluteUri
$bitrate = 0
Do{
#ask user to input an integer for the bitrate of the resulting mp3 file
Write-Host "Please input the audio bitrate for the extracted mp3 file"
$bitrate = Read-Host "Enter the bitrate for the audio of the output file"
}
#loop the above step until user input an integer greater than 0
Until (($bitrate -match "^\d+$") -and ([Int]$bitrate -gt 0))
$argumentList = "-vvv $inputMediaFileURI --sout=`"#transcode{vcodec=none,acodec=mp3,ab=$bitrate,channels=2,samplerate=44100,scodec=none}:std{access=file{no-overwrite},mux=raw,dst=$newFileSaveDestination}`" --sout-all --sout-keep vlc://quit"
$currentTime = Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss.fff K"
Write-Host "Begin video audio extract on $currentTime"
Start-Process -NoNewWindow -FilePath "C:\Program Files\VideoLAN\VLC\vlc.exe" -ArgumentList "$argumentList"
$currentTime = Get-Date -Format "dddd MM/dd/yyyy HH:mm:ss.fff K"
Write-Host "Done video audio extract on $currentTime"
Write-Host "The new mp3 file can be found at $newFileSaveDestination"
Write-Host "Done"