Audio: Difference between revisions
mNo edit summary |
|||
Line 16: | Line 16: | ||
I had a bunch of old, large audio files that were recorded from a mixer. It recorded at stereo 44 kHz, 256kbps. There were just a lot of them, and in order to save space I modified a PowerShell script for myself. The original source can be found at [https://rolfeleveld.wordpress.com/2014/12/20/mp3-music-too-large-solved-with-powershell-and-lame/ Rolf Eleveld's Blog]. This handles all files, including subfolders recursively. When you try to use the script for the first time, you may want to uncomment the wait for key press lines (lines 94 & 95), and remove line 96--it overwrites the original file with converted one. | I had a bunch of old, large audio files that were recorded from a mixer. It recorded at stereo 44 kHz, 256kbps. There were just a lot of them, and in order to save space I modified a PowerShell script for myself. The original source can be found at [https://rolfeleveld.wordpress.com/2014/12/20/mp3-music-too-large-solved-with-powershell-and-lame/ Rolf Eleveld's Blog]. This handles all files, including subfolders recursively. When you try to use the script for the first time, you may want to uncomment the wait for key press lines (lines 94 & 95), and remove line 96--it overwrites the original file with converted one. | ||
<source lang="ps1" line highlight=" | You can change the target parameters on line 62. | ||
<source lang="ps1" line highlight="62,95-96"> | |||
#PowerShell script | #PowerShell script | ||
Revision as of 20:22, 3 June 2017
LAME
Converting audio files
- Last tested with LAME 3.99.5 (64-bits) on Windows 10 (10.0.14393)
for speech
Downsampling to mono 22.05 KHz with CBR of 48 kbps (works fine for recording of speech)
C:\Files> lame --resample 22.05 -m m -b 48 source.mp3 target.mp3
multiple files for speech
I had a bunch of old, large audio files that were recorded from a mixer. It recorded at stereo 44 kHz, 256kbps. There were just a lot of them, and in order to save space I modified a PowerShell script for myself. The original source can be found at Rolf Eleveld's Blog. This handles all files, including subfolders recursively. When you try to use the script for the first time, you may want to uncomment the wait for key press lines (lines 94 & 95), and remove line 96--it overwrites the original file with converted one.
You can change the target parameters on line 62.
#PowerShell script
# Update $sourceRoot here to point to the folder where you keep
$sourceRoot = "M:\audio\2015";
$lameExecutable = "C:\Files\tools\lame.exe";
$albumTitle = "Speech for Year 2015";
$genreName = "Speech";
# just to make sure we have the right index for bitrate, and ID3 tags these will be set in the first cycle
$bitrateAttribute = 0;
$titleAttribute = 0;
$contribAttribute = 0;
$genreAttribute = 0;
$albumAttribute = 0;
$yearAttribute = 0;
$trackAttribute = 0;
$maximumBitRate = 50;
#interact with the file-system (extensions)
$shell = new-object -com shell.application
# get a list of all files that exist in the subdirectory of type mp3
#directory object does not return folder name as per http://stackoverflow.com/questions/1153819/get-list-of-files-recursively-by-bit-rate-in-powershell
$files = Get-ChildItem -LiteralPath $sourceRoot -Filter '*.mp3' -Recurse -File -ErrorAction SilentlyContinue
$files | foreach {
# only process Audio files of type MP3
#filesystem reference object
$directoryObject = $shell.NameSpace( $_.Directory.FullName );
#object reference to instance of the file in context of the filesystem
$file = $directoryObject.ParseName( $_.Name );
#name placeholders of the file
$currentFileName = $_.FullName;
$newFileName = $_.DirectoryName+"\_"+$_.Name
# see http://blogs.technet.com/b/heyscriptingguy/archive/2008/08/13/how-can-i-find-files-metadata.aspx how it may work.
# Find the index of the bit rate attribute, if necessary.
for( $index = 5; -not ($bitrateAttribute -and $titleAttribute) -and $index -le 266; ++$index ) {
$name = $directoryObject.GetDetailsOf( $directoryObject.Items, $index )
if( $name -eq 'Bit rate' ) { $bitrateAttribute = $index }
if( $name -eq 'Title' ) { $titleAttribute = $index }
if( $name -eq 'Contributing artists' ) { $contribAttribute = $index }
if( $name -eq 'Genre' ) { $genreAttribute = $index }
if( $name -eq 'Year' ) { $yearAttribute = $index }
if( $name -eq 'Album' ) { $albumAttribute = $index }
if( $name -eq '#' ) { $trackAttribute = $index }
[System.String]::Format("Attribute: {0} - has index {1} with value: {2}", $name, $index, $directoryObject.GetDetailsOf( $file, $index ) )
}
#get song Details and ID3 tag
$songBitrate = $directoryObject.GetDetailsOf( $file, $bitrateAttribute );
$songTitle = $directoryObject.GetDetailsOf( $file, $titleAttribute );
$songArtist = $directoryObject.GetDetailsOf( $file, $contribAttribute );
$songGenre = $directoryObject.GetDetailsOf( $file, $genreAttribute );
$songYear = $directoryObject.GetDetailsOf( $file, $yearAttribute );
$songAlbum = $directoryObject.GetDetailsOf( $file, $albumAttribute );
$songTrack = $directoryObject.GetDetailsOf( $file, $trackAttribute );
# If the file has the desired bit rate, include it in the results.
[System.String]::Format("Looking at file: {0} with bitrate {1}", $_.Name, $songBitrate )
# Get the bit rate of the file.
if( $songBitrate -match '\d+' ) {
[int]$bitrate = $matches[0];
if( $bitrate -gt $maximumBitRate ) {
#Create the file processing string, note that my Lame is deployed into the public applications folder, this allows me to run the script from all accounts
$expression = "$lameExecutable --resample 22.05 -m m -b 48";
# include -B$maximumBitRate in the call to Lame??-V2 should give atotal average around 190 Kbps, but since we trust the shell object it's only looking at the average of the first few seconds
# if needed to use a fixed bitrate use: -b192 instead of -V2
if (![System.String]::IsNullOrWhiteSpace($songTitle)) {
$songTitle = $songTitle.Replace("""","'");
$expression += " --tt ""$songTitle""";
}
if (![System.String]::IsNullOrWhiteSpace($songArtist)) {
$songArtist = $songArtist.Replace("""","'");
$expression += " --ta ""$songArtist""";
}
if (![System.String]::IsNullOrWhiteSpace($songAlbum)) {
$songAlbum = $songAlbum.Replace("""","'");
$expression += " --tl ""$songAlbum""";
} else {
$expression += " --tl ""$albumTitle""";
}
if ($songYear -match '\d+') {
$expression += " --ty $songYear";
}
if ($songTrack -match '\d+') {
$expression += " --tn $songTrack ";
}
if (![System.String]::IsNullOrWhiteSpace($songGenre)) {
$songGenre = $songGenre.Replace("""","'");
$expression += " --tg ""$songGenre""";
} else {
$expression += " --tg ""$genreName""";
}
$expression += " --ignore-tag-errors ""$currentFileName"" ""$newFileName""";
#just to see what is going on??$expression;
# rename the file
#Rename-Item -LiteralPath $currentFileName -NewName $newFileName
# process the file
Invoke-Expression -Command $expression
Move-Item -Force "$newFileName" "$currentFileName"
# wait for a keypress
# Write-Host "Press any key to continue ..."
# $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
}