plex_fr_discord_repo/Scripts-Divers/Batch MULTI-Merge one MKV (Vid,Aud,Sub) with another MKV Video with external SRT.ps1

885 lines
48 KiB
PowerShell
Raw Permalink Normal View History

##==============================================================================================
## ##
## Batch Merge Subtitles with MKVMerge ##
## By Miles ##
## Idea from Iain McCain : https://superuser.com/a/1249870 ##
## ##
##==============================================================================================
## ##
## Utilisation et conditions : ##
## - Il faut que les .srt et les .mkv soient dans le même dossier. ##
## - Il faut que les sous-titres externes .SRT aient le même nom de fichier que les .MKV. ##
## - Il faut paramétrer l'extension des fichiers de sous-titres pour que ces derniers ##
## reflètent la langue : eng, fre,... ##
## - Il faudra par ailleurs modifier le code langage dans AudioLang_X et SubTrackLang_1 ##
## Il est possible de : ##
## - spécifier des dossiers sources et destinations identiques ou différents ##
## - spécifier un titre de piste (pour toutes) ##
## ##
##==============================================================================================
## ##
## Objectifs : ##
## Remuxer deux MKV en gardant l'audio et le(s) sous-titre du premier ou un sous-titre ##
## externe, et seulement la vidéo du second MKV ##
## ##
##==============================================================================================
# ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
# │ - MKV1 : file to keep with 2 audio, 1 internal SRT, DROP the video (720p) │
# │ - MKV2 : file to keep with only the video 1080p ! │
# │ - SRT1 : external SRT file with same name of the MKV2 file (SRT made by extracted PGS in the MKV2 file) │
# │ - SRT2 : external SRT file with same name of the MKV2 file (SRT made by extracted PGS in the MKV2 file) │
# └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Clear-Host
Invoke-Command -ScriptBlock {
# #############################################################################################
# -> BEGIN VARIABLES DECLARATION -----------------------------------------------------------
# #############################################################################################
# Set MKVMerge.exe and MediaInfo.exe (CLI version) Path
$MKVMerge = "`"PATH_TO\mkvtoolnix\mkvmerge.exe`""
$MediaInfo = "`"PATH_TO\MediaInfo_CLI\MediaInfo.exe`""
# For the parameters to pass to MKVMerge executable, will be construct
# Set this to $true in order to merge a MKV_2 present in $sourceDirectory_2
$merge_another_video_MKV2 = $false # If set to $false, $sourceDirectory_2 will be ignored.
# Don't add a \ at the end of the path...
$sourceDirectory_1 = "PATH_TO_SOURCE_1"
$sourceDirectory_2 = "PATH_TO_SOURCE_1"
$destinationDirectory = "PATH_TO_DESTINATION"
# If you want a .json file created, set this $JSON_Export to $true, set the $sourceDirectory_1_json variable to set the path for the .json file.
# If you don"t want a .json file, set set this $JSON_Export to $false, and let the others as they are.
$JSON_Export = $false
$sourceDirectory_1_json = "PATH_TO_JSON_FILE"
# Remove dots before the season or the resolution ?
$remove_dots = $false
# If there is the year in the title, set it to $true
$set_year_with_brackets = $false
# Renaming the output file (and the video title)
# Use https://regex101.com/r/cU5lC2/1 to get the RegEx
$chain_to_search = ''
$chain__to_replace = ''
# $chain_to_search = '(.*) - S(\d{2})E(\d{3})(.*)H264(.*)'
# $chain_to_search = '(.*) - S(\d{2})E(\d{3})(.*)'
# $chain__to_replace = '$1 - S$2E$3$4x265-10bits$5--Custom'
# Move MKV1 (and SRT) to Not-Merged folder ? Set to $false or $true
$move_mkv1_after_merge = $true
$move_mkv2_after_merge = $true
# Extension without the .
$VideoExtension_1 = "mkv"
$VideoExtension_2 = "mkv"
$no_attachements = $true
# ################ VIDEO to keep ##################
# = 0 : for video from MKV1 (first file)
# = 1 : for video from MKV2 (second file)
$Video_Lang = "ja" # en for english / fr for french / ja for japanese
# ================== AUDIO ================== Common for all videos, MAX 2 audio
# _1 will be the first in track-name order, then _2
# How many audio tracks to keep = 0, 1 or 2
$NB_Audio_MKV1 = 1
# This is to search for the right ID in the MKV (with JSON)
$AudioTrack_1_Lang = "ja" # Set here the first audio lang to keep
$AudioTrack_2_Lang = "fr" # Set here the second audio lang to keep
# $AudioTrack_Default_Lang = "ja" # Set here the default audio lang to set in merge
# This is for personalizing the tracks info !
# Track 1 = Audio n°1
$AudioTrackName_1_sansID = "Japonais - AAC 2.0"
$AudioLang_1_sansID = "ja" # en for english / fr for french / ja for japanese
$AudioTrack_1_default_sansID = "yes"
# Define track order. Do not modify it unless you changed the track number in this section
# Track 2 = Audio n°2
# Name and language of Audio Track n°2
$AudioTrackName_2_sansID = "Français - Dolby Digital 2.0"
$AudioLang_2_sansID = "fr" # en for english / fr for french / ja for japanese
$AudioTrack_2_default_sansID = "no"
# Define track order. Do not modify it unless you changed the track number in this section
# ================================================================
# ===========================================
# ================== Internal SUB ? =========
# To keep or not internal subtitles
# $keep_internal_Sub = $true
$Internal_SUB_Number = 0 # Set this to 1 or 2 to keep 1 or 2 or 3 internal subtitles, or to 0 to keep no internal sub.
$Internal_SUB_Lang_1 = "fr" # Set here the first internal subtitle lang to keep
$Internal_SUB_Lang_2 = "fr" # Set here the second internal subtitle lang to keep
$Internal_SUB_Lang_3 = "fr" # Set here the third internal subtitle lang to keep
# If there is 2 sub in the same language, like "FR Full" and "FR Forced", set those variables :
$Internal_SUB_to_search_with_title = $false # Set this to $true if there is at least 2 internal sub in the same lang
$Internal_SUB_1_title_to_search = "SDH" # Set this to a string to search in sub title to set it into the first internal sub
$Internal_SUB_2_title_to_search = "" # Set this to a string to search in subtitle TITLE to set it into the second internal sub
$Internal_SUB_2_title_to_search = "" # Set this to a string to search in subtitle TITLE to set it into the third internal sub
$Internal_SUB_to_be_forced = "" # Set this to 1 or 2 in order to set it to forced subtitle
# Max 3 Internal SRT is configured in the script.
# If set to 3, must be 2 SRT configured
$SubTrackName_1_sansID = "English SDH - SRT"
$SubTrackLang_1_sansID = "en" # en for english / fr for french / jp for japanese
$SubTrack_1_default_sansID = "yes"
$SubTrack_1_forced_track_sansID = "no"
$SubTrack_1_SDH_sansID = "yes"
$sub_charset_1_sansID = "UTF-8"
$SubTrackName_2_sansID = "Français - SRT"
$SubTrackLang_2_sansID = "fr" # en for english / fr for french / jp for japanese
$SubTrack_2_default_sansID = "no"
$SubTrack_2_forced_track_sansID = "no"
$SubTrack_2_SDH_sansID = "no"
$sub_charset_2_sansID = "UTF-8"
$SubTrackName_3_sansID = "Français - SRT"
$SubTrackLang_3_sansID = "fr" # en for english / fr for french / jp for japanese
$SubTrack_3_default_sansID = "no"
$SubTrack_3_forced_track_sansID = "no"
$SubTrack_3_SDH_sansID = "no"
$sub_charset_3_sansID = "UTF-8"
# ===========================================
# ===========================================
# ================== External SUB ? =========
# Must have the same name as MKV1
$External_SUB = $true
$External_SUB_Number = 1 # Works only if $External_SUB = $true
# Max 2 External SRT is configured in the script.
# If set to 2, must be 2 SRT configured
# Set Subtitle Extension (Don't add the . before the extension)
$SubExtension_1 = 'Full.fre.srt'
$SubExtension_2 = 'Forced.fre.srt'
#### FILE 2 - SRT_1 - Keeping all but the video (audio + chapters tags)
# Track 0 = Sub n°1 to keep
# Name and language of Subtitle Track n°0 + Sync Value
$ExtSubTrackName_1 = "0:Français (Complet) - SRT"
$ExtSubTrackLang_1 = "0:fr"
$ExtSubTrack_1_default = "0:yes"
$ExtSub_charset_1 = "0:UTF-8"
$ExtSub_forced_track_1 = "0:no"
$ExtSub_SDH_1 = "0:no"
#### FILE 3 - SRT_2 - Keeping all but the video (audio + chapters tags)
# Track 0 = Sub n°1 to keep
# Name and language of Subtitle Track n°0 + Sync Value
$ExtSubTrackName_2 = "0:Français (Forcé) - SRT"
$ExtSubTrackLang_2 = "0:fr"
$ExtSubTrack_2_default = "0:no"
$ExtSub_charset_2 = "0:UTF-8"
$ExtSub_forced_track_2 = "0:yes"
$ExtSub_SDH_2 = "0:no"
#----------------------------------------------------------------------------------------------
# DO NOT TOUCH WHAT'S UNDER THIS -----------------------------------------------------------
#----------------------------------------------------------------------------------------------
# #############################################################################################
# -> BEGIN FUNCTION DEFINITIONS -----------------------------------------------------------
# #############################################################################################
function Get-Track_ID {
param (
[Parameter(Mandatory)]$MediaInfo,
[Parameter(Mandatory)]$MKV_1,
[Parameter(Mandatory)][ValidateSet(1, 2)]$NB_Audio_MKV1,
[Parameter(Mandatory)]$AudioTrack_1_Lang,
[Parameter(Mandatory)]$AudioTrack_2_Lang,
[Parameter(Mandatory)][ValidateSet(0, 1, 2, 3)]$Internal_SUB_Number,
[Parameter(Mandatory)]$Internal_SUB_Lang_1,
[Parameter(Mandatory)]$Internal_SUB_Lang_2,
[Parameter(Mandatory)]$Internal_SUB_Lang_3,
[Parameter(Mandatory)][ValidateSet($true, $false)]$Internal_SUB_to_search_with_title,
[Parameter(Mandatory)]$Internal_SUB_1_title_to_search = "",
[Parameter(Mandatory)]$Internal_SUB_2_title_to_search = "",
[Parameter(Mandatory)]$Internal_SUB_3_title_to_search = "",
[Parameter(Mandatory = $false)][ValidateSet($true, $false)]$JSON_Export = $false,
[Parameter(Mandatory = $false)]$JSON_file = ""
)
# Values witch will be found with MediaInfo JSON
$VideoTrack_ID = ""
$AudioTrack_1_ID = ""
$AudioTrack_2_ID = ""
$Internal_Sub_1_ID = ""
$Internal_Sub_2_ID = ""
$Internal_Sub_3_ID = ""
$JSON_video_position = "" # To use with $JSON_object.media.Track[ ]
$JSON_audio_1_position = "" # To use with $JSON_object.media.Track[ ]
$JSON_audio_2_position = "" # To use with $JSON_object.media.Track[ ]
$JSON_sub_1_position = ""
$JSON_sub_2_position = ""
$JSON_sub_3_position = ""
if (( $JSON_Export -eq $true) -And ( $JSON_file -ne "")) {
$MediaInfo_arguments = "--output=JSON --LogFile=`"$JSON_file`""
}
else {
if ( $JSON_Export -eq $false ) {
Write-Host "`tNo JSON file will be created..." -ForegroundColor "White" -BackgroundColor "DarkRed"
}
elseif ( $JSON_file -eq "") {
Write-Host "`tNo JSON file is pass in Get-Track_ID function parameters !!`n`tThere will not be any JSON file created..." -ForegroundColor "White" -BackgroundColor "DarkRed"
}
$MediaInfo_arguments = "--output=JSON"
}
$command_info = "& $MediaInfo $MediaInfo_arguments `"$MKV_1`""
# Launch command begin... and we're grabbing the output into json_output
$json_output = Invoke-Expression $command_info
# Write-Host "`$json_output = `n$json_output" -ForegroundColor "green"
# $JSON_object = Get-Content -Raw -Path "$JSON_file" | ConvertFrom-Json
$JSON_object = $json_output | ConvertFrom-Json
$i = 0 # Counter for video track detected
$j = 0 # Counter for audio track detected
$k = 0 # Counter for audio tracks kept
$l = 0 # Counter for track position
$m = 0 # Counter for sub track detected
$n = 0 # Counter for sub tracks kept
$JSON_VideoCount = $JSON_object.media.Track[0].VideoCount
$JSON_AudioCount = $JSON_object.media.Track[0].AudioCount
$JSON_SubCount = $JSON_object.media.Track[0].TextCount
foreach ( $track in $JSON_object.media.Track ) {
# Note :
# The reel track ID is 1 minus the ID get in JSON...
# Check if there is a bug in the script
if ( $j -gt $JSON_AudioCount ) {
Write-Host "`tThere is a problem in Audio Track detection from JSON !! Need to debug this !!" -ForegroundColor "White" -BackgroundColor "DarkRed"
Exit
}
elseif ( $i -gt $JSON_VideoCount ) {
Write-Host "`tThere is a problem in Video Track detection from JSON !! Need to debug this !!" -ForegroundColor "White" -BackgroundColor "DarkRed"
Exit
}
elseif ( ( $m -gt $JSON_SubCount ) -And ( $JSON_SubCount -ne $null ) ) {
Write-Host "`tThere is a problem in Subtitles Track detection from JSON !! Need to debug this !!" -ForegroundColor "White" -BackgroundColor "DarkRed"
Exit
}
if ( $track."@type" -match "Video" ) {
$VideoTrack_ID = $track.id - 1
$i++
$JSON_video_position = $l
}
elseif ( $track."@type" -match "Audio" ) {
$j++
if (( $track."Language" -match "$AudioTrack_1_Lang" ) -And ( $AudioTrack_1_ID -eq "" ) -And ( $k -ne $NB_Audio_MKV1)) {
$AudioTrack_1_ID = $track.id - 1
$k++
$JSON_audio_1_position = $l
}
elseif (( $track."Language" -match "$AudioTrack_2_Lang" ) -And ( $AudioTrack_2_ID -eq "" ) -And ( $k -ne $NB_Audio_MKV1)) {
$AudioTrack_2_ID = $track.id - 1
$k++
$JSON_audio_2_position = $l
}
else {
if (( $AudioTrack_1_ID -eq "" ) -And ( $AudioTrack_2_ID -eq "" )) {
Write-Host "`tThere is no audio tracks that match your specifications." -ForegroundColor "Yellow"
}
else {
Write-Host "`tThere is no audio tracks that match your specifications." -ForegroundColor "Yellow"
}
}
}
elseif ( $track."@type" -match "Text" ) {
$m++
if ($Internal_SUB_Number -ne 0 ) {
if ( ( $Internal_SUB_Lang_1 -ne "" ) -And ( $track."Language" -match "$Internal_SUB_Lang_1" ) -And ( $Internal_Sub_1_ID -eq "" ) -And ( $n -ne $Internal_SUB_Number ) -And (( $Internal_SUB_to_search_with_title -eq $false ) -OR (( $Internal_SUB_to_search_with_title -eq $true ) -And ( $track.Title -match "$Internal_SUB_1_title_to_search" )) ) ) {
$Internal_Sub_1_ID = $track.id - 1
$n++
$JSON_sub_1_position = $l
}
elseif ( ( $Internal_SUB_Lang_2 -ne "" ) -And ( $track."Language" -match "$Internal_SUB_Lang_2" ) -And ( $Internal_Sub_2_ID -eq "" ) -And ( $n -ne $Internal_SUB_Number) -And (( $Internal_SUB_to_search_with_title -eq $false ) -OR (( $Internal_SUB_to_search_with_title -eq $true ) -And ( $track.Title -match "$Internal_SUB_2_title_to_search" )) ) ) {
$Internal_Sub_2_ID = $track.id - 1
$n++
$JSON_sub_2_position = $l
}
elseif ( ( $Internal_SUB_Lang_3 -ne "" ) -And ( $track."Language" -match "$Internal_SUB_Lang_3" ) -And ( $Internal_Sub_3_ID -eq "" ) -And ( $n -ne $Internal_SUB_Number) -And (( $Internal_SUB_to_search_with_title -eq $false ) -OR (( $Internal_SUB_to_search_with_title -eq $true ) -And ( $track.Title -match "$Internal_SUB_3_title_to_search" )) ) ) {
$Internal_Sub_3_ID = $track.id - 1
$n++
$JSON_sub_3_position = $l
}
else {
}
}
else {
# Some Internal Sub are present, but we don't care, we don't keep it/them...
Write-Host "`tThere is some internal subtitle tracks, but you choose to not keep it/them..." -ForegroundColor "Cyan"
}
}
$l++ # Increment number of track processed
}
if ($Internal_SUB_Number -ne 0 ) {
# Checking if there are the right number of IDs retrieved
if ( ( $Internal_SUB_Number -ne 0 ) -And ( $JSON_SubCount -eq 0 ) ) {
Write-Host "`tYou choose to keep at least one internal subtitle, but there isn't any in the MKV..." -ForegroundColor "Red"
}
if (( $Internal_Sub_1_ID -eq "" ) -And ( $Internal_Sub_2_ID -eq "" ) -And ( $Internal_Sub_3_ID -eq "" )) {
Write-Host "`tThere is no internal subtitle tracks that match your specifications." -ForegroundColor "Yellow"
}
else {
$counter = 0
if ( $Internal_Sub_1_ID -ne "" ) {
$counter++
}
if ( $Internal_Sub_2_ID -ne "" ) {
$counter++
}
if ( $Internal_Sub_3_ID -ne "" ) {
$counter++
}
if ( $counter -lt $Internal_SUB_Number ) {
# Not all desired subtitles are retrieved
Write-Host "`tNot all desired subtitles are retrieved..." -ForegroundColor "Red"
if (( $Internal_SUB_Number -ge 1 ) -And ( $Internal_Sub_1_ID -eq "" )) {
Write-Host "`tThere is no internal subtitle tracks that match your specification language : $Internal_SUB_Lang_1." -ForegroundColor "Yellow"
}
if (( $Internal_SUB_Number -ge 2 ) -And ( $Internal_Sub_2_ID -eq "" )) {
Write-Host "`tThere is no internal subtitle tracks that match your specification language : $Internal_SUB_Lang_2." -ForegroundColor "Yellow"
}
if (( $Internal_SUB_Number -eq 3 ) -And ( $Internal_Sub_3_ID -eq "" )) {
Write-Host "`tThere is no internal subtitle tracks that match your specification language : $Internal_SUB_Lang_3." -ForegroundColor "Yellow"
}
}
# Write-Host "`tThere is no internal subtitle tracks that match your specifications." -ForegroundColor "Yellow"
}
}
# if ( ( $Internal_SUB_Number -eq 1 ) -And ( $Internal_Sub_1_ID -eq "" ) ) {
# Write-Host "`tYou choose to keep one internal subtitle, but there isn't any in the MKV..." -ForegroundColor "Red"
# }
# elseif ( ( $Internal_SUB_Number -eq 2 ) -And ( ($Internal_Sub_1_ID -eq "") -Or ($Internal_Sub_2_ID -eq "") ) ) {
# if ( $Internal_Sub_1_ID -eq "" ) {
# Write-Host "`tYou choose to keep two internal subtitle, but there isn't any in the MKV..." -ForegroundColor "Yellow"
# }
# }
# elseif ( ( $Internal_SUB_Number -eq 2 ) -And ( ($Internal_Sub_1_ID -eq "") -And ($Internal_Sub_2_ID -eq "") ) ) {
# Write-Host "`tYou choose to keep two internal subtitle, but there isn't any in the MKV..." -ForegroundColor "Yellow"
# }
# elseif ( ( $Internal_SUB_Number -eq 2 ) -And ( ($Internal_Sub_1_ID -ne "") -And ($Internal_Sub_2_ID -ne "") -And ($Internal_Sub_3_ID -eq "") ) ) {
# Write-Host "`tOnly Two Internal Subtitles ID have been retrieved ! Great !" -ForegroundColor "Yellow"
# }
# elseif ( ( $Internal_SUB_Number -eq 3 ) -And ( ($Internal_Sub_1_ID -ne "") -And ($Internal_Sub_2_ID -ne "") -And ($Internal_Sub_3_ID -ne "") ) ) {
# Write-Host "`tOnly Three Internal Subtitles ID have been retrieved ! Great !" -ForegroundColor "Yellow"
# }
Write-Host "`t`$VideoTrack_ID = $VideoTrack_ID" -ForegroundColor "Yellow"
if ($NB_Audio_MKV1 -ge 1) {
Write-Host "`t`$AudioTrack_1_ID = $AudioTrack_1_ID`tLang set in the script = $AudioTrack_1_Lang" -ForegroundColor "Yellow"
if ($NB_Audio_MKV1 -eq 2) {
Write-Host "`t`$AudioTrack_2_ID = $AudioTrack_2_ID`tLang set in the script = $AudioTrack_2_Lang" -ForegroundColor "Yellow"
}
}
if ( $Internal_SUB_Number -ne 0 ) {
Write-Host "`t`$Internal_Sub_1_ID = $Internal_Sub_1_ID`tLang set in the script = $Internal_SUB_Lang_1" ( &{ If ($Internal_SUB_to_search_with_title -eq $true) { "`tSearched Title was : $Internal_SUB_1_title_to_search" } Else { "" } }) -ForegroundColor "Yellow"
if ($Internal_SUB_Number -ge 2) {
Write-Host "`t`$Internal_Sub_2_ID = $Internal_Sub_2_ID`tLang set in the script = $Internal_SUB_Lang_2" ( &{ If ($Internal_SUB_to_search_with_title -eq $true) { "`tSearched Title was : $Internal_SUB_2_title_to_search" } Else { "" } }) -ForegroundColor "Yellow"
if ($Internal_SUB_Number -eq 3) {
Write-Host "`t`$Internal_Sub_3_ID = $Internal_Sub_3_ID`tLang set in the script = $Internal_SUB_Lang_3" ( &{ If ($Internal_SUB_to_search_with_title -eq $true) { "`tSearched Title was : $Internal_SUB_3_title_to_search" } Else { "" } }) -ForegroundColor "Yellow"
}
}
}
return $VideoTrack_ID, $AudioTrack_1_ID, $AudioTrack_2_ID, $Internal_Sub_1_ID, $Internal_Sub_2_ID, $Internal_Sub_3_ID
}
function Test-Directory {
param (
[Parameter(Mandatory)]$path_to_verify
)
If (!(test-path $path_to_verify)) {
New-Item -ItemType "Directory" -Force -Path $path_to_verify
Write-Host "The path $path_to_verify (or one of the last subfolder) didn't exist. It has been created." -ForegroundColor "DarkBlue"
}
}
# #############################################################################################
# -> BEGIN MAIN SCRIPT -------------------------------------------------------------------
# #############################################################################################
# Resetting some variables that should be equal to ""
if ( $NB_Audio_MKV1 -eq 0 ) {
$AudioTrack_1_Lang = ""
$AudioTrack_2_Lang = ""
}
elseif ( $NB_Audio_MKV1 -eq 1 ) {
$AudioTrack_2_Lang = ""
}
if ( $Internal_SUB_Number -eq 0 ) {
$Internal_SUB_Lang_1 = ""
$Internal_SUB_Lang_2 = ""
$Internal_SUB_Lang_3 = ""
}
elseif ( $Internal_SUB_Number -eq 1 ) {
$Internal_SUB_Lang_2 = ""
$Internal_SUB_Lang_3 = ""
}
elseif ( $Internal_SUB_Number -eq 2 ) {
$Internal_SUB_Lang_3 = ""
}
# -------------------------------------------------
# Define track order - DO NOT TOUCH
$track_order_EXT_sub = ""
if ( $merge_another_video_MKV2 -eq $false) {
if ( $External_SUB -eq $true ) {
if ( $External_SUB_Number -ge 1 ) {
$track_order_EXT_sub += "1:0"
if ( $External_SUB_Number -eq 2 ) {
$track_order_EXT_sub += ",2:0"
}
}
}
}
else {
if ( $External_SUB -eq $true ) {
if ( $External_SUB_Number -ge 1 ) {
$track_order_EXT_sub += "2:0"
if ( $External_SUB_Number -eq 2 ) {
$track_order_EXT_sub += ",3:0"
}
}
}
}
# ===========================================
# Checking if the $destinationDirectory exists, if not, create the folder(s)
Test-Directory $destinationDirectory
# Counting the number of MKV_1
$MKV_1_List = Get-ChildItem $sourceDirectory_1 -Filter "*.$VideoExtension_1" | ForEach-Object { $_.FullName } | Sort-Object
$Count_1 = $MKV_1_List.count
Write-Host "$Count_1 Videos ($VideoExtension_1) to be processed in $sourceDirectory_1."
# Testing if the Done_Remerged folder already exists : if yes, it will be renamed, else it will be created.
$Path_Folder_NotMerged_1 = $sourceDirectory_1 + "\Done_Remerged"
Test-Directory $Path_Folder_NotMerged_1
if ($merge_another_video_MKV2 -eq $true) {
# Counting the number of MKV_1
$MKV_2_List = Get-ChildItem $sourceDirectory_2 -Filter "*.$VideoExtension_2" | ForEach-Object { $_.FullName } | Sort-Object
$Count_2 = $MKV_2_List.count
Write-Host "$Count_2 MKV's to be processed in $sourceDirectory_2."
# Testing if the Done_Remerged folder already exists : if yes, it will be renamed, else it will be created.
$Path_Folder_NotMerged_2 = $sourceDirectory_2 + "\Done_Remerged"
Test-Directory $Path_Folder_NotMerged_2
# #########################
# Check if there is the same .mkv files in both source directory
if ($Count_1 -eq $Count_2) {
Write-Host "There is the same number of MKV in the two sources folders. Let's continue."
}
else {
Write-Host "The number of MKV in the two sources folders isn't the same. ABORT..." -foreground "red"
Exit
}
# #########################
}
# #########################
# Check if there is the same number of .srt files as .mkv files
if ( $External_SUB -eq $true ) {
if ( $External_SUB_Number -ge 1 ) {
# If $NB_External_SUB >= 1 (include =1 and =2)
$SRT_1_List = Get-ChildItem $sourceDirectory_1 -Filter "*.$SubExtension_1" | ForEach-Object { $_.FullName } | Sort-Object
$Count_1_SRT = $SRT_1_List.count
Write-Host "$Count_1_SRT SRT's ($SubExtension_1) to be processed in $sourceDirectory_1."
if ($Count_1 -eq $Count_1_SRT) {
Write-Host "There is the same number of MKV and SRT n°1 files ($SubExtension_1) in this folder. Let's continue."
}
else {
Write-Host "The number of MKV and SRT n°1 files isn't the same. ABORT..." -foreground "red"
Exit
}
}
if ( $External_SUB_Number -eq 2 ) {
# Only if $External_SUB_Number = 2
$SRT_2_List = Get-ChildItem $sourceDirectory_1 -Filter "*.$SubExtension_2" | ForEach-Object { $_.FullName } | Sort-Object
$Count_2_SRT = $SRT_2_List.count
Write-Host "$Count_2_SRT SRT's ($SubExtension_2) to be processed in $sourceDirectory_1."
if ($Count_1 -eq $Count_2_SRT) {
Write-Host "There is the same number of MKV and SRT n°2 files ($SubExtension_2) in this folder. Let's continue."
}
else {
Write-Host "The number of MKV and SRT n°2 files ($SubExtension_2) isn't the same. ABORT..." -foreground "red"
Exit
}
}
}
# #########################
$compteur = 1 # Counter to show the progress of file processed
Foreach ($MKV_1 in $MKV_1_List) {
Write-Host "" -ForegroundColor "black" -BackgroundColor "white"
Write-Host "Traitement du fichier n° $compteur / $Count_1..." -ForegroundColor "black" -BackgroundColor "white"
Write-Host "" -ForegroundColor "black" -BackgroundColor "white"
# Clear variables
$MKVMerge_param_start = ""
$MKVMerge_param_mkv1 = ""
$MKVMerge_param_srt1 = ""
$MKVMerge_param_srt2 = ""
$MKVMerge_param_all = ""
$MKVMerge_sub_param = ""
$SRT_1_Name = ""
$SRT_2_Name = ""
$SRT_1 = ""
$SRT_2 = ""
$SubTrackName_1 = ""
$SubTrackLang_1 = ""
$SubTrack_1_default = ""
$sub_charset_1 = ""
$SubTrackName_2 = ""
$SubTrackLang_2 = ""
$SubTrack_2_default = ""
$sub_charset_2 = ""
$track_order = ""
$VideoTrack_ID, $AudioTrack_1_ID, $AudioTrack_2_ID = "", "", ""
$Internal_Sub_1_ID, $Internal_Sub_2_ID, $Internal_Sub_3_ID = "", "", ""
$MKVMerge_audio_param = ""
$FormatName_1 = (Get-Item $MKV_1).Basename
$MKV_1_name = $FormatName_1.ToString()
# #################################################################
# Title for the video track and for the destination file $Output
$VideoTrackName = $MKV_1_name
if ( $set_year_with_brackets -eq $true ) {
if ( $type_media -eq "movie" ) {
$VideoTrackName = $VideoTrackName -replace '(\d{4}(?=\..*))', '($1)'
}
elseif ( $type_media -eq "serie" ) {
$VideoTrackName = $VideoTrackName -replace '(\d{4}(?=.*S\d{2}E\d{2}))', '($1)'
}
}
if ( $remove_dots -eq $true ) {
if ( $type_media -eq "movie" ) {
$VideoTrackName = $VideoTrackName -replace '\.(?=.*\d{4})', ' '
}
elseif ( $type_media -eq "serie" ) {
$VideoTrackName = $VideoTrackName -replace '\.(?=.*S\d{2}E\d{2})', ' '
}
}
$VideoTrackName = $VideoTrackName -replace $chain_to_search, $chain__to_replace
$VideoTrackName = "$VideoTrackName"
Write-Host "`tThe Video Track Name will be : `r`n`t`t $VideoTrackName" -ForegroundColor "green"
if ( $compteur -le 3 ) {
# Will prompt for a key to be pressed for the 3 first names
Write-Host "`tFor the 3 first file, this will show up, and pause for 10s" -ForegroundColor "yellow";
Start-Sleep -Seconds 10
}
$Output = "$destinationDirectory" + "\" + "$VideoTrackName" + ".mkv"
$JSON_file = "$sourceDirectory_1_json\$MKV_1_name.json"
# #################################################################
# Now we'll get the track ID for the MKV in action !
$VideoTrack_ID, $AudioTrack_1_ID, $AudioTrack_2_ID, $Internal_Sub_1_ID, $Internal_Sub_2_ID, $Internal_Sub_3_ID = Get-Track_ID $MediaInfo $MKV_1 $NB_Audio_MKV1 $AudioTrack_1_Lang $AudioTrack_2_Lang $Internal_SUB_Number $Internal_SUB_Lang_1 $Internal_SUB_Lang_2 $Internal_SUB_Lang_3 $Internal_SUB_to_search_with_title $Internal_SUB_1_title_to_search $Internal_SUB_2_title_to_search $Internal_SUB_3_title_to_search $JSON_Export $JSON_file
# Exception for some files !!! -----------------------------------------------------------------
if ( $MKV_1_name.Substring(0, 26) -eq "One Piece (1999) - S20E001" ) {
$AudioTrack_1_ID = 2 # Manual set for 1st audio track in final MKV
$AudioTrack_2_ID = 1 # Manual set for 2nd audio track in final MKV
Write-Host "`tThis file doesn't have correct track language. We set it to :`n`t`t`$AudioTrack_1_ID = $AudioTrack_1_ID`t for audio lang 1 = $AudioTrack_1_Lang`n`t`t`$AudioTrack_2_ID = $AudioTrack_2_ID." -ForegroundColor "White" -BackgroundColor "DarkRed"
}
# if ( $MKV_1_name.Substring(0, 26) -eq "One Piece (1999) - S18E001" ) {
# $AudioTrackName_1_sansID = "Japonais - AAC 2.0"
# $AudioTrackName_2_sansID = "Français - AAC 2.0"
# Write-Host "`tThis file doesn't have correct track language. We set it to : `$AudioTrack_1_ID = $AudioTrack_1_ID`tand`t`$AudioTrack_2_ID = $AudioTrack_2_ID."
# }
# if ( $MKV_1_name.Substring(0, 26) -eq "One Piece (1999) - S18E008" ) {
# $AudioTrack_1_ID = 2
# $AudioTrack_2_ID = 1
# Write-Host "`tThis file doesn't have correct track language. We set it to : `$AudioTrack_1_ID = $AudioTrack_1_ID`tand`t`$AudioTrack_2_ID = $AudioTrack_2_ID."
# }
# if ( $MKV_1_name.Substring(0, 26) -eq "One Piece (1999) - S18E020" ) {
# $AudioTrack_1_ID = 2
# $AudioTrack_2_ID = 1
# Write-Host "`tThis file doesn't have correct track language. We set it to : `$AudioTrack_1_ID = $AudioTrack_1_ID`tand`t`$AudioTrack_2_ID = $AudioTrack_2_ID."
# }
# End of Exceptions ----------------------------------------------------------------------------
if ( $merge_another_video_MKV2 -eq $false ) {
# Only 1 MKV is to re-merge
# We keep video and audio from MKV_1
if (( $NB_Audio_MKV1 -gt 0 ) -and ($NB_Audio_MKV1 -le 2)) {
# $NB_Audio_MKV1 = 1 or 2
$MKVMerge_audio_param = "--language $AudioTrack_1_ID" + ":$AudioLang_1_sansID --track-name `"$AudioTrack_1_ID" + ":$AudioTrackName_1_sansID`" --default-track $AudioTrack_1_ID" + ":$AudioTrack_1_default_sansID"
$track_order = "$VideoTrack_ID" + ":0,0:$AudioTrack_1_ID"
if ( $NB_Audio_MKV1 -eq 2 ) {
$MKVMerge_audio_param += " --language $AudioTrack_2_ID" + ":$AudioLang_2_sansID --track-name `"$AudioTrack_2_ID" + ":$AudioTrackName_2_sansID`" --default-track $AudioTrack_2_ID" + ":$AudioTrack_2_default_sansID"
$track_order += ",0:$AudioTrack_2_ID"
}
}
elseif ($NB_Audio_MKV1 -ne 0) {
write-host "Problem with `$NB_Audio_MKV1=$NB_Audio_MKV1.`n`$NB_Audio_MKV1 must be set to : 0, 1 or 2" -ForegroundColor "Red"
exit
}
}
else {
# 2 MKV are to be merged, keeping audio from MKV_1 and only video from MKV_2
# We change the video track ID because it's on the MKV_2 !!
$VideoTrack_ID = "0" # May not be usefull... TO-DO !!!!
# ########################################################
# Extracting the episode number of the OnePiece Serie (must be commented if not used...)
# Generating pattern string to search for the right MKV2 file in case of filename differents...
# $MKV1_search_pattern = '(.*) - (\d{3}) -(.*)'
# # $chain__to_replace = ''
# $MKV1_chain_to_replace_for_MKV2 = '$1 - $2'
# $MKV_2_search_pattern = $MKV_1_name -replace $MKV1_search_pattern, $MKV1_chain_to_replace_for_MKV2
$MKV_2_search_pattern = $MKV_1_name
# Get MKV2 filename
$MKV_2_file = get-ChildItem $sourceDirectory_2 | where { $_.name -like "*$MKV_2_search_pattern*" } | select name
$FormatName_2 = $MKV_2_file.Name.ToString()
$MKV_2_name = $FormatName_2.Substring(0, $FormatName_2.Length - ($VideoExtension_2.Length + 1))
$MKV_2 = $sourceDirectory_2 + "\" + $MKV_2_name + ".$VideoExtension_2"
# Now that MKV_2 is defined, we can create the MKVmerge parameters, witch will be common to all merge.
$MKVMerge_param_mkv2 = "--no-audio --no-subtitles --no-track-tags --no-global-tags --language 0:$Video_Lang --track-name `"0:$VideoTrackName`" --default-track 0:yes `"$MKV_2`""
if (( $NB_Audio_MKV1 -gt 0 ) -and ($NB_Audio_MKV1 -le 2)) {
# $NB_Audio_MKV1 = 1 or 2
$MKVMerge_audio_param = "--language $AudioTrack_1_ID" + ":$AudioLang_1_sansID --track-name `"$AudioTrack_1_ID" + ":$AudioTrackName_1_sansID`" --default-track $AudioTrack_1_ID" + ":$AudioTrack_1_default_sansID"
$track_order = "1:0,0:$AudioTrack_1_ID"
if ( $NB_Audio_MKV1 -eq 2 ) {
$MKVMerge_audio_param += " --language $AudioTrack_2_ID" + ":$AudioLang_2_sansID --track-name `"$AudioTrack_2_ID" + ":$AudioTrackName_2_sansID`" --default-track $AudioTrack_2_ID" + ":$AudioTrack_2_default_sansID"
$track_order += ",0:$AudioTrack_2_ID"
}
}
elseif ($NB_Audio_MKV1 -ne 0) {
write-host "Problem with `$NB_Audio_MKV1=$NB_Audio_MKV1.`n`$NB_Audio_MKV1 must be set to : 0, 1 or 2" -ForegroundColor "Red"
exit
}
}
if ( $Internal_SUB_Number -ne 0 ) {
# Sub n°1 to keep
# Name and language of Subtitle Track n°1 + Sync Value
$SubTrackName_1 = "$Internal_Sub_1_ID" + ":$SubTrackName_1_sansID"
$SubTrackLang_1 = "$Internal_Sub_1_ID" + ":$SubTrackLang_1_sansID"
$SubTrack_1_default = "$Internal_Sub_1_ID" + ":$SubTrack_1_default_sansID"
$SubTrack_1_forced_track = "$Internal_Sub_1_ID" + ":$SubTrack_1_forced_track_sansID"
$SubTrack_1_SDH = "$Internal_Sub_1_ID" + ":$SubTrack_1_SDH_sansID"
$sub_charset_1 = "$Internal_Sub_1_ID" + ":$sub_charset_1_sansID"
# Define track order
$track_order += ",0:$Internal_Sub_1_ID"
$subtitle_tracks = "$Internal_Sub_1_ID"
$MKVMerge_sub_param = "--sub-charset $sub_charset_1 --language $SubTrackLang_1 --track-name `"$SubTrackName_1`" --default-track $SubTrack_1_default --forced-track $SubTrack_1_forced_track --hearing-impaired-flag $SubTrack_1_SDH"
if ( $Internal_SUB_Number -ge 2 ) {
# Sub n°2 to keep
# Name and language of Subtitle Track n°1 + Sync Value
$SubTrackName_2 = "$Internal_Sub_2_ID" + ":$SubTrackName_2_sansID"
$SubTrackLang_2 = "$Internal_Sub_2_ID" + ":$SubTrackLang_2_sansID"
$SubTrack_2_default = "$Internal_Sub_2_ID" + ":$SubTrack_2_default_sansID"
$SubTrack_2_forced_track = "$Internal_Sub_2_ID" + ":$SubTrack_2_forced_track_sansID"
$SubTrack_2_SDH = "$Internal_Sub_2_ID" + ":$SubTrack_2_SDH_sansID"
$sub_charset_2 = "$Internal_Sub_2_ID" + ":$sub_charset_2_sansID"
# Define track order
$track_order += ",0:$Internal_Sub_2_ID"
$subtitle_tracks += ",$Internal_Sub_2_ID"
$MKVMerge_sub_param += " --sub-charset $sub_charset_2 --language $SubTrackLang_2 --track-name `"$SubTrackName_2`" --default-track $SubTrack_2_default --forced-track $SubTrack_2_forced_track --hearing-impaired-flag $SubTrack_2_SDH"
}
if ( $Internal_SUB_Number -eq 3 ) {
# Sub n°3 to keep
# Name and language of Subtitle Track n°1 + Sync Value
$SubTrackName_3 = "$Internal_Sub_3_ID" + ":$SubTrackName_3_sansID"
$SubTrackLang_3 = "$Internal_Sub_3_ID" + ":$SubTrackLang_3_sansID"
$SubTrack_3_default = "$Internal_Sub_3_ID" + ":$SubTrack_3_default_sansID"
$SubTrack_3_forced_track = "$Internal_Sub_3_ID" + ":$SubTrack_3_forced_track_sansID"
$SubTrack_3_SDH = "$Internal_Sub_3_ID" + ":$SubTrack_3_SDH_sansID"
$sub_charset_3 = "$Internal_Sub_3_ID" + ":$sub_charset_3_sansID"
# Define track order
$track_order += ",0:$Internal_Sub_3_ID"
$subtitle_tracks += ",$Internal_Sub_3_ID"
$MKVMerge_sub_param += " --sub-charset $sub_charset_3 --language $SubTrackLang_3 --track-name `"$SubTrackName_3`" --default-track $SubTrack_3_default --forced-track $SubTrack_3_forced_track --hearing-impaired-flag $SubTrack_3_SDH"
}
$subtitle_tracks = "`"$subtitle_tracks`""
}
if ( ( $External_SUB_Number -ne 0 ) -and ( $External_SUB -eq $true ) ) {
# First external SRT to include
$SRT_1_Name = $MKV_1_name + ".$SubExtension_1"
$SRT_1 = $sourceDirectory_1 + "\" + $SRT_1_name
# Subtitles are elsewhere...
# $SRT_1_Name = "3_English.srt"
# $SRT_1 = $sourceDirectory_1 + "\" + $SRT_1_name
# $SRT_1 = $sourceDirectory_1 + "\Subs\" + $MKV_1_name + "\" + $SRT_1_name
If (-Not (Test-Path $SRT_1) ) {
Write-Host "File NON-EXISTANT - $SRT_1" -ForegroundColor "Red"
Write-Host "Check the filename or the script. ABORT..." -ForegroundColor "Red"
Exit
}
$track_order += ",$track_order_EXT_sub"
$MKVMerge_param_srt1 = "--sub-charset $Extsub_charset_1 --language $ExtSubTrackLang_1 --track-name `"$ExtSubTrackName_1`" --default-track $ExtSubTrack_1_default --forced-track $ExtSub_forced_track_1 --hearing-impaired-flag $ExtSub_SDH_1 `"$SRT_1`""
if ( $External_SUB_Number -eq 2 ) {
# Second external SRT to include
$SRT_2_Name = $MKV_1_name + ".$SubExtension_2"
$SRT_2 = $sourceDirectory_1 + "\" + $SRT_2_name
# Subtitles are elsewhere...
# $SRT_2_Name = "3_English.srt"
# $SRT_2 = $sourceDirectory_1 + "\" + $SRT_2_name
# $SRT_2 = $sourceDirectory_1 + "\Subs\" + $MKV_1_name + "\" + $SRT_2_name
$MKVMerge_param_srt2 = "--sub-charset $Extsub_charset_2 --language $ExtSubTrackLang_2 --track-name `"$ExtSubTrackName_2`" --default-track $ExtSubTrack_2_default --forced-track $ExtSub_forced_track_2 --hearing-impaired-flag $ExtSub_SDH_2 `"$SRT_2`""
}
}
$MKVMerge_param_start = "--output `"$Output`" --title `"$VideoTrackName`" --track-order `"$track_order`""
if ( $Internal_SUB_Number -eq 0 ) {
$MKVMerge_param_start += " --no-subtitles"
}
else {
# Part for internal subtitles
$MKVMerge_param_start += " --subtitle-tracks $subtitle_tracks"
}
if ($merge_another_video_MKV2 -eq $false) {
# Video is from MKV_1
$MKVMerge_param_start += " --track-name `"$VideoTrack_ID" + ":$VideoTrackName`" --default-track $VideoTrack_ID" + ":yes --language $VideoTrack_ID" + ":$Video_Lang"
}
else {
# If Video is from MKV_2, we don't want the video from MKV_1 added...
$MKVMerge_param_start += " --no-video"
}
if ( $no_attachements -eq $true ) {
$MKVMerge_param_start += " --no-attachments"
}
if ( $Internal_SUB_Number -ne 0 ) {
# Part for internal subtitles
$MKVMerge_param_mkv1 = "$MKVMerge_audio_param $MKVMerge_sub_param `"$MKV_1`""
}
else {
$MKVMerge_param_mkv1 = "$MKVMerge_audio_param `"$MKV_1`""
}
$MKVMerge_param_all = "$MKVMerge_param_start $MKVMerge_param_mkv1"
if ( $merge_another_video_MKV2 -eq $true ) {
$MKVMerge_param_all += " $MKVMerge_param_mkv2"
}
if ( ( $External_SUB_Number -ne 0 ) -and ( $External_SUB -eq $true ) ) {
$MKVMerge_param_all += " $MKVMerge_param_srt1"
if ( $External_SUB_Number -eq 2 ) {
$MKVMerge_param_all += " $MKVMerge_param_srt2"
}
}
# Write-Host "`$MKVMerge_param_start = $MKVMerge_param_start" -ForegroundColor "White"
# Write-Host "`$MKVMerge_param_mkv1 = $MKVMerge_param_mkv1" -ForegroundColor "White"
# Write-Host "`$MKVMerge_param_all = $MKVMerge_param_all" -ForegroundColor "White"
Write-Host "`tRecap. of all language tracks :" -ForegroundColor "Cyan"
Write-Host "`t`t- Video lang will be : $Video_Lang" -ForegroundColor "Cyan"
Write-Host "`t`t- Audio 1 lang will be : $AudioLang_1_sansID`t`tand name = $AudioTrackName_1_sansID" -ForegroundColor "Cyan"
if ($NB_Audio_MKV1 -eq 2) {
Write-Host "`t`t- Audio 2 lang will be : $AudioLang_2_sansID`t`tand name = $AudioTrackName_2_sansID" -ForegroundColor "Cyan"
}
if ($Internal_SUB_Number -ge 1) {
Write-Host "`t`t- Internal Sub 1 lang will be : $SubTrackLang_1_sansID`tand name = $SubTrackName_1_sansID" ( &{ If ( ($SubTrack_1_SDH_sansID -eq "yes") -eq $true) { "`tFlagged with SDH (--hearing-impaired-flag)" } }) -ForegroundColor "Cyan"
if ($Internal_SUB_Number -ge 2) {
Write-Host "`t`t- Internal Sub 2 lang will be : $SubTrackLang_2_sansID`tand name = $SubTrackName_2_sansID" ( &{ If ( ($SubTrack_2_SDH_sansID -eq "yes") -eq $true) { "`tFlagged with SDH (--hearing-impaired-flag)" } }) -ForegroundColor "Cyan"
if ($Internal_SUB_Number -eq 3) {
Write-Host "`t`t- Internal Sub 3 lang will be : $SubTrackLang_3_sansID`tand name = $SubTrackName_3_sansID" ( &{ If ( ($SubTrack_3_SDH_sansID -eq "yes") -eq $true) { "`tFlagged with SDH (--hearing-impaired-flag)" } }) -ForegroundColor "Cyan"
}
}
}
if ($External_SUB -eq $true) {
Write-Host "`t`t- External Sub 1 lang will be : $($ExtSubTrackLang_1 -replace '0:', '')`tand name = $($ExtSubTrackName_1 -replace '0:', '')" ( &{ If ( ($ExtSub_SDH_1 -match "yes") -eq $true) { "`tFlagged with SDH (--hearing-impaired-flag)" } }) -ForegroundColor "Cyan"
if ($External_SUB_Number -eq 2) {
Write-Host "`t`t- External Sub 2 lang will be : $($ExtSubTrackLang_2 -replace '0:', '')`tand name = $($ExtSubTrackName_1 -replace '0:', '')" ( &{ If ( ($ExtSub_SDH_2 -match "yes") -eq $true) { "`tFlagged with SDH (--hearing-impaired-flag)" } }) -ForegroundColor "Cyan"
}
}
# Last step to command construction
$command = "& $MKVMerge $MKVMerge_param_all"
# Launch begin...
Invoke-Expression $command
Write-Host ""
Write-Host "Fin du traitement du fichier n° $compteur / $Count_1.`n" -ForegroundColor "black" -BackgroundColor "white"
If (-Not (Test-Path $Output) ) {
write-host "File NON-EXISTANT - $Output" -foreground "red"
"File NON-EXISTANT - $Output" | Out-File "$destinationDirectory\Errors.txt" -Append
}
else {
if ( $move_mkv1_after_merge -eq $true ) {
Move-Item -Path $MKV_1 -Destination $Path_Folder_NotMerged_1 -Verbose
if ( $External_SUB -eq $true ) {
Move-Item -Path $SRT_1 -Destination $Path_Folder_NotMerged_1 -Verbose
if ( $External_SUB_Number -eq 2 ) {
Move-Item -Path $SRT_2 -Destination $Path_Folder_NotMerged_1 -Verbose
}
}
}
if ( ( $move_mkv2_after_merge -eq $true ) -And ( $merge_another_video_MKV2 -eq $true) ) {
Move-Item -Path $MKV_2 -Destination $Path_Folder_NotMerged_2 -Verbose
}
}
$compteur++
}
}