diff --git a/conv2mp4-ps.ps1 b/conv2mp4-ps.ps1 index 10ad063..d2c50f0 100644 --- a/conv2mp4-ps.ps1 +++ b/conv2mp4-ps.ps1 @@ -1,5 +1,5 @@ <#================================================================================== -conv2mp4 - https://github.com/BrianDMG/conv2mp4-ps v1.3 +conv2mp4 - https://github.com/BrianDMG/conv2mp4-ps v1.4 This Powershell script will recursively search through a defined file path and convert all MKV, AVI, FLV, and MPEG files to MP4 using ffmpeg + audio to AAC. If it @@ -8,7 +8,6 @@ It then refreshes a Plex library, and upon conversion success deletes the source (original) file. The purpose of this script is to reduce transcodes from Plex. ===================================================================================== -This script requires ffmpeg and Handbrake. You can download them at the URLs below. ffmpeg : https://ffmpeg.org/download.html handbrakecli : https://handbrake.fr/downloads.php @@ -16,9 +15,9 @@ handbrakecli : https://handbrake.fr/downloads.php User-specific variables ------------------------------------------------------------------------------------- $mediaPath = the path to the media you want to convert -NOTE: to use mapped drive, you must run "net use z: \\server\share /persistent:yes" as the user you're going to run the script as (generally Administrator) prior to running the script +NOTE: to use mapped drive, you must run "net use z: \\server\share /persistent:yes" as the user you're going to run the script as (generally Administrator) prior to running the script. You can also use a UNC path here. $fileTypes = the extensions of the files you want to convert in the format "*.ex1", "*.ex2" -$log = path you want the log file to save to. defaults to your desktop. +$logPath = path you want the log file to save to. defaults to your desktop. $plexIP = the IP address and port of your Plex server (for the purpose of refreshing its library) $plexToken = your Plex server's token (for the purpose of refreshing its library). -Plex server token - See https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token @@ -26,10 +25,10 @@ $plexToken = your Plex server's token (for the purpose of refreshing its library $ffmpeg = path to ffmpeg.exe $handbrake = path to HandBrakeCLI.exe #> $mediaPath = "Z:\media" -$fileTypes = "*.mkv", "*.avi", "*.flv", "*.mpeg" -$log = "C:\Users\$env:username\Desktop\conv2mp4-ps.log" -$plexIP = 'yourplexip:32400' -$plexToken = 'yourplextoken' +$fileTypes = "*.mkv", "*.avi", "*.flv", "*.mpeg", "*.ts" +$logPath = "C:\Users\$env:username\Desktop\conv2mp4-ps.log" +$plexIP = 'plexip:32400' +$plexToken = 'plextoken' $ffmpeg = "C:\ffmpeg\bin\ffmpeg.exe" $handbrake = "C:\Program Files\HandBrake\HandBrakeCLI.exe" @@ -56,124 +55,161 @@ ForEach ($file in $fileList) $progress = [Math]::Round($progress,2) <#---------------------------------------------------------------------------------- - Do work + Begin logging output ----------------------------------------------------------------------------------#> Clear-Host - Write-Output "------------------------------------------------------------------------------------" | Out-File $log -Append - Write-Output "$($time.Invoke()) Processing - $oldFile" | Out-File $log -Append - Write-Output "$($time.Invoke()) File $i of $fileCount - $progress%" | Out-File $log -Append + Write-Output "------------------------------------------------------------------------------------" | Out-File $logPath -Append + Write-Output "$($time.Invoke()) Processing - $oldFile" | Out-File $logPath -Append + Write-Output "$($time.Invoke()) File $i of $fileCount - $progress%" | Out-File $logPath -Append - <#---------------------------------------------------------------------------------- - ffmpeg variables - ----------------------------------------------------------------------------------#> - $ffarg1 = "-n" - $ffarg2 = "-fflags" - $ffarg3 = "+genpts" - $ffarg4 = "-i" - $ffarg5 = "$oldFile" - $ffarg6 = "-vcodec" - $ffarg7 = "copy" - $ffarg8 = "-acodec" - $ffarg9 = "aac" - $ffarg10 = "$newFile" - $ffargs = @($ffarg1, $ffarg2, $ffarg3, $ffarg4, $ffarg5, $ffarg6, $ffarg7, $ffarg8, $ffarg9, $ffarg10) - $ffcmd = &$ffmpeg $ffargs - - <#---------------------------------------------------------------------------------- - Begin ffmpeg conversion (lossless) - -----------------------------------------------------------------------------------#> - $ffcmd - Write-Output "$($time.Invoke()) ffmpeg completed" | Out-File $log -Append - - <#---------------------------------------------------------------------------------- - Refresh Plex libraries in Chrome - -----------------------------------------------------------------------------------#> - Invoke-WebRequest $plexURL - Write-Output "$($time.Invoke()) Plex library refreshed" | Out-File $log -Append #> + <#---------------------------------------------------------------------------------- + ffmpeg arguments + ----------------------------------------------------------------------------------#> + $ffArg1 = "-n" + $ffArg2 = "-fflags" + $ffArg3 = "+genpts" + $ffArg4 = "-i" + $ffArg5 = "$oldFile" + $ffArg6 = "-map" + $ffArg7 = "0" + $ffArg8 = "-c:v" + $ffArg9 = "copy" + $ffArg10 = "-c:a" + $ffArg11 = "aac" + $ffArg12 = "-c:s" + $ffArg13 = "mov_text" + $ffArg14 = "$newFile" + $ffArgs = @($ffArg1, $ffArg2, $ffArg3, $ffArg4, $ffArg5, $ffArg6, $ffArg7, $ffArg8, $ffArg9, $ffArg10, $ffArg11, $ffArg12, $ffArg13, $ffArg14) + $ffCMD = &$ffmpeg $ffArgs - <#---------------------------------------------------------------------------------- - Begin file comparison between old file and new file to determine conversion success - -----------------------------------------------------------------------------------#> - # Load files for comparison - $fileOld = Get-Item $oldFile - $fileNew = Get-Item $newFile + <#---------------------------------------------------------------------------------- + Begin ffmpeg conversion (lossless) + -----------------------------------------------------------------------------------#> + $ffCMD + Write-Output "$($time.Invoke()) ffmpeg completed" | Out-File $logPath -Append + + <#---------------------------------------------------------------------------------- + Refresh Plex libraries in Chrome + -----------------------------------------------------------------------------------#> + Invoke-WebRequest $plexURL + Write-Output "$($time.Invoke()) Plex library refreshed" | Out-File $logPath -Append + + <#---------------------------------------------------------------------------------- + Begin file comparison between old file and new file to determine conversion success + -----------------------------------------------------------------------------------#> + # Load files for comparison + $fileOld = Get-Item $oldFile + $fileNew = Get-Item $newFile + $conDelOld = Test-Path $oldFile + $conDelNew = Test-Path $newFile - # If new file is the same size as old file, log status and delete old file - If ($fileNew.length -eq $fileOld.length) + # If new file is the same size as old file, log status and delete old file + If ($fileNew.length -eq $fileOld.length) { Remove-Item $oldFile -Force - Write-Output "$($time.Invoke()) Same file size ($($fileNew.length)MB). $oldFile deleted." | Out-File $log -Append + + # Test to see if old file was deleted. If not, tries again. + If ($confDelOld -eq $False) + { + Write-Output "$($time.Invoke()) Same file size. $oldFile deleted." | Out-File $logPath -Append + } + Else + { + Remove-Item $oldFile -Force + Write-Output "$($time.Invoke()) Same file size. $oldFile deleted." | Out-File $logPath -Append + } } - - # If new file is larger than old file, log status and delete old file - Elseif ($fileNew.length -gt $fileOld.length) + + # If new file is larger than old file, log status and delete old file + Elseif ($fileNew.length -gt $fileOld.length) { $diffGT = [Math]::Round($fileNew.length-$fileOld.length)/1MB -as [int] + Remove-Item $oldFile -Force - Write-Output "$($time.Invoke()) New file is $($diffGT)MB larger. $oldFile deleted." | Out-File $log -Append + + If ($confDelOld -eq $False) + { + Write-Output "$($time.Invoke()) New file is $($diffGT)MB larger. $oldFile deleted." | Out-File $logPath -Append + } + Else + { + Remove-Item $oldFile -Force + Write-Output "$($time.Invoke()) New file is $($diffGT)MB larger. $oldFile deleted." | Out-File $logPath -Append + } } - - # If new file is much smaller than old file (indicating a failed conversion), log status, delete new file, and re-encode with HandbrakeCLI - Elseif ($fileNew.length -lt ($fileOld.length * .75) ) + + # If new file is much smaller than old file (indicating a failed conversion), log status, delete new file, and re-encode with HandbrakeCLI + Elseif ($fileNew.length -lt ($fileOld.length * .75) ) { $diffErr = [Math]::Round($fileNew.length-$fileOld.length)/1MB -as [int] + Remove-Item $newFile -Force - Write-Output "$($time.Invoke()) EXCEPTION: New file is over 25% smaller ($($diffErr)MB). $newFile deleted." | Out-File $log -Append - Write-Output "$($time.Invoke()) FAILOVER: Re-encoding $oldFile with Handbrake." | Out-File $log -Append - # Begin Handbrake encode (lossy) - # Handbrake CLI: https://trac.handbrake.fr/wiki/CLIGuide#presets - <#---------------------------------------------------------------------------------- - HandbrakeCLI variables - ----------------------------------------------------------------------------------#> - $hbarg1 = "-i" - $hbarg2 = "$oldFile" - $hbarg3 = "-o" - $hbarg4 = "$newFile" - $hbarg5 = "-f" - $hbarg6 = "mp4" - $hbarg7 = "--loose-anamorphic" - $hbarg8 = "--modulus" - $hbarg9 = "2" - $hbarg10 = "-e" - $hbarg11 = "x264" - $hbarg12 = "-q" - $hbarg13 = "19" - $hbarg14 = "--cfr" - $hbarg15 = "-a" - $hbarg16 = "1" - $hbarg17 = "-E" - $hbarg18 = "faac" - $hbarg19 = "-6" - $hbarg20 = "dp12" - $hbarg21 = "-R" - $hbarg22 = "Auto" - $hbarg23 = "-B" - $hbarg24 = "320" - $hbarg25 = "-D" - $hbarg26 = "0" - $hbarg27 = "--gain" - $hbarg28 = "0" - $hbarg29 = "--audio-copy-mask" - $hbarg30 = "none" - $hbarg31 = "--audio-fallback" - $hbarg32 = "ffac3" - $hbarg33 = "-x" - $hbarg34 = "level=4.0:ref=16:bframes=16:b-adapt=2:direct=auto:me=tesa:merange=24:subq=11:rc-lookahead=60:analyse=all:trellis=2:no-fast-pskip=1" - $hbarg35 = "--verbose=1" - $hbargs = @($hbarg1, $hbarg2, $hbarg3, $hbarg4, $hbarg5, $hbarg6, $hbarg7, $hbarg8, $hbarg9, $hbarg10, $hbarg11, $hbarg12, $hbarg13, $hbarg14, $hbarg15, $hbarg16, $hbarg17, $hbarg18, $hbarg19, $hbarg20, $hbarg21, $hbarg22, $hbarg23, $hbarg24, $hbarg25, $hbarg26, $hbarg27, $hbarg28, $hbarg29, $hbarg30, $hbarg31, $hbarg32, $hbarg33, $hbarg34, $hbarg35) - $hbcmd = &$handbrake $hbargs - - $hbcmd - Write-Output "$($time.Invoke()) Handbrake finished." | Out-File $log -Append - Write-Output "$($time.Invoke()) $oldFile deleted." | Out-File $log -Append - } + If ($confDelNew -eq $False) + { + Write-Output "$($time.Invoke()) EXCEPTION: New file is over 25% smaller ($($diffErr)MB). $newFile deleted." | Out-File $logPath -Append + Write-Output "$($time.Invoke()) FAILOVER: Re-encoding $oldFile with Handbrake." | Out-File $logPath -Append + } + Else + { + Remove-Item $newFile -Force + Write-Output "$($time.Invoke()) EXCEPTION: New file is over 25% smaller ($($diffErr)MB). $newFile deleted." | Out-File $logPath -Append + Write-Output "$($time.Invoke()) FAILOVER: Re-encoding $oldFile with Handbrake." | Out-File $logPath -Append + + # Begin Handbrake encode (lossy) + # Handbrake CLI: https://trac.handbrake.fr/wiki/CLIGuide#presets + + <#---------------------------------------------------------------------------------- + HandbrakeCLI arguments + ----------------------------------------------------------------------------------#> + $hbArg1 = "-i" + $hbArg2 = "$oldFile" + $hbArg3 = "-o" + $hbArg4 = "$newFile" + $hbArg5 = "-f" + $hbArg6 = "mp4" + $hbArg7 = "-a" + $hbArg8 = "1,2,3,4,5,6,7,8,9,10" + $hbArg9 = "--subtitle" + $hbArg10 = "scan,1,2,3,4,5,6,7,8,9,10" + $hbArg11 = "--x264-profile" + $hbArg12 = "high" + $hbArg13 = "--verbose=1" + $hbArgs = @($hbArg1, $hbArg2, $hbArg3, $hbArg4, $hbArg5, $hbArg6, $hbArg7, $hbArg8, $hbArg9, $hbArg10, $hbArg11, $hbArg12, $hbArg13) + $hbCMD = &$handbrake $hbArgs - # If new file is smaller than old file, log status and delete old file - Elseif ($fileNew.length -lt $fileOld.length) + <#---------------------------------------------------------------------------------- + Begin HandbrakeCLI conversion (lossy) with supplied arguments + -----------------------------------------------------------------------------------#> + $hbCMD + Write-Output "$($time.Invoke()) Handbrake finished." | Out-File $logPath -Append + Remove-Item $oldFile -Force + If ($confDelOld -eq $False) + { + Write-Output "$($time.Invoke()) $oldFile deleted." | Out-File $logPath -Append + } + Else + { + Remove-Item $oldFile -Force + Write-Output "$($time.Invoke()) $oldFile deleted." | Out-File $logPath -Append + } + } + } + # If new file is smaller than old file, log status and delete old file + Elseif ($fileNew.length -lt $fileOld.length) { $diffLT = [Math]::Round($fileOld.length-$fileNew.length)/1MB -as [int] Remove-Item $oldFile -Force - Write-Output "$($time.Invoke()) New file is $($diffLT)MB smaller. $oldFile deleted." | Out-File $log -Append - } -} \ No newline at end of file + + If ($confDelOld -eq $False) + { + Write-Output "$($time.Invoke()) New file is $($diffLT)MB smaller. $oldFile deleted." | Out-File $logPath -Append + } + Else + { + Remove-Item $oldFile -Force + Write-Output "$($time.Invoke()) New file is $($diffLT)MB smaller. $oldFile deleted." | Out-File $logPath -Append + + } + } +} \ No newline at end of file