Skip to content

Instantly share code, notes, and snippets.

@khaosx
Last active December 15, 2020 21:24
Show Gist options
  • Save khaosx/e6a7ea8b47b8e716ba8e7a7d30848597 to your computer and use it in GitHub Desktop.
Save khaosx/e6a7ea8b47b8e716ba8e7a7d30848597 to your computer and use it in GitHub Desktop.
Prepare media for transcoding using Don Melton's Other Video Transcoding
# sanitize-media.ps1
#
# Copyright 2020 - K. Austin Newman
# Version: 1.0.5
# Prepare media for transcoding using Don Melton's Other Video Transcoding
#
# Requirements: ffprobe,mkvmerge,remux.rb,jq,mkvpropedit,filebot
# Define user vars
$dirWorkflowRoot = "workflows"
$dirWorkflowName = "transcoding"
# Define Functions
function findDriveLetterForVolume {
param ($volumeName)
try {
$drvData = get-volume -FileSystemLabel "$volumeName" -ErrorAction Stop
$script:strRootVol = $drvData.DriveLetter
}
catch {
Write-warning "Volume $volumeName not available"
exit 1
}
}
function buildAndVerifyWorkingDirectories {
$script:dirWorkFlow = "$strRootVol`:\$dirWorkflowRoot\$dirWorkflowName"
$script:dirInbox = "$dirWorkFlow\inbox"
$script:dirSanitize = "$dirWorkFlow\sanitized"
$script:dirReady = "$dirWorkFlow\ready"
$script:dirOutbox = "$dirWorkFlow\outbox"
$script:dirManual = "$dirWorkFlow\manual"
$script:dirLogs = "$dirWorkFlow\encodingLogs"
$aPaths = $dirWorkFlow,$dirInbox,$dirSanitize,$dirReady,$dirOutbox,$dirManual
foreach ( $dir in $aPaths ) {
New-Item -ItemType Directory -Force -Path $dir -ErrorAction SilentlyContinue
}
}
function verifyAllToolRequirements {
$aTools = "ffprobe","remux","mkvmerge","jq","mkvpropedit","filebot"
foreach ( $tool in $aTools ) {
if ( $null -eq ( Get-Command "$tool" -ErrorAction SilentlyContinue )) {
Write-warning -Message "Executable not in path: $tool"
$strExit = $true
}
if ( $strExit -eq $true ) {
Write-warning "Exiting due to previous errors"
exit 1
}
}
}
# Begin Processing
## Define immutable vars
$strTVRegEx = "([sS]([0-9]{2,}|[X]{2,})[eE]([0-9]{2,}|[Y]{2,}))"
$dirStartingLoc = Get-Location
## Verify all paths and tools
findDriveLetterForVolume "Data"
buildAndVerifyWorkingDirectories
verifyAllToolRequirements
## Convert m2ts files to mkv
$fm2tsArray = Get-ChildItem -recurse $dirInbox -include *.m2ts -File
foreach ( $element in $fm2tsArray ) {
$strTheFile = $element.ToString()
$strFilename = (Get-Item $strTheFile).Basename
write-host "Converting $strFilename to mkv"
ffmpeg -i "$strTheFile" -c:v copy -c:s copy -c:a copy "$dirInbox\$strFilename.mkv"
remove-item -force $strTheFile
}
## Sanitize all MKV files
$fPrepArray = Get-ChildItem -recurse $dirInbox -include *.mkv -File
foreach ( $element in $fPrepArray ) {
$strTheFile = $element.ToString()
$strFilename = (Get-Item $strTheFile).Basename
write-host "Starting on file $strFilename"
if ( Test-Path $dirLogs\$strFilename.mkv.log ) {
write-warning "$strFilename has been transcoded recently - Skipping"
continue
}
$strTitles = ffprobe -i "$strTheFile" -select_streams s -show_streams -of json -v quiet | jq -r '.streams[] | .tags | .title'
$strLanguage = ffprobe -i "$strTheFile" -select_streams s -show_streams -of json -v quiet | jq -r '.streams[] | .tags | .language'
if ( $strLanguage -notcontains "eng" ) {
write-warning "$strTheFile has no subs. Moving to manual folder."
Move-Item -Path "$strTheFile" -Destination "$dirManual\$strFilename-NoEngSubs.mkv"
continue
}
if ( $strTitles -match "[Ss][Dd][Hh]" ) {
$flgSDHCheck = $true
Write-warning "$strFilename contains at least one SDH subtitle"
}
$tracktoedit = 0
for ($i=0; $i -le $strLanguage.count; $i++) {
$strTitle = ffprobe -i "$strTheFile" -select_streams s -show_streams -of json -v quiet | jq -r ".streams[$i] | .tags | .title"
$tracktoedit++
if ( $strTitle -match "[Ff][Oo][Rr][Cc][Ee][Dd]" ) {
& mkvpropedit --edit track:s$tracktoedit --set flag-forced=1 "$strTheFile" 2>&1 | Out-Null
}
if ( $flgSDHCheck = $true `
-And ! $strTitle -eq "null" `
-And $strTitle -notmatch "[Ss][Dd][Hh]" `
-And $strTitle -notmatch "[Ff][Oo][Rr][Cc][Ee][Dd]" `
-And $strTitle -notmatch "[Cc][Oo][Mm][Mm][Ee][Nn][Tt][Aa][Rr][Yy]" `
-And $strTitle -notmatch "[Ee][Nn][Gg][Ll][Ii][Ss][Hh][ ][Dd][Uu][Bb]" ) {
write-warning "Non-SDH English track `"$strTitle`" detected. Removing sub track $tracktoedit."
& mkvpropedit --edit track:s$tracktoedit --delete name "$strTheFile" 2>&1 | Out-Null
}
}
### Sanitize video track
write-host Sanitizing video track title
& mkvpropedit --edit track:v1 --delete name --delete language "$strTheFile" 2>&1 | Out-Null
#& mkvpropedit --edit track:v1 --delete language "$strTheFile" 2>&1 | Out-Null
### Rename file with FileBot
If ($strTheFile -match $strTVRegEx){
& filebot -rename "$strTheFile" --db TheTVDB --format "$dirSanitize\{n} - {s00e00} - {t}" -non-strict 2>&1 | Out-Null
} else {
& filebot -rename "$strTheFile" --db TheMovieDB --format "$dirSanitize\{n.colon(' - ')} ({y})" -non-strict 2>&1 | Out-Null
}
write-host Completed file $strFilename
}
## Remux all MKV files
Set-Location -Path $dirReady
$fTrimArray = Get-ChildItem -recurse $dirSanitize -include *.mkv -File
foreach ( $element in $fTrimArray ) {
$strTheFile = $element.ToString()
$strFilename = (Get-Item $strTheFile).Basename
write-host "Remuxing $strFilename"
& mkvpropedit "$strTheFile" --edit info --set "title=$strFilename" 2>&1 | Out-Null
& remux.rb --add-title --add-audio "english dub" --prefer-sdh $strTheFile 2>&1 | Out-Null
if ( $LASTEXITCODE -eq 0 ) {
write-host $strFilename is complete. Removing original file.
Remove-Item $strTheFile
}
else {
write-warning "$strFilename had errors. Moving to issues folder."
Move-Item -Path "$strTheFile" -Destination "$dirManual\$strFilename-BadRemux.mkv"
}
}
Set-Location -Path $dirStartingLoc
Write-Output "No files remain to be processed. Exiting..."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment