Code: Select all
$TargetDirectory="E:\Make\Iron Man- Armored Adventures"
$TempDir="D:\Source\"
$Debug=$false
$OldFolder=$TargetDirectory + "\Old"
if (Get-Item -Path $OldFolder -ErrorAction SilentlyContinue)
{
"Old Directory Already Exists."
}
else
{
New-Item -Path $OldFolder -ItemType "directory"
}
function Get_Track
{
param([string]$InfoLine = "File Blank")
$InfoLine.Remove($InfoLine.IndexOf(' '))
}
function Get_Field
{
param([string]$InfoLine = "File Blank")
$Get_Field_Temp=$InfoLine.Remove(0,$InfoLine.IndexOf(' ') + 1)
if ($Get_Field_Temp.IndexOf(' ') -ge 0)
{
$Get_Field_Temp=$Get_Field_Temp.Remove($Get_Field_Temp.IndexOf(' '))
}
$Get_Field_Temp
}
function Get_Data
{
param([string]$InfoLine = "File Blank")
$Get_Data_Temp=$InfoLine.Remove(0,$InfoLine.IndexOf(' ') + 1)
if ($Get_Data_Temp.IndexOf(' ') -ge 0)
{
$Get_Data_Temp.Remove(0,$Get_Data_Temp.IndexOf(' ') + 1)
} else {
""
}
}
function Get_Info
{
param([string]$Get_Info_Track = "File", [string]$Get_Info_Field = "Duration")
for ($Get_Info_Count=0; $Get_Info_Count -lt $Info.Length; $Get_Info_Count++)
{
$Get_Info_Temp_Track = Get_Track $Info[$Get_Info_Count]
$Get_Info_Temp_Field = Get_Field $Info[$Get_Info_Count]
if (($Get_Info_Temp_Track -eq $Get_Info_Track) -and ($Get_Info_Temp_Field -eq $Get_Info_Field))
{
Get_Data $Info[$Get_Info_Count]
break
}
}
}
function Get_SourceInfo
{
param([string]$Get_Info_Track = "File", [string]$Get_Info_Field = "Duration")
for ($Get_Info_Count=0; $Get_Info_Count -lt $SourceInfo.Length; $Get_Info_Count++)
{
$Get_Info_Temp_Track = Get_Track $SourceInfo[$Get_Info_Count]
$Get_Info_Temp_Field = Get_Field $SourceInfo[$Get_Info_Count]
if (($Get_Info_Temp_Track -eq $Get_Info_Track) -and ($Get_Info_Temp_Field -eq $Get_Info_Field))
{
Get_Data $SourceInfo[$Get_Info_Count]
break
}
}
}
cls
Get-ChildItem -Path $TargetDirectory -OutVariable "Boom" -Filter *.mkv | foreach-object -process {
$FileName=$_.BaseName
$FullName=$_.FullName
$ExtPName=$_.BaseName + $_.Extension
$SourceInfo=MyMediaBatch "$FullName"
$TempFolder=$TempDir+$FileName+"\"
$VideoCount=0
$AudioCount=0
$SubtitleCount=0
$VideoNames=@()
$VideoStream=@()
$AudioNames=@()
$AudioStream=@()
$SubtitleNames=@()
$SubtitleStream=@()
$ExtractList=""
$MergeList=""
$PropEditList=""
"------------------------"
""
"Processing: `"" + $ExtPName + "`""
#Extract All Tracks to Temp Directory
for ($a=0; $a -lt $SourceInfo.Length; $a++)
{
$Track=Get_Track $SourceInfo[$a]
$Field=Get_Field $SourceInfo[$a]
$Data=Get_Data $SourceInfo[$a]
$TempFile=""
$Boom=""
if ($Field -eq "Codec")
{
if ($Data -eq "V_MPEG2")
{
$Boom="v"
$TempFile=".mp2"
}
elseif ($Data -eq "V_MS/VFW/FOURCC / WVC1")
{
$Boom="v"
$TempFile=".avi"
}
elseif ($Data -eq "V_MPEG4/ISO/AVC")
{
$Boom="v"
$TempFile=".h264"
}
elseif ($Data -eq "A_FLAC")
{
$Boom="a"
$TempFile=".flac"
}
elseif ($Data -eq "A_PCM/INT/LIT")
{
$Boom="a"
$TempFile=".wav"
}
elseif ($Data -eq "A_DTS")
{
$Boom="a"
$TempFile=".dts"
}
elseif ($Data -eq "A_TRUEHD")
{
$Boom="a"
$TempFile=".ac3"
}
elseif ($Data -eq "A_AC3")
{
$Boom="a"
$TempFile=".ac3"
}
elseif ($Data -eq "A_AAC")
{
$Boom="a"
$TempFile=".aac"
}
elseif ($Data -eq "S_HDMV/PGS")
{
$Boom="s"
$TempFile=".sup"
}
elseif ($Data -eq "S_VOBSUB")
{
$Boom="s"
$TempFile=".sub"
}
if ($Boom -ne "")
{
if ($Boom -eq "v")
{
$VideoCount++
$TempFile=$TempFolder + "Video " + $VideoCount + $TempFile
$VideoNames=$VideoNames+$TempFile
$VideoStream=$VideoStream+$Track
}
elseif ($Boom -eq "a")
{
$AudioCount++
$TempFile=$TempFolder + "Audio " + $AudioCount + $TempFile
$AudioNames=$AudioNames+$TempFile
$AudioStream=$AudioStream+$Track
}
elseif ($Boom -eq "s")
{
$SubtitleCount++
if ($TempFile -eq ".sub")
{
$SubFile=$TempFolder + "Subtitle " + $SubtitleCount + ".idx"
$TempFile=$TempFolder + "Subtitle " + $SubtitleCount + $TempFile
$SubtitleNames=$SubtitleNames+$SubFile
}
else
{
$TempFile=$TempFolder + "Subtitle " + $SubtitleCount + $TempFile
$SubtitleNames=$SubtitleNames+$TempFile
}
$SubtitleStream=$SubtitleStream+$Track
}
if ($ExtractList -ne "") {$ExtractList=$ExtractList + " "}
$ExtractList=$ExtractList + $Track + ":`"" + $TempFile + "`""
}
}
}
$ExtractList="`"tracks`" `"" + $FullName + "`" " + $ExtractList
"EXtracting Tracks..."
$Boom=MKVEXTRACT $ExtractList
if ($Debug) {$Boom}
#Mux All Tracks into Individual MKVs, truncating those that are larger than the Video Streams and set track UIDs
$TempFile=$TargetDirectory + "\Temp.mkv"
$FinalMerge="`"-o`" `"" + $TempFile + "`" `"-A`" `"-D`" `"-S`" `"" + $FullName + "`""
$Track=0
$VDuration="0"
for ($a=1; $a -le $VideoCount; $a++)
{
$MergeList="`"-o`" `"" + $TempFolder + "Video " + $a + ".mkv`" `"" + $VideoNames[$a-1] + "`""
"Muxing Video " + $a + "..."
$Boom=MKVMERGE $MergeList
if ($Debug) {$Boom}
Remove-Item -Path $VideoNames[$a-1]
$VideoNames[$a-1]=$TempFolder + "Video " + $a + ".mkv"
$Info=MyMediaBatch $VideoNames[$a-1]
$TDuration=Get_Info "File" "Duration"
if ($TDuration -gt $VDuration)
{
$VDuration=$TDuration
$VHours=Get_Info "File" "Hours"
$VMins=Get_Info "File" "Mins"
$VSecs=Get_Info "File" "Secs"
$VMilli=Get_Info "File" "Milli"
}
$Track++
$StreamName=Get_SourceInfo $VideoStream[$a-1] "Name"
$StreamDefault=Get_SourceInfo $VideoStream[$a-1] "Default"
$StreamForced=Get_SourceInfo $VideoStream[$a-1] "Forced"
$PropEditList="`"" + $VideoNames[$a-1] + "`" `"-e`" `"track:v1`" `"-s`" `"track-uid=" + $Track + "`" `"-s`" `"flag-default=" + $StreamDefault + "`" `"-s`" `"flag-forced=" + $StreamForced + "`" `"-s`" `"name=" + $StreamName + "`""
$Boom=MKVPropEdit $PropEditList
if ($Debug) {$Boom}
$FinalMerge=$FinalMerge + " `"" + $VideoNames[$a-1] + "`""
}
$CutAt="`"--split`" `"parts:00:00:00.000-" + $VHours.PadLeft(2,'0') + ":" + $VMins.PadLeft(2,'0') + ":" + $VSecs.PadLeft(2,'0') + ":" + $VMilli.PadLeft(3,'0') + "`" `"-o`" `""
for ($a=1; $a -le $AudioCount; $a++)
{
$WorkFile=$TempFolder + "Audio " + $a + ".mka"
$MergeList=$CutAt + $WorkFile + "`" `"" + $AudioNames[$a-1] + "`""
"Muxing Audio " + $a + "..."
$Boom=MKVMERGE $MergeList
if ($Debug) {$Boom}
Remove-Item -Path $AudioNames[$a-1]
$AudioNames[$a-1]=$WorkFile
$Track++
$StreamName=Get_SourceInfo $AudioStream[$a-1] "Name"
$StreamDefault=Get_SourceInfo $AudioStream[$a-1] "Default"
$StreamForced=Get_SourceInfo $AudioStream[$a-1] "Forced"
$PropEditList="`"" + $AudioNames[$a-1] + "`" `"-e`" `"track:a1`" `"-s`" `"track-uid=" + $Track + "`" `"-s`" `"flag-default=" + $StreamDefault + "`" `"-s`" `"flag-forced=" + $StreamForced + "`" `"-s`" `"name=" + $StreamName + "`""
$Boom=MKVPropEdit $PropEditList
if ($Debug) {$Boom}
$FinalMerge=$FinalMerge + " `"" + $AudioNames[$a-1] + "`""
}
for ($a=1; $a -le $SubtitleCount; $a++)
{
$WorkFile=$TempFolder + "Subtitle " + $a + ".mks"
$MergeList=$CutAt + $WorkFile + "`" `"" + $SubtitleNames[$a-1] + "`""
"Muxing Subtitle " + $a + "..."
$Boom=MKVMERGE $MergeList
if ($Debug) {$Boom}
Remove-Item -Path $SubtitleNames[$a-1]
$SubtitleNames[$a-1]=$WorkFile
$Track++
$StreamName=Get_SourceInfo $SubtitleStream[$a-1] "Name"
$StreamDefault=Get_SourceInfo $SubtitleStream[$a-1] "Default"
$StreamForced=Get_SourceInfo $SubtitleStream[$a-1] "Forced"
$PropEditList="`"" + $SubtitleNames[$a-1] + "`" `"-e`" `"track:s1`" `"-s`" `"track-uid=" + $Track + "`" `"-s`" `"flag-default=" + $StreamDefault + "`" `"-s`" `"flag-forced=" + $StreamForced + "`" `"-s`" `"name=" + $StreamName + "`""
$Boom=MKVPropEdit $PropEditList
if ($Debug) {$Boom}
$FinalMerge=$FinalMerge + " `"" + $SubtitleNames[$a-1] + "`""
}
#Combine Back Into A Single File
"Muxing New File..."
$Boom=MKVMerge $FinalMerge
if ($Debug) {$Boom}
#Set Last End Chapter Time to New Duration
"Prepping Chapters.."
$Info=MyMediaBatch $TempFile
$VHours=Get_Info "File" "Hours"
$VMins=Get_Info "File" "Mins"
$VSecs=Get_Info "File" "Secs"
$VMilli=Get_Info "File" "Milli"
$ExtractList="`"chapters`" `"" + $TempFile + "`""
$Info=MKVExtract $ExtractList
for ($a=$Info.Length-1; $a -ge 0; $a--)
{
if ($Info[$a].Contains("<ChapterTimeEnd>"))
{
$Info[$a]=$Info[$a].Remove($Info[$a].IndexOf('>') + 1) + $VHours.PadLeft(2,'0') + ":" + $VMins.PadLeft(2,'0') + ":" + $VSecs.PadLeft(2,'0') + ":" + $VMilli.PadLeft(3,'0').PadRight(9, '0') + $Info[$a].Remove(0, $Info[$a].LastIndexOf('<'))
break
}
}
$ExtractList=$TempFolder + "\Chapters.xml"
Out-File -FilePath $ExtractList -Encoding utf8 -InputObject $Info
$Boom=MKVPropEdit -c "$ExtractList" "$TempFile"
#Replace Old File With New File and Move Old File to "Old" Directory
Move-Item -Path $FullName -Destination $OldFolder
Rename-Item -NewName $ExtPName -Path $TempFile
"Done!"
""
}