Skip to content

Commit

Permalink
v2.1
Browse files Browse the repository at this point in the history
Addressed issue #5 - Added switch to allow the log to append new session
logs to previous session logs or overwrite previous session log with new
session log.

Fixed issue #6 - Correct file size no being updated when Handbrake
failover was encountered., causing Handbrake encodes to be deleted
regardless of encode success/failure. Now reports the correct file size
of the re-encoded file and only deletes the file again if it's actually
still too small.
  • Loading branch information
BrianDMG committed Apr 12, 2017
1 parent 70a9d5f commit 918536b
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions conv2mp4-ps.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#======================================================================================================================
conv2mp4-ps - https://github.com/BrianDMG/conv2mp4-ps v2.0 RELEASE
conv2mp4-ps - https://github.com/BrianDMG/conv2mp4-ps v2.1 RELEASE
This Powershell script will recursively search through a user-defined file path and convert all videos of user-specified
filetypes to MP4 with H264 video and AAC audio using ffmpeg. If a conversion failure is detected, the script re-encodes
Expand All @@ -15,7 +15,7 @@ User-defined variables
------------------------------------------------------------------------------------------------------------------------
$mediaPath = the path to the media you want to convert (no trailing "\")
NOTE: For network shares, use UNC path if you plan on running this script as a scheduled task.
----- If running manually and using a mapped drive, you must run "net use z: \\server\share /persistent:yes" as the user y
----- If running manually and using a 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.
$fileTypes = the extensions of the files you want to convert in the format "*.ex1", "*.ex2". Do NOT add .mp4!
$logPath = path you want the log file to save to. defaults to your desktop. (no trailing "\")
Expand All @@ -25,8 +25,9 @@ $plexToken = your Plex server's token (for the purpose of refreshing its librari
NOTE: Plex server token - See https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token
----- Plex server token is also easy to retrieve with PlexPy, Ombi, Couchpotato, or SickRage
$ffmpegBinDir = path to ffmpeg bin folder (no trailing "\"). This is the directory containing ffmpeg.exe and ffprobe.exe
$handbrake = path to Handbrake directory (no trailing "\"). This is the directory containing HandBrakeCLI.exe
$handbrakeDir = path to Handbrake directory (no trailing "\"). This is the directory containing HandBrakeCLI.exe
$script:garbage = the extensions of the files you want to delete in the format "*.ex1", "*.ex2"
$appendLog = $False will clear log at the beginning of every session, $True will append new session log to old session log
-----------------------------------------------------------------------------------------------------------------------#>
$mediaPath = "\\your\path\here"
$fileTypes = "*.mkv", "*.avi", "*.flv", "*.mpeg", "*.ts" #Do NOT add .mp4!
Expand All @@ -37,14 +38,26 @@ $plexToken = 'plextoken'
$ffmpegBinDir = "C:\ffmpeg\bin"
$handbrakeDir = "C:\Program Files\HandBrake"
$script:garbage = "*.nfo"
$appendLog = $False

<#----------------------------------------------------------------------------------
Static variables
----------------------------------------------------------------------------------#>
# Time and format used for timestamps in the log
$time = {Get-Date -format "MM/dd/yy HH:mm:ss"}
#Join-Path for log file
$log = Join-Path "$logPath" "$logName"
# Clear log contents
Clear-Content $log
#Should the log append or clear
If ($appendLog -eq $False)
{
# Clear log contents
Clear-Content $log
}
Else
{
Write-Output "`n`n------------------------------------------------------------------------------------" | Tee -filepath $log -append
Write-Output ">>>>> NEW SESSION (started $($time.Invoke()))" | Tee -filepath $log -append
}
# Print version information to top of log
Write-Output "`nconv2mp4-ps v2.0 - https://github.com/BrianDMG/conv2mp4-ps v2.0 RELEASE" | Tee -filepath $log -append
Write-Output "------------------------------------------------------------------------------------" | Tee -filepath $log -append
Expand Down Expand Up @@ -105,8 +118,6 @@ Static variables
$fileList = Get-ChildItem "$($mPath.FullName)\*" -i $fileTypes -recurse
$num = $fileList | measure
$fileCount = $num.count
# Time and format used for timestamps in the log
$time = {Get-Date -format "MM/dd/yy HH:mm:ss"}
# Initialize disk usage change to 0
$diskUsage = 0
# Initialize 'video length converted' to 0
Expand Down Expand Up @@ -195,9 +206,7 @@ Functions
}
# If new and old files are the same size
Function IfSame
{
$errOccured = $False

{
Try
{
Remove-Item $oldFile -Force -ErrorAction Stop
Expand All @@ -206,15 +215,13 @@ Functions
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: $oldFile could not be deleted. Full error below."
Log $_
}
}
# If new file is larger than old file
Function IfLarger
{
$errOccured = $False
$diffGT = [Math]::Round($fileNew.length-$fileOld.length)/1MB -as [int]

Try
Expand Down Expand Up @@ -256,15 +263,13 @@ Functions
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: $oldFile could not be deleted. Full error below."
Log $_
}
}
# If new file is smaller than old file
Function IfSmaller
{
$errOccured = $False
$diffLT = [Math]::Round($fileOld.length-$fileNew.length)/1MB -as [int]

Try
Expand Down Expand Up @@ -306,15 +311,13 @@ Functions
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: $oldFile could not be deleted. Full error below."
Log $_
}
}
## If new file is over 25% smaller than the original file, trigger encoding failure
Function FailureDetected
{
$errOccured = $False
{
$diffErr = [Math]::Round($fileNew.length-$fileOld.length)/1MB -as [int]

Try
Expand All @@ -325,7 +328,6 @@ Functions
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: $newFile could not be deleted. Full error below."
Log $_
}
Expand Down Expand Up @@ -491,15 +493,13 @@ Functions
$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)
$hbCMD = &$handbrake $hbArgs
# Begin Handbrake operation
$errOccured = $False
Try
{
$hbCMD
Log "$($time.Invoke()) Handbrake finished."
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: Handbrake has encountered an error."
Log $_
}
Expand Down Expand Up @@ -541,7 +541,6 @@ Functions
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: $turd could not be deleted. Full error below."
Log $_
}
Expand Down Expand Up @@ -674,19 +673,23 @@ Begin search loop
----------------------------------------------------------------------------------#>
EncodeHandbrake

# Load files for comparison
$fileOld = Get-Item $oldFile
$fileNew = Get-Item $newFile

# If new file is much smaller than old file (likely because the script was aborted re-encode), leave original file alone and print error
If ($fileNew.length -lt ($fileOld.length * .75))
{
$errOccured = $False
$diffErr = [Math]::Round($fileNew.length-$fileOld.length)/1MB -as [int]
Try
{
Remove-Item $newFile -Force -ErrorAction Stop
Log "ERROR: New file was too small. Deleted newfile and retained $oldFile."
Log "$($time.Invoke()) ERROR: New file was too small ($($diffErr)MB)."
Log "$($time.Invoke()) Deleted new file and retained $oldFile."
}
Catch
{
$errOccured = $True
Log "$($time.Invoke()) ERROR: New file was too small. Retained $oldFile."
Log "$($time.Invoke()) ERROR: New file was too small ($($diffErr)MB). Retained $oldFile."
Log "$($time.Invoke()) ERROR: $newFile could not be deleted. Full error below."
Log $_
}
Expand Down

0 comments on commit 918536b

Please sign in to comment.