From 136c44df41210b2b4286ae0eaf12e9f709aa3ebf Mon Sep 17 00:00:00 2001 From: Brian Date: Thu, 12 Mar 2020 17:15:05 -0400 Subject: [PATCH] v4.1.1 (#62) Closes #58 - Using out_path now preserves media_path directory structure in the out_path. - Using out_path will now delete empty directories in the media_path, but will not delete the media_path itself (this avoids leaving behind a bunch of empty directories for files that were successfully converted and moved to out_path). This will leave directories containing failed conversions or corrupt files untouched in media_path for manual troubleshooting. - The out_path directory is now validated in the pre-flight script, with proper error logging. --- conv2mp4-ps.ps1 | 44 ++++++++++++++++++------- files/func/validate/ValidateOutPath.ps1 | 17 ++++++++++ files/init/preflight.ps1 | 5 +++ files/prop/properties | 2 +- 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 files/func/validate/ValidateOutPath.ps1 diff --git a/conv2mp4-ps.ps1 b/conv2mp4-ps.ps1 index ed8850d..af0205a 100644 --- a/conv2mp4-ps.ps1 +++ b/conv2mp4-ps.ps1 @@ -1,5 +1,5 @@ <#====================================================================================================================== -conv2mp4-ps v4.1 - https://github.com/BrianDMG/conv2mp4-ps +conv2mp4-ps v4.1.1 - https://github.com/BrianDMG/conv2mp4-ps This Powershell script will recursively search through a user-defined file path and convert all videos of user-specified include_file_types to MP4 with H264 video and AAC audio using ffmpeg. If a conversion failure is detected, the script re-encodes @@ -42,21 +42,21 @@ ForEach ($file in $fileList) { $title = $file.BaseName - $sourceFile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension; + $sourceFile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension + + $fileSubDirs = ($file.DirectoryName).Substring($cfg.media_path.Length, ($file.DirectoryName).Length - $cfg.media_path.Length) - $fileSubDirs = ($file.DirectoryName).Substring($cfg.media_path.Length, ($file.DirectoryName).Length - $cfg.media_path.Length); If ($cfg.use_out_path) { - $cfg.out_path = $baseout_path + $fileSubDirs; + $targetPath = $targetFile = $cfg.out_path + $fileSubDirs + "\" - If (-Not (Test-Path $cfg.out_path)) { - mkdir $cfg.out_path + If (-Not (Test-Path $targetPath)) { + mkdir $targetPath -Force } - $targetFile = $cfg.out_path + "\" + $file.BaseName + "_NEW" + ".mp4"; - Log "out_path = $($cfg.out_path)" + $targetFile = $targetPath + $file.BaseName + "_NEW" + ".mp4" } Else { - $targetFile = $file.DirectoryName + "\" + $file.BaseName + "_NEW" + ".mp4"; + $targetFile = $file.DirectoryName + "\" + $file.BaseName + "_NEW" + ".mp4" } $progress = ($(@($fileList).indexOf($file)+1) / $fileList.Count) * 100 @@ -68,10 +68,23 @@ ForEach ($file in $fileList) { Log "$($time.Invoke()) Processing - $sourceFile" Log "$($time.Invoke()) File $(@($fileList).indexOf($file)+1) of $($fileList.Count) - Total queue $progress%" + #Set targetFile final name + If ($cfg.use_out_path) { + $targetFileRenamed = $targetPath + $file.BaseName + ".mp4" + + #If using out_path, create target path if it doesn't exist + If ($cfg.use_out_path) { + If (-Not (Test-Path $targetPath)) { + mkdir $$targetPath -Force + } + } + } + Else { + $targetFileRenamed = $file.DirectoryName + "\" + $file.BaseName + ".mp4" + } + <#Test if $targetFile (.mp4) already exists, if yes then delete $sourceFile (.mkv) This outputs a more specific log message acknowleding the file already existed.#> - $targetFileRenamed = $file.DirectoryName + "\" + $file.BaseName + ".mp4" - If ((Test-Path $targetFileRenamed) -And $file.Extension -ne ".mp4") { Remove-Item $sourceFile -Force Log "$($time.Invoke()) Already exists: $targetFileRenamed" @@ -191,12 +204,19 @@ ForEach ($file in $fileList) { $targetFileRenamed = "$targetFile" -replace "_NEW","" Move-Item $targetFile $targetFileRenamed + #If using out_path, delete empty source directories + If ($cfg.use_out_path) { + If ($Null -eq (Get-ChildItem -Force $file.DirectoryName) -AND $file.DirectoryName -ne $cfg.media_path) { + Remove-Item $file.DirectoryName + } + } + } Else { Log "$($time.Invoke()) MP4 already compliant." If ($cfg.use_ignore_list) { Log "$($time.Invoke()) Added file to ignore list." - $fileToIgnore = $file.BaseName + $file.Extension; + $fileToIgnore = $file.BaseName + $file.Extension AddToIgnoreList "$($fileToIgnore)" } } diff --git a/files/func/validate/ValidateOutPath.ps1 b/files/func/validate/ValidateOutPath.ps1 new file mode 100644 index 0000000..11f23bb --- /dev/null +++ b/files/func/validate/ValidateOutPath.ps1 @@ -0,0 +1,17 @@ +Function ValidateOutPath { + + param( + [String]$Path + ) + + If (-Not (Test-Path $Path)) { + Try { + mkdir $Path -Force + } + Catch { + Log "Could not create $Path. Aborting script." + DeleteLockFile + Exit + } + } +} \ No newline at end of file diff --git a/files/init/preflight.ps1 b/files/init/preflight.ps1 index f059ae7..cceaae7 100644 --- a/files/init/preflight.ps1 +++ b/files/init/preflight.ps1 @@ -24,6 +24,11 @@ ValidateHandbrakeCLIPath -Path $cfg.handbrakecli_bin_dir #Validate media_path Validatemedia_path -Path $cfg.media_path +#Validate OutPath +If ($cfg.use_out_path) { + ValidateOutPath -Path $cfg.out_path +} + #Validate config booleans ValidateConfigBooleans diff --git a/files/prop/properties b/files/prop/properties index 1d05387..019957b 100644 --- a/files/prop/properties +++ b/files/prop/properties @@ -1,5 +1,5 @@ #Version -version=4.1 +version=4.1.1 platform=ps #URLs