Skip to content

Commit

Permalink
v4.1.1 (#62)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BrianDMG authored Mar 12, 2020
1 parent ad48031 commit 136c44d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
44 changes: 32 additions & 12 deletions conv2mp4-ps.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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)"
}
}
Expand Down
17 changes: 17 additions & 0 deletions files/func/validate/ValidateOutPath.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
5 changes: 5 additions & 0 deletions files/init/preflight.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion files/prop/properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Version
version=4.1
version=4.1.1
platform=ps

#URLs
Expand Down

0 comments on commit 136c44d

Please sign in to comment.