1

Ok, I'm total new at this... Basically I'm using a tool call mkvmerge to attach multiple font files(.ttf) to .mkv files. I have separated the .mkv files into folders together with the respective fonts I would like to attach.

My aim is to create a batch that creates a copy of all the .mkv files with all the added attachments and deposits them in a newly created a folder (i.e Revised) in the parent directory.

Beginning with just a single folder:

mkdir Revised for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%~.ttf" 

This works if I change the "%%~.ttf" to an actual .tff file name (i.e

mkdir Revised for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "sans serif.ttf" 

and I would end up with newly created Revised folder which contains a .mkv file with the sans serif.tff file attach within the .mkv file itself. However I would like to add multiple .ttf files without naming them individually. (searching online it seems I need something like "$file" though I dont know how to use it)

Next if I have a parent folder with multiple sub-folders:

mkdir Revised for /R %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%~.ttf" 

This just flat out doesn't work. Not just because of the "%%~.ttf" issue I'm sure.

I know that it might be a bit too ambitious, so if some one could just help solve the first half of my problem, that would be lovely. Thanks a lot in advance.

Ps: If anyone need to understand the mkvmerge specific commands to help out: https://mkvtoolnix.download/doc/mkvmerge.html

Updates: For the first part

mkdir Revised for %%x in (*.ttf) do ( for %%A in (*.mkv) do "%mkvmerge%" -q -o "Revised\%%A" "%%A" --attachment-mime-type application/x-truetype-font --attach-file "%%x" ) 

It seems to work better but I think the script would now add and remove the the .ttf files until the last .ttf file in the folder remained.

2
  • What is "**%%~.ttf**" ? :) Also It would be easier if you show some input examples as well as expected output, which would make it easier for us to assist. Commented Feb 15, 2019 at 6:04
  • @GerhardBarnard Sorry I wasn't very clear. There are multiple .ttf files and 1 single .mkv file within any given folder. There is no relationship between their naming. I have edited the post to hopefully provide some more clarity. Please do tell me if a better explaination is needed Commented Feb 15, 2019 at 6:27

4 Answers 4

3

Please give this a try. (Remember to set your %mkvmerge% variable to your executable path):

@echo off set "mkvmerge=C:\Some Path\mkvmerge.exe" for %%a in (*.ttf) do ( for /f %%i in ('dir /s /b /a-d *.mkv ^| findstr /vi Revised') do ( if not exist "%%~dpiRevised" mkdir "%%~dpi\Revised" if not exist "%%~dpiRevised\%%~nxi" copy "%%~fi" "%%~dpiRevised" "%mkvmerge%" -q -o "%%~dpiRevised\%%~ni_rev%%~xi" "%%~dpiRevised\%%~nxi" --attachment-mime-type application/x-truetype-font --attach-file "%%~dpi%%a" ) ) 

So to explain what went wrong with your examples:

In the for loop, you take apply from the mkv inside the root folder, and apply a ttf file to it and create the new mkv file with the attached ttf to the Revised directory, then for the next ttf you again copy from the root directory, overwriting the mkv file in the Revised directory with a new one where a new ttf was applied etc.

Instead, we need to first make a copy of the mkv file into the Revised directory then we apply the first ttf file to itself in Revised and then take the mkv with the already attached ttf and apply another ttf to it until all ttf files have been applied to the new mkv inside of Revised The original mkv and all the ttf files will remain in the parent folder.

Note if any of what I explained does not make sense, let me know and I will rephrase.

Sign up to request clarification or add additional context in comments.

Comments

1

I have decided to take a stab at this, it is intended to be run from the parent directory holding your directories, (I have assumed that those directories are all on the same level, this is not recursing through nested directories).

Please be aware that I am unable to test this.

@Echo Off Set "mkvm=%UserProfile%\Downloads\Video Players, Editors, & Downloaders\MKVTool Batch Modifier\mkvmerge.exe" For /F Delims^=^ EOL^= %%A In ('Dir/B/AD 2^>Nul^|FindStr/IVXC:"Revised"' ) Do If Exist "%%A\*.mkv" (If Exist "%%A\*.ttf" ( If Not Exist "Revised\" MD "Revised" 2>Nul||Exit /B Call :S1 "%%A")) GoTo :EOF :S1 PushD %1 2>Nul||Exit /B Set "as=--attachment-mime-type application/x-truetype-font --attach-file" Set "ttfs=" For /F Delims^=^ EOL^= %%A In ('Where .:*.ttf' ) Do Call Set "ttfs=%%ttfs%% %as% "%%~nxA"" For /F Delims^=^ EOL^= %%A In ('Where .:*.mkv' ) Do "%mkvm%" -q -o "%~dp0Revised\%%~nxA" "%%~nxA" %ttfs% PopD 

I think I have also made this in such a way as to allow for more than one .mkv file in a directory, where each will be attached to all of the same .ttf files.

3 Comments

It work like a charm!!! Thank you so much. Do you mind explaining your though process and how you went about doing it?
Without wishing to sound rude, I'd perefer not to. I'd prefer that you step through the file yourself, reading each command and learning them as you go. You can open the Command Prompt and enter each command followed by the help option to learn how each works, e.g. PushD /?, Set /? …etc.
No offence taken. I appreciate the time you took to write it in the first place. Thanks a dozen.
0
Get-ChildItem *.mkv | ForEach-Object { $FontFlags = "" Get-ChildItem -LiteralPath $_.BaseName -Exclude *.xml | ForEach-Object { $FontFlags += " --add-attachment `"$($_.FullName)`"" } Write-Host "Running &`"C:\Program Files\MKVToolNix\mkvpropedit.exe`" `"$_`" $FontFlags" Invoke-Expression "&`"C:\Program Files\MKVToolNix\mkvpropedit.exe`" `"$_`" $FontFlags" } Read-Host "Press any key to exit..." 

Make a folder with the same names as mkv without extension. Put mkv file, folder , this code in same folder and run it.

I didn't make this code. I asked someone to make this for my personal use.

Comments

0

When I ran into the problem of adding fonts to the MKV container, the scripts proposed above did not give the necessary result. Therefore, I wrote my PowerShell script, understandable and reliable.

$mkvpropedit = "D:\Program\MKV_toolnix\mkvpropedit.exe" $ScriptPath = $MyInvocation.MyCommand.Path | split-path -parent Write-Host "Fonts found:`n" -ForegroundColor Cyan Get-ChildItem -LiteralPath $ScriptPath *.ttf | ForEach-Object { $Attaches = $Attaches + ' --attachment-mime-type application/x-truetype-font --add-attachment "' + $_.FullName + '"' Write-Host $_.Name } Write-Host "`nWe sew them in the following MKV containers:" -ForegroundColor Cyan Get-ChildItem -LiteralPath $ScriptPath *.mkv | ForEach-Object { Write-Host "`n-"$_.Name -ForegroundColor Yellow Invoke-Expression "&`"$mkvpropedit`" `"$($_.FullName)`"$Attaches" } Read-Host "`n`nThe script worked, you can close it..." 

How to start and what does:

  1. Save the script to a text file with the extension .ps1 (for example, AttachFonts.ps1).
  2. Be sure to edit the variable in the first line inside - the path to the mkvpropedit.exe file (contained in the MKV_toolnix package).
  3. We move the script in one folder with all .ttf fonts and all .mkv video. Each font will be integrated into each video.
  4. Click the right mouse button on the AttachFonts.ps1 script in the folder and select execute using PowerShell.

All is ready. An important point: if the font is already contained inside the file, it will be duplicated again with the same name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.