Skip to content

Instantly share code, notes, and snippets.

@dimon4ezzz
Last active October 13, 2022 08:12
Show Gist options
  • Save dimon4ezzz/be4e5731a3641f6283fc5f0e16de4f83 to your computer and use it in GitHub Desktop.
Save dimon4ezzz/be4e5731a3641f6283fc5f0e16de4f83 to your computer and use it in GitHub Desktop.
PowerShell script, that convert mp3 files to aac (m4a) with ffmpeg. Can be verbose and can remove old mp3 files.
# Thanks to slhck: https://superuser.com/a/370637
# Thanks to Tim Van Wassenhove: https://timvw.be/2010/11/20/convert-aacm4a-files-to-mp3-with-vlc-and-powershell/
# Test: ffmpeg-mp3-to-aac -Verbose -RemoveOld
# Test: ffmpeg-mp3-to-aac -RemoveOld
# Test: ffmpeg-mp3-to-aac
Param (
[Parameter(Mandatory=$false, HelpMessage="Does the script remove old mp3 file")]
[switch]
$RemoveOld
)
# Clear hosted shell
# Очищаем окно консоли
Clear-Host;
# Check russian locale in Windows
# Проверяем, русский ли язык в Windows главный
$RU = (Get-WinUserLanguageList)[0].LanguageTag -eq "ru";
# Set ffmpeg location, please place ffmpeg to location with this script \ffmpeg
# Задаём расположение ffmpeg. Положи ffmpeg в папку со скриптом \ffmpeg
$ffmpeg = "$(Get-Location)\ffmpeg\bin\ffmpeg.exe";
# $ffmpeg = "$env:PROGRAMFILES\ffmpeg\bin\ffmpeg.exe";
# Set music location, please place all audio files to location with this script \music
# Задаём расположение музыки. Положи музыку в папку со скриптом \music
$dir = "$(Get-Location)\music";
# $dir = "$env:HOMEPATH\Music";
# Check if verbose was given, then ffmpeg will show every message
# Проверяем установлен ли аргумент Verbose, чтобы ffmpeg отображал все сообщения
$ffmpegVerbose = $PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent;
# Test ffmpeg avaliable
# Проверяем есть ли ffmpeg в этой папке
if (!(Test-Path $ffmpeg)) {
if ($RU) {
Write-Host "ffmpeg не найден по адресу $ffmpeg";
} else {
Write-Host "Cannot find ffmpeg in $ffmpeg";
}
# Terminate the script if ffmpeg not found
# Завершить скрипт, если ffmpeg не найден
exit;
}
# Test music directory avaliable
# Проверяем, есть ли папка с музыкой
if (!(Test-Path $dir)) {
if ($RU) {
Write-Host "Папка с музыкой не найдена в $dir";
} else {
Write-Host "Cannot directory for music in $dir";
}
# Terminate the script if music not found
# Завершить скрипт, если папка с музыкой не найдена
exit;
}
# Convert any supported audio to aac (m4a)
# Конвертирует все поддерживаемые форматы аудио в aac (m4a)
function ConvertToM4A([switch] $inputObject) {
PROCESS {
$oldFile = $_;
# Change old extension to m4a (aac)
# Меняем старое расширение файла на m4a (aac)
$newFile = $oldFile.FullName.Replace($oldFile.Extension, ".m4a");
# If Verbose parameter was given, show all messages from ffmpeg
# Если параметр Verbose был задан, показывать все сообщения от ffmpeg
if ($ffmpegVerbose) {
&"$ffmpeg" -loglevel trace -i $oldFile -c:a aac -b:a 192k "$newFile" | Out-Null;
} else {
&"$ffmpeg" -loglevel panic -hide_banner -i $oldFile -c:a aac -b:a 192k "$newFile" | Out-Null;
}
Write-Verbose "$oldFile is done";
# Remove old file if set in parameter
# Удаляет старый mp3 файл, если задано в аргументе
if ($RemoveOld) {
Remove-Item $oldFile;
Write-Verbose "$oldFile was removed";
}
}
}
# Converting each mp3 file to m4a (aac). Hardcoded mp3 extension, change it if you want
# Все файлы mp3 в этой папке конвертируем в m4a (aac). Если нужно, смени формат файла
Get-ChildItem "$dir\*" -Recurse -Include *.mp3 | ConvertToM4A;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment