plex_fr_discord_repo/Scripts-Divers/Batch Merge one MKV Audio with another video MKV.ps1

171 lines
9.0 KiB
PowerShell
Raw 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, et seulement la vidéo ##
## du second ##
## ##
##==============================================================================================
#Set MKVMerge.exe Path
$MKVMerge = 'H:\z_MKV\mkvtoolnix\mkvmerge.exe'
#Set Source and Target directories (Don't put an \ at the end)
$sourceDirectory_1 = "D:\DOSSIER_SOURCE-1"
$sourceDirectory_2 = "D:\DOSSIER_SOURCE-2"
$destinationDirectory = "F:\TEMP\"
#If source and destination are the same folder, set this to True, Otherwise let it to "False"
$merged_SUFFIX_name = "False"
#### Objective : Keep audio and subtitles and tags from FILE n°1 - Add video from FILE n°2
#### FILE 1 - Keeping all but the video
# ================== AUDIO ==================
# Track 1 = Audio n°1
# Name and language of Audio Track n°1
$AudioTrackName_1 = "1:Français - AC3 5.1 640kb/s"
$AudioLang_1 = "1:fr"
$AudioTrack_1_default = "1:yes"
# ================== SUBTITLES ==================
# Track 3 = Sub n°1 to keep
# Name and language of Subtitle Track n°1 + Sync Value
$SubTrackName_1 = "2:Français Forcés - SRT"
$SubTrackLang_1 = "2:fr"
$SubTrack_1_default = "2:yes"
####
# ================== OTHER SETTINGS ==================
# Define track order
$track_order = "1:0,0:1,0:2"
# Define subtitles track to keep
$subtitle_tracks = "2"
# Files name, without .mkv :
# 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
$filename_1_part_1 = "Ma série (20xx) - "
$filename_1_part_2 = " - 1080p-h264"
# Final name 1 = Ma série (20xx) - S01E01 - 1080p-h264
$filename_2_part_1 = "Ma série (20xx) - "
$filename_2_part_2 = " - 1080p.x265"
# Final name 2 = Ma série (20xx) - S01E01 - 1080p.x265
$filename_season = "02"
$filename_ep_total = "10"
##==============================================================================================
##==============================================================================================
# 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
}
#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."
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_total)) {
Write-Host "The number of epidoes set in the script isn't the same as the number of files in the folders... EXITING NOW !" -foreground "red"
Exit
}
for ($i = 1; $i -lt $Count_1+1; $i++) {
# 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
# Final name 1 = Le Bureau des Légendes (2015) - S01E01 - 1080p-h264
# Final name 2 = Le Bureau des Légendes (2015) - S01E01 - 1080p.BDRip.x265.Aac.NoTag
if ($i -lt 10 ) { # i<10
$MKV_1_name = $filename_1_part_1 + "S" + $filename_season + "E0" + $i + $filename_1_part_2
$MKV_2_name = $filename_2_part_1 + "S" + $filename_season + "E0" + $i + $filename_2_part_2
} else { # i=10
$MKV_1_name = $filename_1_part_1 + "S" + $filename_season + "E" + $i + $filename_1_part_2
$MKV_2_name = $filename_2_part_1 + "S" + $filename_season + "E" + $i + $filename_2_part_2
}
$MKV_1 = $sourceDirectory_1 + "\" + $MKV_1_name + ".mkv"
$MKV_2 = $sourceDirectory_2 + "\" + $MKV_2_name + ".mkv"
# Title for the video track
$VideoTrackName = $MKV_2_name
#Set Output File Name
#$Output = $Name + '___MERGED' + '.mkv'
If ($merged_SUFFIX_name -eq "True") {
$Output = $destinationDirectory + "\" + $VideoTrackName + '___MERGED' + '.mkv'
} elseif ($merged_SUFFIX_name -eq "False") {
$Output = $destinationDirectory + "\" + $VideoTrackName + '.mkv'
} else {
write-host "Error in the value of the merged_SUFFIX_name variable. Current value = $merged_SUFFIX_name" -foreground "red"
write-host "Should be set to True or False.`nScript is exiting now..." -foreground "white"
Exit
}
#Execute
# Keep Audio/Subtitles/tags from MKV_1 + Only video from MKV_2
# & $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 "2:UTF-8" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default"
# "$MKV_1"
# --language "0:fr" --track-name "0:$VideoTrackName" --default-track "0:yes"
# "$MKV_2"
& $MKVMerge --title "$VideoTrackName" --track-order "$track_order" --subtitle-tracks "$subtitle_tracks" -o "$Output" --no-video --no-chapters --no-global-tags --language "$AudioLang_1" --track-name "$AudioTrackName_1" --default-track "$AudioTrack_1_default" --sub-charset "2:UTF-8" --language "$SubTrackLang_1" --track-name "$SubTrackName_1" --default-track "$SubTrack_1_default" "$MKV_1" --language "0:fr" --track-name "0:$VideoTrackName" --default-track "0:yes" --no-subtitles --no-audio "$MKV_2"
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.
Move-Item -Path $MKV_2 -Destination $Path_Folder_NotMerged_2 -Verbose
#Move-Item -Path $Sub -Destination $Path_Folder_NotMerged_2 -Verbose
# Renaming the output file is not necessary if the output filename doesn't have __MERGED in it
#Rename-Item -Path $Output -NewName $MKV -Verbose
} Else {
write-host "File NON-EXISTANT - $Output" -foreground "red"
"File NON-EXISTANT - $Output" | Out-File "$destinationDirectory\Errors.txt" -Append
}
}