diff --git a/Scripts-Divers/Batch MULTI-Merge one MKV (Vid,Aud,Sub) with another MKV Video with external SRT.ps1 b/Scripts-Divers/Batch MULTI-Merge one MKV (Vid,Aud,Sub) with another MKV Video with external SRT.ps1 new file mode 100644 index 0000000..198bb0a --- /dev/null +++ b/Scripts-Divers/Batch MULTI-Merge one MKV (Vid,Aud,Sub) with another MKV Video with external SRT.ps1 @@ -0,0 +1,808 @@ +##============================================================================================== +## ## +## 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 ## +## ## +##============================================================================================== +# Pour ONE PIECE !!!! +# ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ +# │ - 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_separate = $false # Set this to $true if there is at least 2 internal sub in the same lang + $Internal_SUB_1_title_to_search = "FR" # 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 = "Français - SRT" + $SubTrackLang_1_sansID = "fr" # en for english / fr for french / jp for japanese + $SubTrack_1_default_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" + $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" + $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 = $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 ) { + 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)) { + $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)) { + $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)) { + $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" + Write-Host "`t`$AudioTrack_1_ID = $AudioTrack_1_ID`tLang set in the script = $AudioTrack_1_Lang" -ForegroundColor "Yellow" + 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" -ForegroundColor "Yellow" + Write-Host "`t`$Internal_Sub_2_ID = $Internal_Sub_2_ID`tLang set in the script = $Internal_SUB_Lang_2" -ForegroundColor "Yellow" + Write-Host "`t`$Internal_Sub_3_ID = $Internal_Sub_3_ID`tLang set in the script = $Internal_SUB_Lang_3" -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 ) { + $VideoTrackName = $VideoTrackName -replace '(\d{4}(?=.*S\d{2}E\d{2}))', '($1)' + } + if ( $remove_dots -eq $true ) { + $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 $JSON_Export $JSON_file + + # Exception for some files !!! ----------------------------------------------------------------- + if ( $MKV_1_name.Substring(0, 26) -eq "MY_FILE_1" ) { + $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 "MY_FILE_3" ) { + # $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." + # } + + # 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 ) { + + # TO-DO : RE_CHECK THIS PART (for internal SUB) + + # if ($sub_id_persistent -eq $false) { + # $sub_1_id, $sub_2_id = Get-SubID $MKV_1_name + # } + # # Track 2 = Sub n°1 to keep + # # Name and language of Subtitle Track n°1 + Sync Value + # $SubTrackName_1 = $sub_1_id + ":$SubTrackName_1_sansID" + # $SubTrackLang_1 = $sub_1_id + ":$SubTrackLang_1_sansID" + # $SubTrack_1_default = $sub_1_id + ":$SubTrack_1_default_sansID" + # $sub_charset_1 = $sub_1_id + ":$sub_charset_1_sansID" + # # Define track order + # $track_order += ",0:$sub_1_id" + # $subtitle_tracks = "$sub_1_id" + # $MKVMerge_sub_param = "--sub-charset $sub_charset_1 --language $SubTrackLang_1 --track-name `"$SubTrackName_1`" --default-track $SubTrack_1_default" + + # if ( $Internal_SUB_Number -eq 2 ) { + # # Track 3 = Sub n°2 to keep + # # Name and language of Subtitle Track n°1 + Sync Value + # $SubTrackName_2 = $sub_2_id + ":$SubTrackName_2_sansID" + # $SubTrackLang_2 = $sub_2_id + ":$SubTrackLang_2_sansID" + # $SubTrack_2_default = $sub_2_id + ":$SubTrack_2_default_sansID" + # $sub_charset_2 = $sub_2_id + ":$sub_charset_2_sansID" + # # Define track order + # $track_order += ",0:$sub_2_id" + # $subtitle_tracks += ",$sub_2_id" + # $MKVMerge_sub_param += " --sub-charset $sub_charset_2 --language $SubTrackLang_2 --track-name `"$SubTrackName_2`" --default-track $SubTrack_2_default" + # } + } + + 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 { + # TO-DO REWRITE THIS CODE (for internal SUB) + # $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 ) { + # CODE TO BE written + } + 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" + + # 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++ + + + } +} \ No newline at end of file diff --git a/Scripts-Divers/Batch Merge one MKV Audio with another video MKV.ps1 b/Scripts-Divers/Batch Merge one MKV Audio with another video MKV.ps1 index 856d82e..4de6516 100644 --- a/Scripts-Divers/Batch Merge one MKV Audio with another video MKV.ps1 +++ b/Scripts-Divers/Batch Merge one MKV Audio with another video MKV.ps1 @@ -25,430 +25,455 @@ ##============================================================================================== Clear-Host +Invoke-Command -ScriptBlock { -# Set MKVMerge.exe Path -$MKVMerge = "`"H:\z_MKV\mkvtoolnix\mkvmerge.exe`"" -# For the parameters to pass to MKVMerge executable, will be construct + # Set MKVMerge.exe Path + $MKVMerge = "`"PATH_TO\mkvtoolnix\mkvmerge.exe`"" + # For the parameters to pass to MKVMerge executable, will be construct -#Set Source and Target directories (Don't put an \ at the end) -$sourceDirectory_1 = "PATH_TO_SOURCE_1" -$sourceDirectory_2 = "PATH_TO_SOURCE_2" -$destinationDirectory = "PATH_TO_DESTINATION" + #Set Source and Target directories (Don't put an \ at the end) + $sourceDirectory_1 = "PATH_TO_SOURCE_1" + $sourceDirectory_2 = "PATH_TO_SOURCE_2" + $destinationDirectory = "PATH_TO_DESTINATION" -# Rename output file with this settings : -# $chain_to_search = '' -# $chain_to_search = '(\w+)\.S(\d{2})E(\d{2}).(.*)H264(.*)' -$chain_to_search = '(\w+)\.S(\d{2})E(\d{2})(.*)1080p.(.*)H.264(.*)' -# $chain__to_replace = '' -# $chain__to_replace = '$1 - S$2E$3 - $4x265-10bits$5--Reencoded' -$chain__to_replace = '$1 (2021) - S$2E$3 - 1080p.$5x265-10bits$6--Reencoded' + # Rename output file with this settings : + # Use https://regex101.com/r/cU5lC2/1 to get the RegEx + $chain_to_search = '' + # $chain_to_search = '(\w+)\.S(\d{2})E(\d{2}).(.*)H264(.*)' + # $chain_to_search = '(\w+)\.S(\d{2})E(\d{2})(.*)1080p.(.*)H.264(.*)' + $chain__to_replace = '' + # $chain__to_replace = '$1 - S$2E$3 - $4x265-10bits$5--Reencoded' + # $chain__to_replace = '$1 (2021) - S$2E$3 - 1080p.$5x265-10bits$6--Reencoded' -# The filenames must be like : blabla.S00E00.blabla.H.264.blabla -# They will be renamed to : blabla - S00E00 - blabla.x265-10bits.blabla--Reencoded -# If you don't want the rename to be made, juste set to '' the variables. + # The filenames must be like : blabla.S00E00.blabla.H.264.blabla + # They will be renamed to : blabla - S00E00 - blabla.x265-10bits.blabla--Reencoded + # If you don't want the rename to be made, juste set to '' the variables. -# If there is no internal subtitles to keep : -# Set to $false for no internal subtitle -# Set to $true for one or more internal subtitles -$Internal_SUB = $true + # If there is no internal subtitles to keep : + # Set to $false for no internal subtitle + # Set to $true for one or more internal subtitles + $Internal_SUB = $true -# Is there an external subtitle ? Set the number of .SRT (0 - 2) -# It's exclusive, no internal SUB will be proceed.. -$NB_External_SUB = 0 + # Is there an external subtitle ? Set the number of .SRT (0 - 2) + # It's exclusive, no internal SUB will be proceed.. + $NB_External_SUB = 0 -# Extension without the . -$MkvExtension = "mkv" -#Set Subtitle Extension (Don't add the . before the extension) -$SubExtension_1 = 'eng.srt' -$SubExtension_2 = 'fre.srt' + # Extension without the . + $MkvExtension = "mkv" + #Set Subtitle Extension (Don't add the . before the extension) + $SubExtension_1 = 'eng.srt' + $SubExtension_2 = 'fre.srt' -#If source and destination are the same folder, set this to True, Otherwise let it to "False" -$merged_SUFFIX_name = "False" + #If source and destination are the same folder, set this to True, Otherwise let it to "False" + $merged_SUFFIX_name = "False" -# Move MKV1 (and SRT) to Not-Merged folder ? Set to $false or $true -$move_mkv1_after_merge = $true -# Move MKV2 (and SRT) to Not-Merged folder ? Set to $false or $true -$move_mkv1_after_merge = $true + # Move MKV1 (and SRT) to Not-Merged folder ? Set to $false or $true + $move_mkv1_after_merge = $true + # Move MKV2 (and SRT) to Not-Merged folder ? Set to $false or $true + $move_mkv1_after_merge = $true -# Initialization for those 3 variables, do not change them ! -$MKVMerge_sub_param = "" -$MKVMerge_audio_param = "" -$subtitle_tracks = "" -# ################## + # Initialization for those 3 variables, do not change them ! + $MKVMerge_sub_param = "" + $MKVMerge_audio_param = "" + $subtitle_tracks = "" + # ################## -#### FILE 1 - Keeping all but the video -# ================== AUDIO ================== -# Track 1 = Audio n°1 -# Name and language of Audio Track n°1 -$AudioTrackName_1 = "1:English - DDP 5.1" -$AudioLang_1 = "1:eng" -$AudioTrack_1_default = "1:yes" -# Define track order. Do not modify it unless you changed the track number in this section -$track_order = "1:0,0:1" -# $MKVMerge_audio_param = '--language', "$AudioLang_1", '--track-name', "$AudioTrackName_1", '--default-track', "$AudioTrack_1_default" -$MKVMerge_audio_param = "--language $AudioLang_1 --track-name `"$AudioTrackName_1`" --default-track $AudioTrack_1_default" + #### FILE 1 - Keeping all but the video + # ================== AUDIO ================== + # Track 1 = Audio n°1 + # Name and language of Audio Track n°1 + $AudioTrackName_1 = "1:English - DDP 5.1" + $AudioLang_1 = "1:eng" + $AudioTrack_1_default = "1:yes" + # Define track order. Do not modify it unless you changed the track number in this section + $track_order = "1:0,0:1" + # $MKVMerge_audio_param = '--language', "$AudioLang_1", '--track-name', "$AudioTrackName_1", '--default-track', "$AudioTrack_1_default" + $MKVMerge_audio_param = "--language $AudioLang_1 --track-name `"$AudioTrackName_1`" --default-track $AudioTrack_1_default" -# # Track 2 = Audio n°2 -# # Name and language of Audio Track n°2 -# $AudioTrackName_1 = "2:English - DDP 5.1" -# $AudioLang_1 = "2:en" -# $AudioTrack_1_default = "2:yes" -# # Define track order. Do not modify it unless you changed the track number in this section -# $track_order = "1:0,0:1,0:2" -# $MKVMerge_audio_param += "--language $AudioLang_2 --track-name `"$AudioTrackName_2`" --default-track $AudioTrack_2_default" + # # Track 2 = Audio n°2 + # # Name and language of Audio Track n°2 + # $AudioTrackName_1 = "2:English - DDP 5.1" + # $AudioLang_1 = "2:en" + # $AudioTrack_1_default = "2:yes" + # # Define track order. Do not modify it unless you changed the track number in this section + # $track_order = "1:0,0:1,0:2" + # $MKVMerge_audio_param += "--language $AudioLang_2 --track-name `"$AudioTrackName_2`" --default-track $AudioTrack_2_default" -########################################## -#### FILE 2 - MKV - Keeping only the video -#### $file_2_options = "--no-audio --no-track-tags --no-global-tags" + ########################################## + #### FILE 2 - MKV - Keeping only the video + #### $file_2_options = "--no-audio --no-track-tags --no-global-tags" -if ( $NB_External_SUB -ne 0 ) { - # ================== EXTERNAL SUBTITLE ================== - # Must have the same name as MKV1 - #### - #### FILE 3 - SRT - Keeping all but the video (audio + chapters tags) - # ================== SUBTITLES ================== - # Track 0 = Sub n°1 to keep - # Name and language of Subtitle Track n°0 + Sync Value - $ExtSubTrackName_1 = "0:English - SRT" - $ExtSubTrackLang_1 = "0:eng" - $ExtSubTrack_1_default = "0:yes" - $ExtSub_charset_1 = "0:UTF-8" - # Define track order - $track_order += ",2:0" - #### - #### FILE 4 - SRT - Keeping all but the video (audio + chapters tags) - # ================== SUBTITLES ================== - # Track 0 = Sub n°1 to keep - # Name and language of Subtitle Track n°0 + Sync Value - $ExtSubTrackName_2 = "0:Français - SRT" - $ExtSubTrackLang_2 = "0:fr" - $ExtSubTrack_2_default = "0:no" - $ExtSub_charset_2 = "0:UTF-8" - # Define track order - if ( $NB_External_SUB -eq 2 ) { - $track_order += ",3:0" - } -} -else { - # ================== INTERNAL SUBTITLES ================== - if ( $Internal_SUB -eq $true ) { - # Track 2 = Sub n°1 to keep - # Name and language of Subtitle Track n°1 + Sync Value - # $SubTrackName_1 = "2:English - SRT" - # $SubTrackLang_1 = "2:en" - # $SubTrack_1_default = "2:yes" - # $sub_charset_1 = "2:UTF-8" - # # Define track order - # $track_order += ",0:2" - # $subtitle_tracks = "2" - # # $MKVMerge_sub_param = '--sub-charset', "$sub_charset_1", '--language', "$SubTrackLang_1", '--track-name', "$SubTrackName_1", '--default-track', "$SubTrack_1_default" - # $MKVMerge_sub_param = "--sub-charset $sub_charset_1 --language $SubTrackLang_1 --track-name `"$SubTrackName_1`" --default-track $SubTrack_1_default" - - # Track 3 = Sub n°2 to keep - # Name and language of Subtitle Track n°1 + Sync Value - $SubTrackName_2 = "3:English SDH - SRT" - $SubTrackLang_2 = "3:eng" - $SubTrack_2_default = "3:yes" - $sub_charset_2 = "3:UTF-8" - # Define track order - $track_order += ",0:3" - $subtitle_tracks = "3" - $MKVMerge_sub_param += "--sub-charset $sub_charset_2 --language $SubTrackLang_2 --track-name `"$SubTrackName_2`" --default-track $SubTrack_2_default" + if ( $NB_External_SUB -ne 0 ) { + # ================== EXTERNAL SUBTITLE ================== + # Must have the same name as MKV1 #### + #### FILE 3 - SRT - Keeping all but the video (audio + chapters tags) + # ================== SUBTITLES ================== + # Track 0 = Sub n°1 to keep + # Name and language of Subtitle Track n°0 + Sync Value + $ExtSubTrackName_1 = "0:English - SRT" + $ExtSubTrackLang_1 = "0:eng" + $ExtSubTrack_1_default = "0:yes" + $ExtSub_charset_1 = "0:UTF-8" + # Define track order + $track_order += ",2:0" + #### + #### FILE 4 - SRT - Keeping all but the video (audio + chapters tags) + # ================== SUBTITLES ================== + # Track 0 = Sub n°1 to keep + # Name and language of Subtitle Track n°0 + Sync Value + $ExtSubTrackName_2 = "0:Français - SRT" + $ExtSubTrackLang_2 = "0:fr" + $ExtSubTrack_2_default = "0:no" + $ExtSub_charset_2 = "0:UTF-8" + # Define track order + if ( $NB_External_SUB -eq 2 ) { + $track_order += ",3:0" + } } else { - # No internal subtitles are to be kept... - $MKVMerge_sub_param = "" - } -} -## End of part where there is something to change ! -##============================================================================================== - -# Testing if the Not-Merged folder already exists : if yes, it will be renamed, else it will be created. -$Path_Folder_NotMerged_2 = $sourceDirectory_2 + "\Not-Merged" -If(!(test-path $Path_Folder_NotMerged_2)) { - New-Item -ItemType "Directory" -Force -Path $Path_Folder_NotMerged_2 -Verbose -} else { - # Don't know what's the best... TO BE IMPROVED - #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose - #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose -} -# Testing if the Not-Merged folder already exists : if yes, it will be renamed, else it will be created. -$Path_Folder_NotMerged_1 = $sourceDirectory_1 + "\Not-Merged" -If(!(test-path $Path_Folder_NotMerged_1)) { - New-Item -ItemType "Directory" -Force -Path $Path_Folder_NotMerged_1 -Verbose -} else { - # Don't know what's the best... TO BE IMPROVED - #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose - #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose -} - -#Process -$MKV_1_List = Get-ChildItem $sourceDirectory_1 -Filter "*.mkv" | ForEach-Object { $_.FullName } | Sort-Object -$Count_1 = $MKV_1_List.count -Write-Host "$Count_1 MKV's to be processed in $sourceDirectory_1." - -$MKV_2_List = Get-ChildItem $sourceDirectory_2 -Filter "*.mkv" | ForEach-Object { $_.FullName } | Sort-Object -$Count_2 = $MKV_2_List.count -Write-Host "$Count_2 MKV's to be processed in $sourceDirectory_2." - -# ######################### -# Check if there is the same number of .srt files as .mkv files -if ( $NB_External_SUB -ne 0 ) { - if ( $NB_External_SUB -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 MKV's to be processed and $Count_1_SRT SRT's 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 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 ( $NB_External_SUB -eq 2 ) { # Only if $NB_External_SUB = 2 - $SRT_2_List = Get-ChildItem $sourceDirectory_1 -Filter "*.$SubExtension_2" | ForEach-Object { $_.FullName } | Sort-Object - $Count_2_SRT = $SRT_2_List.count - if ($Count_1 -eq $Count_2_SRT) { - Write-Host "There is the same number of MKV and SRT n°2 files in this folder. Let's continue." - } else { - Write-Host "The number of MKV and SRT n°2 files isn't the same. ABORT..." -foreground "red" - Exit - } - } -} -# ######################### - -# ######################### -# 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 -} -# if (!($Count_1 -eq ($filename_ep_final - $filename_ep_start + 1))) { -# Write-Host "The number of episodes set in the script isn't the same as the number of files in the folders... EXITING NOW !" -foreground "red" -# Exit -# } -# ######################### -$compteur = 1 - -# for ($i = $filename_ep_start; $i -lt $filename_ep_final+1; $i++) { -Foreach ($MKV_1 in $MKV_1_List) { - - # Genreating File Name - # format = filename_1_part_1 + S + filename_season + E + i_counter + filename_1_part_2 + .mkv - # format = filename_2_part_1 + S + filename_season + E + i_counter + filename_2_part_2 + .mkv - $FormatName_1 = (Get-Item $MKV_1).Basename - $MKV_1_name = $FormatName_1.ToString() - - $MKV_2_file = get-ChildItem $sourceDirectory_2 -recurse | where {$_.name -like "*$MKV_1_name*"} | select name - - # Faire test si count = 1 -> ok Sinon soucis ! - - $FormatName_2 = $MKV_2_file.Name.ToString() - $MKV_2_name = $FormatName_2.Substring(0,$FormatName_2.Length-($MkvExtension.Length+1)) - $MKV_2 = $sourceDirectory_2 + "\" + $MKV_2_name + ".$MkvExtension" - - # Title for the video track and for the destination file - #$VideoTrackName = $MKV_1_name -replace '(.*).S(\d*)E(\d*).(.*)H.264(.*)', '$1 - S$2E$3 - $4x265-10bits$5--Reencoded' - $VideoTrackName = $MKV_1_name -replace $chain_to_search, $chain__to_replace - $VideoTrackName = "$VideoTrackName" - - ######################################################### - # Exceptions for some files with other than 2 SRT inside - # You must copy paste the default entries set in the begining of this script in the Default section - switch ($MKV_1_name) - { - "Defiance.S03E01E02.720p.The World We Seize.HDTV.X264-DIMENSION" - { - # # ================== AUDIO ================== - # # Track 1 = Audio n°1 - # # Name and language of Audio Track n°1 - # $AudioTrackName_1 = "1:English - DDP 5.1" - # $AudioLang_1 = "1:en" - # $AudioTrack_1_default = "1:yes" - - # # Only One subtitle track : n°2 - # $SubTrackName_1 = "3:English SDH - SRT" - # $SubTrackLang_1 = "3:en" - # $SubTrack_1_default = "3:yes" - # $sub_charset_1 = "3:UTF-8" - # # Define track order - # $track_order = "1:0,0:1,0:3" - # # Define subtitles track to keep - # $subtitle_tracks = "3" - - $VideoTrackName = "Defiance - S03E01-E02 - 720p.HDTV.x265-10bits-DIMENSION--Reencoded" - break - } - "Defiance.S02S02E12-E13.720p.All Things Must Pass--I Almost Prayed.HDTV.x264-KILLERS" - { - # # Track 1 = Audio n°1 - # # Name and language of Audio Track n°1 - # $AudioTrackName_1 = "1:English - AAC 2.0" - # $AudioLang_1 = "1:en" - # $AudioTrack_1_default = "1:yes" - - # # Only One subtitle track : n°2 + # ================== INTERNAL SUBTITLES ================== + if ( $Internal_SUB -eq $true ) { + # Track 2 = Sub n°1 to keep + # Name and language of Subtitle Track n°1 + Sync Value # $SubTrackName_1 = "2:English - SRT" # $SubTrackLang_1 = "2:en" # $SubTrack_1_default = "2:yes" # $sub_charset_1 = "2:UTF-8" # # Define track order - # $track_order = "1:0,0:1,0:2" - # # Define subtitles track to keep + # $track_order += ",0:2" # $subtitle_tracks = "2" + # # $MKVMerge_sub_param = '--sub-charset', "$sub_charset_1", '--language', "$SubTrackLang_1", '--track-name', "$SubTrackName_1", '--default-track', "$SubTrack_1_default" + # $MKVMerge_sub_param = "--sub-charset $sub_charset_1 --language $SubTrackLang_1 --track-name `"$SubTrackName_1`" --default-track $SubTrack_1_default" - $VideoTrackName = "Defiance - S02S02E12-E13 - PROPER.720p.HDTV.x265-10bits-KILLERS--Reencoded" - - break - } - #Default state - Default - { - # If not an exception, we don't want to modify the variables - # $SubTrackName_1 = "3:English SDH - SRT" - # $SubTrackLang_1 = "3:eng" - # $SubTrack_1_default = "3:yes" - # $sub_charset_1 = "3:UTF-8" - # # Define track order - # $track_order = "1:0,0:1,0:3" - # # Define subtitles track to keep - # $subtitle_tracks = "3" - break - } - } - ######################################################### - - # Set Output File Name - If ($merged_SUFFIX_name -eq "True") { - $Output = $destinationDirectory + "\" + $VideoTrackName + '___MERGED' + '.mkv' - } elseif ($merged_SUFFIX_name -eq "False") { - # $Output = $destinationDirectory + "\" + $VideoTrackName + '.mkv' - $Output = "$destinationDirectory" + "\" + "$VideoTrackName" + ".mkv" - } else { - write-host "Error in the value of the merged_SUFFIX_name variable. Current value = $merged_SUFFIX_name" -ForegroundColor "red" - write-host "Should be set to True or False.`nScript is exiting now..." -ForegroundColor "white" - Exit - } - # write-host "Output file will be :" -ForegroundColor "white" - # write-host "$Output" -ForegroundColor "white" - # write-host "--" -ForegroundColor "white" - - 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" - - if ( $NB_External_SUB -ne 0 ) { - - # First external SRT to include - $SRT_1_Name = $MKV_1_name + ".$SubExtension_1" - $SRT_1 = $sourceDirectory_1 + "\" + $SRT_1_name - - $MKVMerge_param_start = "---output `"$Output`" --title `"$VideoTrackName`" --track-order `"$track_order`" --no-video --no-subtitles" - $MKVMerge_param_mkv1 = "$MKVMerge_audio_param `"$MKV_1`"" - $MKVMerge_param_mkv2 = "--no-audio --no-track-tags --no-global-tags --language 0:en --track-name `"0:$VideoTrackName`" --default-track 0:yes --no-subtitles --no-audio `"$MKV_2`"" - - $MKVMerge_param_srt1 = "--sub-charset $Extsub_charset_1 --language $ExtSubTrackLang_1 --track-name `"$ExtSubTrackName_1`" --default-track $ExtSubTrack_1_default `"$SRT_1`"" - - $MKVMerge_param_srt_all = "$MKVMerge_param_srt1" - - if ( $NB_External_SUB -ne 0 ) { - # Second external SRT to include - $SRT_2_Name = $MKV_1_name + ".$SubExtension_2" - $SRT_2 = $sourceDirectory_1 + "\" + $SRT_2_name - - $MKVMerge_param_srt2 = "--sub-charset $Extsub_charset_2 --language $ExtSubTrackLang_2 --track-name `"$ExtSubTrackName_2`" --default-track $ExtSubTrack_2_default `"$SRT_2`"" - $MKVMerge_param_srt_all += " $MKVMerge_param_srt2" - } - - $MKVMerge_param_all = "$MKVMerge_param_start $MKVMerge_param_mkv1 $MKVMerge_param_mkv2 $MKVMerge_param_srt_all" - - } - else { - # INTERNAL SUB to keep - $MKVMerge_param_start = "--output `"$Output`" --title `"$VideoTrackName`" --track-order `"$track_order`" --subtitle-tracks $subtitle_tracks --no-video" - if ( $Internal_SUB -eq $true ) { - # There is some internal subtitles to keep - $MKVMerge_param_mkv1 = "$MKVMerge_audio_param $MKVMerge_sub_param `"$MKV_1`"" + # Track 3 = Sub n°2 to keep + # Name and language of Subtitle Track n°1 + Sync Value + $SubTrackName_2 = "3:English SDH - SRT" + $SubTrackLang_2 = "3:eng" + $SubTrack_2_default = "3:yes" + $sub_charset_2 = "3:UTF-8" + # Define track order + $track_order += ",0:3" + $subtitle_tracks = "3" + $MKVMerge_sub_param += "--sub-charset $sub_charset_2 --language $SubTrackLang_2 --track-name `"$SubTrackName_2`" --default-track $SubTrack_2_default" + #### } else { - # There is no internal subtitles to keep - $MKVMerge_param_mkv1 = "$MKVMerge_audio_param `"$MKV_1`"" + # No internal subtitles are to be kept... + $MKVMerge_sub_param = "" } - $MKVMerge_param_mkv2 = "--no-audio --no-track-tags --no-global-tags --language 0:en --track-name `"0:$VideoTrackName`" --default-track 0:yes --no-subtitles --no-audio `"$MKV_2`"" - - $MKVMerge_param_all = "$MKVMerge_param_start $MKVMerge_param_mkv1 $MKVMerge_param_mkv2" - - # ################## - # Debug - # Write-Host "$MKVMerge_param_start" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "$MKVMerge_audio_param" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "$MKVMerge_sub_param" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "$MKVMerge_param_mkv2" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "$MKVMerge_param_all" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "" -ForegroundColor "black" -BackgroundColor "DarkGray" - # Write-Host "Commande qui sera lancée :" -ForegroundColor "black" -BackgroundColor "DarkGray" - - # Write-Host "& $MKVMerge" "$MKVMerge_param_all" -ForegroundColor "black" -BackgroundColor "DarkGray" - # ################## } - - # Last step to command construction - $command = "& $MKVMerge $MKVMerge_param_all" - # Launch begin... - Invoke-Expression $command + ## End of part where there is something to change ! + ##============================================================================================== - Write-Host "" -ForegroundColor "black" -BackgroundColor "white" - Write-Host "Fin du traitement du fichier n° $compteur / $Count_1." -ForegroundColor "black" -BackgroundColor "white" - Write-Host "" -ForegroundColor "black" -BackgroundColor "white" + # Testing if the Not-Merged folder already exists : if yes, it will be renamed, else it will be created. + $Path_Folder_NotMerged_2 = $sourceDirectory_2 + "\Not-Merged" + If (!(test-path $Path_Folder_NotMerged_2)) { + New-Item -ItemType "Directory" -Force -Path $Path_Folder_NotMerged_2 -Verbose + } + else { + # Don't know what's the best... TO BE IMPROVED + #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose + #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose + } + # Testing if the Not-Merged folder already exists : if yes, it will be renamed, else it will be created. + $Path_Folder_NotMerged_1 = $sourceDirectory_1 + "\Not-Merged" + If (!(test-path $Path_Folder_NotMerged_1)) { + New-Item -ItemType "Directory" -Force -Path $Path_Folder_NotMerged_1 -Verbose + } + else { + # Don't know what's the best... TO BE IMPROVED + #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose + #Move-Item -Path $Path_Folder_NotMerged_2 -Destination "$sourceDirectory\Not-Merged--backup" -Verbose + } - If (Test-Path $Output) { - #Clean Up - #Remove-Item $MKV - #Remove-Item $Sub - #Remove-Item $OtherSub - # Move to folder Not-Merged, this folder's existence has already been tested, and the folder is created. - if ( $move_mkv1_after_merge -eq $true ) { - Move-Item -Path $MKV_1 -Destination $Path_Folder_NotMerged_1 -Verbose - if ( $NB_External_SUB -ne 0 ) { - Move-Item -Path $SRT_1 -Destination $Path_Folder_NotMerged_1 -Verbose - if ( $NB_External_SUB -eq 2 ) { - Move-Item -Path $SRT_2 -Destination $Path_Folder_NotMerged_2 -Verbose - } + #Process + $MKV_1_List = Get-ChildItem $sourceDirectory_1 -Filter "*.mkv" | ForEach-Object { $_.FullName } | Sort-Object + $Count_1 = $MKV_1_List.count + Write-Host "$Count_1 MKV's to be processed in $sourceDirectory_1." + + $MKV_2_List = Get-ChildItem $sourceDirectory_2 -Filter "*.mkv" | ForEach-Object { $_.FullName } | Sort-Object + $Count_2 = $MKV_2_List.count + Write-Host "$Count_2 MKV's to be processed in $sourceDirectory_2." + + # ######################### + # Check if there is the same number of .srt files as .mkv files + if ( $NB_External_SUB -ne 0 ) { + if ( $NB_External_SUB -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 MKV's to be processed and $Count_1_SRT SRT's 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 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 ( $NB_External_SUB -eq 2 ) { + # Only if $NB_External_SUB = 2 + $SRT_2_List = Get-ChildItem $sourceDirectory_1 -Filter "*.$SubExtension_2" | ForEach-Object { $_.FullName } | Sort-Object + $Count_2_SRT = $SRT_2_List.count + if ($Count_1 -eq $Count_2_SRT) { + Write-Host "There is the same number of MKV and SRT n°2 files in this folder. Let's continue." + } + else { + Write-Host "The number of MKV and SRT n°2 files isn't the same. ABORT..." -foreground "red" + Exit } } - if ( $move_mkv2_after_merge -eq $true ) { - Move-Item -Path $MKV_2 -Destination $Path_Folder_NotMerged_2 -Verbose - } } - Else { - write-host "File NON-EXISTANT - $Output" -foreground "red" - "File NON-EXISTANT - $Output" | Out-File "$destinationDirectory\Errors.txt" -Append + # ######################### + + # ######################### + # 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 + } + # if (!($Count_1 -eq ($filename_ep_final - $filename_ep_start + 1))) { + # Write-Host "The number of episodes set in the script isn't the same as the number of files in the folders... EXITING NOW !" -foreground "red" + # Exit + # } + # ######################### + $compteur = 1 + + # for ($i = $filename_ep_start; $i -lt $filename_ep_final+1; $i++) { + 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" + + # Genreating Files Name & Video Title + # format = filename_1_part_1 + S + filename_season + E + i_counter + filename_1_part_2 + .mkv + # format = filename_2_part_1 + S + filename_season + E + i_counter + filename_2_part_2 + .mkv + $FormatName_1 = (Get-Item $MKV_1).Basename + $MKV_1_name = $FormatName_1.ToString() + + $MKV_2_file = get-ChildItem $sourceDirectory_2 -recurse | where { $_.name -like "*$MKV_1_name*" } | select name + + # Faire test si count = 1 -> ok Sinon soucis ! + + $FormatName_2 = $MKV_2_file.Name.ToString() + $MKV_2_name = $FormatName_2.Substring(0, $FormatName_2.Length - ($MkvExtension.Length + 1)) + $MKV_2 = $sourceDirectory_2 + "\" + $MKV_2_name + ".$MkvExtension" + + # Title for the video track and for the destination file + $VideoTrackName = $MKV_1_name + if ( $set_year_with_brackets -eq $true ) { + $VideoTrackName = $VideoTrackName -replace '(\d{4}(?=.*S\d{2}E\d{2}))', '($1)' + } + if ( $remove_dots -eq $true ) { + $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 + } + + ######################################################### + # Exceptions for some files with other than 2 SRT inside + # You must copy paste the default entries set in the begining of this script in the Default section + switch ($MKV_1_name) { + "MY_FILE_WITH_EXCEPTION" { + # # ================== AUDIO ================== + # # Track 1 = Audio n°1 + # # Name and language of Audio Track n°1 + # $AudioTrackName_1 = "1:English - DDP 5.1" + # $AudioLang_1 = "1:en" + # $AudioTrack_1_default = "1:yes" + + # # Only One subtitle track : n°2 + # $SubTrackName_1 = "3:English SDH - SRT" + # $SubTrackLang_1 = "3:en" + # $SubTrack_1_default = "3:yes" + # $sub_charset_1 = "3:UTF-8" + # # Define track order + # $track_order = "1:0,0:1,0:3" + # # Define subtitles track to keep + # $subtitle_tracks = "3" + + $VideoTrackName = "Defiance - S03E01-E02 - 720p.HDTV.x265-10bits-DIMENSION--Reencoded" + break + } + "MY_FILE_WITH_EXCEPTION_2" { + # # Track 1 = Audio n°1 + # # Name and language of Audio Track n°1 + # $AudioTrackName_1 = "1:English - AAC 2.0" + # $AudioLang_1 = "1:en" + # $AudioTrack_1_default = "1:yes" + + # # Only One subtitle track : n°2 + # $SubTrackName_1 = "2:English - SRT" + # $SubTrackLang_1 = "2:en" + # $SubTrack_1_default = "2:yes" + # $sub_charset_1 = "2:UTF-8" + # # Define track order + # $track_order = "1:0,0:1,0:2" + # # Define subtitles track to keep + # $subtitle_tracks = "2" + + $VideoTrackName = "Defiance - S02S02E12-E13 - PROPER.720p.HDTV.x265-10bits-KILLERS--Reencoded" + + break + } + #Default state + Default { + # If not an exception, we don't want to modify the variables + # $SubTrackName_1 = "3:English SDH - SRT" + # $SubTrackLang_1 = "3:eng" + # $SubTrack_1_default = "3:yes" + # $sub_charset_1 = "3:UTF-8" + # # Define track order + # $track_order = "1:0,0:1,0:3" + # # Define subtitles track to keep + # $subtitle_tracks = "3" + break + } + } + ######################################################### + + # Set Output File Name + If ($merged_SUFFIX_name -eq "True") { + $Output = $destinationDirectory + "\" + $VideoTrackName + '___MERGED' + '.mkv' + } + elseif ($merged_SUFFIX_name -eq "False") { + # $Output = $destinationDirectory + "\" + $VideoTrackName + '.mkv' + $Output = "$destinationDirectory" + "\" + "$VideoTrackName" + ".mkv" + } + else { + write-host "Error in the value of the merged_SUFFIX_name variable. Current value = $merged_SUFFIX_name" -ForegroundColor "red" + write-host "Should be set to True or False.`nScript is exiting now..." -ForegroundColor "white" + Exit + } + # write-host "Output file will be :" -ForegroundColor "white" + # write-host "$Output" -ForegroundColor "white" + # write-host "--" -ForegroundColor "white" + + if ( $NB_External_SUB -ne 0 ) { + + # First external SRT to include + $SRT_1_Name = $MKV_1_name + ".$SubExtension_1" + $SRT_1 = $sourceDirectory_1 + "\" + $SRT_1_name + + $MKVMerge_param_start = "--output `"$Output`" --title `"$VideoTrackName`" --track-order `"$track_order`" --no-video --no-subtitles" + if ( $no_attachements -eq $true ) { + $MKVMerge_param_start += " --no-attachments" + } + + $MKVMerge_param_mkv1 = "$MKVMerge_audio_param `"$MKV_1`"" + $MKVMerge_param_mkv2 = "--no-audio --no-track-tags --no-global-tags --language 0:en --track-name `"0:$VideoTrackName`" --default-track 0:yes --no-subtitles --no-audio `"$MKV_2`"" + + $MKVMerge_param_srt1 = "--sub-charset $Extsub_charset_1 --language $ExtSubTrackLang_1 --track-name `"$ExtSubTrackName_1`" --default-track $ExtSubTrack_1_default `"$SRT_1`"" + + $MKVMerge_param_srt_all = "$MKVMerge_param_srt1" + + if ( $NB_External_SUB -eq 2 ) { + # Second external SRT to include + $SRT_2_Name = $MKV_1_name + ".$SubExtension_2" + $SRT_2 = $sourceDirectory_1 + "\" + $SRT_2_name + + $MKVMerge_param_srt2 = "--sub-charset $Extsub_charset_2 --language $ExtSubTrackLang_2 --track-name `"$ExtSubTrackName_2`" --default-track $ExtSubTrack_2_default `"$SRT_2`"" + $MKVMerge_param_srt_all += " $MKVMerge_param_srt2" + } + + $MKVMerge_param_all = "$MKVMerge_param_start $MKVMerge_param_mkv1 $MKVMerge_param_mkv2 $MKVMerge_param_srt_all" + + } + else { + # INTERNAL SUB to keep + $MKVMerge_param_start = "--output `"$Output`" --title `"$VideoTrackName`" --track-order `"$track_order`" --subtitle-tracks $subtitle_tracks --no-video" + if ( $Internal_SUB -eq $true ) { + # There is some internal subtitles to keep + $MKVMerge_param_mkv1 = "$MKVMerge_audio_param $MKVMerge_sub_param `"$MKV_1`"" + } + else { + # There is no internal subtitles to keep + $MKVMerge_param_mkv1 = "$MKVMerge_audio_param `"$MKV_1`"" + } + $MKVMerge_param_mkv2 = "--no-audio --no-track-tags --no-global-tags --language 0:en --track-name `"0:$VideoTrackName`" --default-track 0:yes --no-subtitles --no-audio `"$MKV_2`"" + + $MKVMerge_param_all = "$MKVMerge_param_start $MKVMerge_param_mkv1 $MKVMerge_param_mkv2" + + # ################## + # Debug + # Write-Host "$MKVMerge_param_start" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "$MKVMerge_audio_param" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "$MKVMerge_sub_param" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "$MKVMerge_param_mkv2" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "$MKVMerge_param_all" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "" -ForegroundColor "black" -BackgroundColor "DarkGray" + # Write-Host "Commande qui sera lancée :" -ForegroundColor "black" -BackgroundColor "DarkGray" + + # Write-Host "& $MKVMerge" "$MKVMerge_param_all" -ForegroundColor "black" -BackgroundColor "DarkGray" + # ################## + } + + # Last step to command construction + $command = "& $MKVMerge $MKVMerge_param_all" + # Launch begin... + Invoke-Expression $command + + Write-Host "" -ForegroundColor "black" -BackgroundColor "white" + Write-Host "Fin du traitement du fichier n° $compteur / $Count_1." -ForegroundColor "black" -BackgroundColor "white" + Write-Host "" -ForegroundColor "black" -BackgroundColor "white" + + If (Test-Path $Output) { + #Clean Up + #Remove-Item $MKV + #Remove-Item $Sub + #Remove-Item $OtherSub + # Move to folder Not-Merged, this folder's existence has already been tested, and the folder is created. + if ( $move_mkv1_after_merge -eq $true ) { + Move-Item -Path $MKV_1 -Destination $Path_Folder_NotMerged_1 -Verbose + if ( $NB_External_SUB -ne 0 ) { + Move-Item -Path $SRT_1 -Destination $Path_Folder_NotMerged_1 -Verbose + if ( $NB_External_SUB -eq 2 ) { + Move-Item -Path $SRT_2 -Destination $Path_Folder_NotMerged_2 -Verbose + } + } + } + if ( $move_mkv2_after_merge -eq $true ) { + Move-Item -Path $MKV_2 -Destination $Path_Folder_NotMerged_2 -Verbose + } + } + Else { + write-host "File NON-EXISTANT - $Output" -foreground "red" + "File NON-EXISTANT - $Output" | Out-File "$destinationDirectory\Errors.txt" -Append + } + $compteur++ } - $compteur++ -} -# ############################## -# Command History -# -# internal Subtitles : -# two subtiles to keep : -#& $MKVMerge --title "$VideoTrackName" --track-order "$track_order" --subtitle-tracks "$subtitle_tracks" -o "$Output" --no-video --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --sub-charset "$sub_charset_1" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default" --sub-charset "$sub_charset_2" --language "$SubTrackLang_2" --track-name "$SubTrackName_2" --default-track "$SubTrack_2_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" --no-subtitles --no-audio "$MKV_2" + # ############################## + # Command History + # + # internal Subtitles : + # two subtiles to keep : + #& $MKVMerge --title "$VideoTrackName" --track-order "$track_order" --subtitle-tracks "$subtitle_tracks" -o "$Output" --no-video --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --sub-charset "$sub_charset_1" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default" --sub-charset "$sub_charset_2" --language "$SubTrackLang_2" --track-name "$SubTrackName_2" --default-track "$SubTrack_2_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" --no-subtitles --no-audio "$MKV_2" -# Only one subtitles to keep -# & $MKVMerge --title "$VideoTrackName" --track-order "$track_order" --subtitle-tracks "$subtitle_tracks" -o "$Output" --no-video --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --sub-charset "$sub_charset_1" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" --no-subtitles --no-audio "$MKV_2" -# -# -# External Subtitles -# # 1 SRT -# & $MKVMerge --output "$Output" --title "$VideoTrackName" --track-order "$track_order" --no-video --no-subtitles --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" "$MKV_2" --sub-charset "$Extsub_charset_1" --language "$ExtSubTrackLang_1" --track-name "$ExtSubTrackName_1" --default-track "$ExtSubTrack_1_default" "$SRT_1" -# -# # 2 SRTs -# & $MKVMerge --title "$VideoTrackName" --track-order "$track_order" -o "$Output" --no-video --no-subtitles --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" "$MKV_2" --sub-charset "$ExtSub_charset_1" --language "$ExtSubTrackLang_1" --track-name "$ExtSubTrackName_1" --default-track "$ExtSubTrack_1_default" "$SRT_1" --sub-charset "$ExtSub_charset_2" --language "$ExtSubTrackLang_2" --track-name "$ExtSubTrackName_2" --default-track "$ExtSubTrack_2_default" "$SRT_2" \ No newline at end of file + # Only one subtitles to keep + # & $MKVMerge --title "$VideoTrackName" --track-order "$track_order" --subtitle-tracks "$subtitle_tracks" -o "$Output" --no-video --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --sub-charset "$sub_charset_1" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" --no-subtitles --no-audio "$MKV_2" + # + # + # External Subtitles + # # 1 SRT + # & $MKVMerge --output "$Output" --title "$VideoTrackName" --track-order "$track_order" --no-video --no-subtitles --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" "$MKV_2" --sub-charset "$Extsub_charset_1" --language "$ExtSubTrackLang_1" --track-name "$ExtSubTrackName_1" --default-track "$ExtSubTrack_1_default" "$SRT_1" + # + # # 2 SRTs + # & $MKVMerge --title "$VideoTrackName" --track-order "$track_order" -o "$Output" --no-video --no-subtitles --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" "$MKV_1" --no-audio --no-track-tags --no-global-tags --language "0:en" --track-name "0:$VideoTrackName" --default-track "0:yes" "$MKV_2" --sub-charset "$ExtSub_charset_1" --language "$ExtSubTrackLang_1" --track-name "$ExtSubTrackName_1" --default-track "$ExtSubTrack_1_default" "$SRT_1" --sub-charset "$ExtSub_charset_2" --language "$ExtSubTrackLang_2" --track-name "$ExtSubTrackName_2" --default-track "$ExtSubTrack_2_default" "$SRT_2" + +} \ No newline at end of file diff --git a/Scripts-Divers/Script Création de dossier suivant un pattern dans un dossier donné.ps1 b/Scripts-Divers/Script Création de dossier suivant un pattern dans un dossier donné.ps1 new file mode 100644 index 0000000..ceb3458 --- /dev/null +++ b/Scripts-Divers/Script Création de dossier suivant un pattern dans un dossier donné.ps1 @@ -0,0 +1,46 @@ +# Script pour créer un certain nombre de dossiers de même nom avec un n° qui change + +# Source : https://stackoverflow.com/questions/28631419/how-to-recursively-remove-all-empty-folders-in-powershell + +Clear-Host +Invoke-Command -ScriptBlock { + $source_folder = "PATH_WHERE_TO_CREATE_FOLDERS" + $nb_folder = 10 + # Base Name : (number will be added after with a space) + $folder_to_create = "My Folder Base NAME" + + 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" + } + else { + Write-Host "The path $path_to_verify already exist." -ForegroundColor "DarkBlue" + } + } + + + for ($i = 1 ; $i -le $nb_folder ; $i++) { + + $folder_name = $folder_to_create + " $i" + + # Exceptions + if ( $folder_name -eq "My Folder Base NAME 9" ) { + $folder_name = "My Folder Base NAME Part.1" + } + elseif ( $folder_name -eq "My Folder Base NAME 10" ) { + $folder_name = "My Folder Base NAME Part.2" + } + + # Construction of the final folder name + $final_folder_name = $source_folder + "\" + $folder_name + Test-Directory $final_folder_name + + + } + + +} diff --git a/Scripts-Divers/Script Suppression de tous les Dossiers vides dans un dossier donné.ps1 b/Scripts-Divers/Script Suppression de tous les Dossiers vides dans un dossier donné.ps1 new file mode 100644 index 0000000..75bd156 --- /dev/null +++ b/Scripts-Divers/Script Suppression de tous les Dossiers vides dans un dossier donné.ps1 @@ -0,0 +1,14 @@ +# Script pour tester si le dossier mis en paramètre contient des dossiers vides +# et si oui, les supprimer. + +# Source : https://stackoverflow.com/questions/28631419/how-to-recursively-remove-all-empty-folders-in-powershell + +Clear-Host +Invoke-Command -ScriptBlock { + $folder_to_check = "PATH_TO_FOLDER_CONTAINING_EMPTY_FOLDERS" + + $dirs = Get-ChildItem $folder_to_check -Directory -Recurse | Where-Object { (Get-ChildItem $_.fullName).count -eq 0 } | Select-Object -ExpandProperty FullName + $dirs | Foreach-Object { Write-Host "Dossier qui sera supprimer car considéré comme vide : " $_ } + # $dirs | Foreach-Object { Remove-Item $_ } + +}