1+ param (
2+ [string ]$ext = ' m4a' ,
3+ [int32 ]$jobs ,
4+ [switch ]$h ,
5+ [switch ]$r
6+ )
7+
8+ if ($jobs -eq 0 ) {
9+ if ($IsWindows ) {
10+ $jobs = (Get-CimInstance - ClassName ' Win32_ComputerSystem' ).NumberOfLogicalProcessors
11+ } elseif ($IsMacOS ) {
12+ $jobs = sysctl - n hw.ncpu
13+ } else {
14+ $jobs = grep.exe - c ^processor / proc/ cpuinfo
15+ }
16+ }
17+
18+ if ($h ) {
19+ Write-Output " Converts [$ext ] files from `$ pwd recursivly to flac using CUETool's Flaccl"
20+ Write-Output " Requires CUETools.FLACCL.cmd.exe and ffmpeg to be in env:path"
21+ Write-Output " flaccl can only handle 24bit and 16 bit"
22+ Write-Output " 32bit and codecs without a bitdepth will be converted to 24bit"
23+ Write-Output " -ext [$ext ] for input file extension"
24+ Write-Output " -jobs [$jobs ] for the ammount of concurrent converts"
25+ exit
26+ }
27+
28+ $extension = (' *.' + $ext )
29+
30+ if ($r ) {
31+ $lscommands = ' Get-ChildItem -File -Recurse -filter $extension'
32+ } else {
33+ $lscommands = ' Get-ChildItem -File -filter $extension'
34+ }
35+
36+ Invoke-Expression $lscommands | ForEach-Object {
37+ $Check = $false
38+ while ($Check -eq $false ) {
39+ if ((Get-Job - State ' Running' ).Count -ne $jobs ) {
40+ Start-Job - ArgumentList $_ , $ext - ScriptBlock {
41+ param ($inputfile , $ext );
42+ if ($inputfile.Extension -ne ' .wav' ) {
43+ if ((ffprobe.exe - v error - select_streams a:0 - show_entries stream= bits_per_raw_sample - of default = noprint_wrappers= 1 :nokey= 1 - i $inputfile ) -eq " 16" ) {
44+ $bit = " 16"
45+ } else {
46+ $bit = " 24"
47+ }
48+ $tempfile = New-TemporaryFile
49+ ffmpeg.exe - y - threads 8 - loglevel fatal - i $inputfile.FullName - c:a (' pcm_s' + $bit + ' le' ) -f wav $tempfile.FullName
50+ Move-Item $tempfile.FullName $tempfile.FullName.Replace ($tempfile.Extension , ' .wav' )
51+ $tempi = Get-Item $tempfile.FullName.Replace ($tempfile.Extension , ' .wav' )
52+ } else {
53+ $tempi = Get-Item $inputfile.FullName
54+ }
55+ ffmpeg.exe - y - threads 8 - loglevel fatal - i $inputfile.FullName - map_metadata 0 - map_metadata:s:v 0 :s:v - map_metadata:s:a 0 :s:a -f ffmetadata $inputfile.FullName.Replace ($inputfile.Extension , ' .txt' )
56+ $tempfile1 = New-TemporaryFile
57+ Move-Item $tempfile1.FullName $tempfile1.FullName.Replace ($tempfile1.Extension , ' .flac' )
58+ CUETools.FLACCL.cmd.exe -8 -- cpu- threads 8 -- ignore- chunk- sizes - o $tempfile1.FullName.Replace ($tempfile1.Extension , ' .flac' ) $tempi.FullName
59+ ffmpeg.exe - y - threads 8 - loglevel fatal - i $tempfile1.FullName.Replace ($tempfile1.Extension , ' .flac' ) - i $inputfile.FullName.Replace ($inputfile.Extension , ' .txt' ) - map_metadata 1 - c copy $inputfile.FullName.Replace ($inputfile.Extension , ' .flac' )
60+ if (ffprobe.exe - v error - select_streams v:0 - show_entries stream= codec_name - of default = noprint_wrappers= 1 :nokey= 1 - i $inputfile ) {
61+ if ((ffprobe.exe - v error - select_streams v:0 - show_entries stream= codec_name - of default = noprint_wrappers= 1 :nokey= 1 - i $inputfile ) -eq " mjpeg" ) {
62+ $type = " .jpg"
63+ } else {
64+ $type = " .png"
65+ }
66+ ffmpeg.exe - i $inputfile - an - c:v copy - sn $inputfile.FullName.Replace ($inputfile.extension , $type )
67+ metaflac.exe -- import-picture - from $inputfile.FullName.Replace ($inputfile.extension , $type ) $inputfile.FullName.Replace ($inputfile.Extension , ' .flac' )
68+ Remove-Item $inputfile.FullName.Replace ($inputfile.extension , $type )
69+ }
70+ if ($inputfile.Extension -ne ' .wav' ) { Remove-Item $tempfile.FullName.Replace ($tempfile.Extension , ' .wav' ) }
71+ Remove-Item $inputfile.FullName.Replace ($inputfile.Extension , ' .txt' )
72+ Remove-Item $tempfile1.FullName.Replace ($tempfile1.Extension , ' .flac' )
73+ }
74+ $Check = $true
75+ }
76+ }
77+ Remove-Job - State Completed
78+ }
0 commit comments