Page 1 of 2

Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 08:12
by Highlander

Code: Select all

 INFO.BAT version 1.5
--------------------------------------------------------------------------------
Windows version        :  Microsoft Windows [Version 10.0.19042.685]
Product name           :  Windows 10 Pro, 64 bit
Performance indicators :  Processor Cores: 4      Visible RAM: 8268000 kilobytes

Date/Time format       :  (dd/mm/yy)  12/12/2020   0:46:11.23
__APPDIR__             :  C:\WINDOWS\system32\
ComSpec                :  C:\WINDOWS\system32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Disabled
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  en-GB       Code Pages: OEM  437    ANSI 1252
DIR  format            :  10/12/2020  06:52 PM     1,543,503,872 pagefile.sys
Permissions            :  Elevated Admin=No, Admin group=Yes

                          Missing from the tool collection:  debug
Hi,

I have several folders full of MKV video files (mainly TV programmes) and I would like to add a "Title" into each of them. I can use the program mkvmerge.exe to do this. The correct syntax would be:-

Code: Select all

mkvmerge --title < text string> -o "<path/filename>"
where you replace<text string> with whatever (text) you want the title of the video file to be (gets displayed for 5 sec at the start in VLC inter alia; but not all players).

This is fine for an individual video file but, naturally, I would like to process all the files in each folder in one go; hence the need for a batch file to do this.

I started by using this snippet:-

Code: Select all

for %%f in (*.mkv) do %mkvmerge% --title %%~nI -o "Output_Folder_Path/%%f" "%%f"
(I didn't write this myself. I'm not a programmer, I just dabble and I don't fully undersatnd the syntax and arguments that are used in Windows Batch Scripting; or any other programming language for that matter, lol)

I used "%~nI" because I read somewhere that that would "fetch" the filename and strip the extension off it which was just what I wanted to do, ie: add the filename of each video (without the .mkv extension) as its title (embedded in its Matroska container). This didn't work at all, at first, but when I added the second "%" (%~nI replaced by %%~nI) the loop worked ran perfectly. However, although it processed all the videos successfully, it just set (the text) "%~nI" as the title of each and every video instead of their filenames! lol.

So next I tried replacing %%~nI with %%~nf, thus:-

Code: Select all

for %%f in (*.mkv) do %mkvmerge% --title %%~nf -o "Output_Folder_Path/%%f" "%%f"
Now this certainly fetched the filename (being processed) but it also corrupted the loop counter (apparently, ie: as far as I could ascertain) so the routine failed to write any modified files!

Since (I thought) using the loop counter variable (?) as the argument for the --title option seemed to be corrupting it, I tried to introduce a new variable that might overcome this and produced this:-

Code: Select all

for %%f in (*.mkv) do SET str=%~nf & %mkvmerge% --title %%str "Output_Folder_Path/%%f" "%%f"
and when that failed miserably too, I adjusted it to this:-

Code: Select all

for %%f in (*.mkv) do SET str=%f & SET str=%~nstr %mkvmerge% --title %%str "Output_Folder_Path/%%f" "%%f"
but, as you can probably see, I'm just tying myself up in knots with all these strings, lol. So I decided to seek the help of folks who know what they're doing, ie: you, good readers.

Can any kind soul out there offer me a simple adjustment to the original piece of code that will successfully do what I want?

Ideally, if you could just tell me what to replace <your_suggestion> with in the code below, that would be great but if that's not possible I'd be happy to get any other suggestions you'd care to offer.

Code: Select all

for %%f in (*.mkv) do %mkvmerge% --title <your_suggestion> -o "Output_Folder_Path/%%f" "%%f"
(Of course, <your_suggestion> needs to be something that will get the filename of the video currently being processed and strip the .mkv extension off it.)

I hope I've explained all this properly but, if not, or there's any other info you need, please don't hesitate to get in touch.

Thanks,
The Highlander.
[/size]

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 08:37
by aGerman
Forget about the additional variable. It'll cause more problems than being beneficial. I can imagine that writing back to the same file (if so, because I don't know whether Output_Folder_Path is the current folder) will just confuse the CMD and it'll find the files more than once in the FOR loop because they have been updated meanwhile.
One opportunity that I can think of to work around this is to cache the file names beforehand. A FOR /F loop usually performs such an internal capturing.

Code: Select all

for /f "delims=" %%f in ('dir /a-d /b *.mkv') do %mkvmerge% --title %%~nf -o "Output_Folder_Path/%%f" "%%f"
Another possibility is that the file name contains characters like spaces or ampersands which would certainly require to enclose the title into quotes.

Code: Select all

for /f "delims=" %%f in ('dir /a-d /b *.mkv') do %mkvmerge% --title "%%~nf" -o "Output_Folder_Path/%%f" "%%f"
Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 08:58
by Highlander
Yes, the output path would be the current folder. The idea is to run the batch file in the folder where the videoes are stored (and, hopefully, recurse the operation in any/all sub-folders that are in that dir3ectory too)

How would that affect your code suggestion (if at all)?

Also, the filenames are very likely to contain spaces and/or other (non alphanumeric characters, like &) so using quotes sorts that does it?

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 09:10
by aGerman
First of all create a new folder and try to output the files to this folder. It's likely that reading and writing a file at the same time might corrupt it.
The reason for the quotes is that spaces are separators for arguments in a command line. Ampersands concatenate commands and thus, everything following an ampersand will be treated as a new command. I'm sure that the recent command won't process any files in subfolders. Apparently I don't know the tool you're using here. So, please try my suggestion first with having some mkv file in the current directory.

Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 09:36
by Highlander
Thanks, I'll certainly try what you suggest.

The recursion into sub-folders isn't that important. If I can get it to work on just one folder then I'll be more than happy. :D

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 09:44
by aGerman
The recursion into sub-folders isn't that important.
I guess this shouldn't get too complicated. I'll support on that once it works for files in the current directory.

Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 10:27
by Highlander
Hi Steffen,

Thanks for your suggestions.

Here is the original code (written by someone else, of course) that I am trying to modify to do what I want.

Code: Select all

@echo off
set mkvmerge="C:\Program Files\MKVToolNix\mkvmerge.exe"
if not exist "options.json" (
    echo Options file 'options.json' not found. Exiting.
    pause>nul
    exit
)
if not exist "mkvmerge_out" (mkdir "mkvmerge_out")
for %%f in (*.mkv) do %mkvmerge% @options.json -o "mkvmerge_out/%%f" "%%f"
echo.
echo ============================
echo Done. Press any key to exit.
pause>nul
exit
I tried to insert your code into it to replace (what I thought were) the appropriate bits and it now looks like this:

Code: Select all

@echo off
set mkvmerge="C:\Program Files\MKVToolNix\mkvmerge.exe"
if not exist "options.json" (
    echo Options file 'options.json' not found. Exiting.
    pause>nul
    exit
)
if not exist "mkvmerge_out" (mkdir "mkvmerge_out")
for /f "delims=" %%f in ('dir /a-d /b *.mkv') do %mkvmerge% --title "%%~nf" @options.json -o "mkvmerge_out/%%f" "%%f"
echo.
echo ============================
echo Done. Press any key to exit.
pause>nul
exit
Does that look right?

I tried it on a folder with a couple of MKVs in it and it certainly seemed to do the job. Yayyy!

Here is the "output" that was produced:

Code: Select all

mkvmerge v51.0.0 ('I Wish') 64-bit
'03. The Unquiet Dead.mkv': Using the demultiplexer for the format 'Matroska'.
'03. The Unquiet Dead.mkv' track 0: Using the output module for the format 'AVC/H.264'.
'03. The Unquiet Dead.mkv' track 1: Using the output module for the format 'AAC'.
'03. The Unquiet Dead.mkv' track 2: Using the output module for the format 'text subtitles'.
Using the track with the ID 0 from the file '03. The Unquiet Dead.mkv' as the reference for chapter generation.
The file 'mkvmerge_out/03. The Unquiet Dead.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Multiplexing took 0 seconds.
mkvmerge v51.0.0 ('I Wish') 64-bit
'10. The Doctor Dances.mkv': Using the demultiplexer for the format 'Matroska'.
'10. The Doctor Dances.mkv' track 0: Using the output module for the format 'AVC/H.264'.
'10. The Doctor Dances.mkv' track 1: Using the output module for the format 'AAC'.
'10. The Doctor Dances.mkv' track 2: Using the output module for the format 'text subtitles'.
Using the track with the ID 0 from the file '10. The Doctor Dances.mkv' as the reference for chapter generation.
The file 'mkvmerge_out/10. The Doctor Dances.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Multiplexing took 0 seconds.

============================
Done. Press any key to exit.
Which all seems OK to me (BWTHDIK, lol)

I removed "@echo" from the start and that produced this "output":

Code: Select all

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>set mkvmerge="C:\Program Files\MKVToolNix\mkvmerge.exe"

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>if not exist "options.json" (
echo Options file 'options.json' not found. Exiting.
 pause1>nul
 exit
)

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>if not exist "mkvmerge_out" (mkdir "mkvmerge_out" )

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>for /F "delims=" %f in ('dir /a-d /b *.mkv') do "C:\Program Files\MKVToolNix\mkvmerge.exe" --title "%~nf" @options.json -o "mkvmerge_out/%f" "%f"

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>"C:\Program Files\MKVToolNix\mkvmerge.exe" --title "03. The Unquiet Dead" @options.json -o "mkvmerge_out/03. The Unquiet Dead.mkv" "03. The Unquiet Dead.mkv"
mkvmerge v51.0.0 ('I Wish') 64-bit
'03. The Unquiet Dead.mkv': Using the demultiplexer for the format 'Matroska'.
'03. The Unquiet Dead.mkv' track 0: Using the output module for the format 'AVC/H.264'.
'03. The Unquiet Dead.mkv' track 1: Using the output module for the format 'AAC'.
'03. The Unquiet Dead.mkv' track 2: Using the output module for the format 'text subtitles'.
Using the track with the ID 0 from the file '03. The Unquiet Dead.mkv' as the reference for chapter generation.
The file 'mkvmerge_out/03. The Unquiet Dead.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Multiplexing took 0 seconds.

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>"C:\Program Files\MKVToolNix\mkvmerge.exe" --title "10. The Doctor Dances" @options.json -o "mkvmerge_out/10. The Doctor Dances.mkv" "10. The Doctor Dances.mkv"
mkvmerge v51.0.0 ('I Wish') 64-bit
'10. The Doctor Dances.mkv': Using the demultiplexer for the format 'Matroska'.
'10. The Doctor Dances.mkv' track 0: Using the output module for the format 'AVC/H.264'.
'10. The Doctor Dances.mkv' track 1: Using the output module for the format 'AAC'.
'10. The Doctor Dances.mkv' track 2: Using the output module for the format 'text subtitles'.
Using the track with the ID 0 from the file '10. The Doctor Dances.mkv' as the reference for chapter generation.
The file 'mkvmerge_out/10. The Doctor Dances.mkv' has been opened for writing.
Progress: 100%
The cue entries (the index) are being written...
Multiplexing took 0 seconds.

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>echo.


Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>echo ============================
============================

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>echo Done. Press any key to exit.
Done. Press any key to exit.

Z:\# 2 fIX\SoftWare Updates\MKV Tools\Batch Ops\mkvtoolnix-batch (Changing things in muxed mkv)\2 Episode Title>pause1>nul
Do you think it's "safe" fro me to run it on my 'real' video files????

Thanks,
Brian.[/size]

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 10:31
by Highlander
PS: Please just forget the recursion.

On reflection, it's better (safer?) if I just do this one folder at a time, lol.

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 10:45
by aGerman
Haha, come on. Now that we are at that point let's at least try to get it sorted for the recursive processing.

Code: Select all

...
set mkvmerge="C:\Program Files\MKVToolNix\mkvmerge.exe"
...
chcp 1252
for /f "delims=" %%f in ('dir /a-d /b /s *.mkv') do (
  %mkvmerge% --title "%%~nf" -o "%%~f.~tmp" "%%~f"
  move /y "%%~f.~tmp" "%%~f"
)
Try it with a limited number of backed-up files first to be on the safe side. You're right, scripts don't have an undo button. This code creates a temporary file first (in order to avoid clashes). So it's not necessary to write into a different folder.

Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 10:59
by Highlander
I'm not sure how to change the batch now. Are you suggesting I remove the bit that creates a new destination folder ("mkvmerge_out") altogether. Does this now delete the originals?

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 11:08
by aGerman
The move /y "%%~f.~tmp" "%%~f" line will overwrite the original file with the temporary file that mkvmerge created in the previous line.
So, all in all the script code should look like that (untested though):

Code: Select all

@echo off
set mkvmerge="C:\Program Files\MKVToolNix\mkvmerge.exe"
if not exist "options.json" (
    echo Options file 'options.json' not found. Exiting.
    pause>nul
    exit /b
)
chcp 1252
for /f "delims=" %%f in ('dir /a-d /b /s *.mkv') do (
  %mkvmerge% --title "%%~nf" @options.json -o "%%~f.~tmp" "%%~f"
  move /y "%%~f.~tmp" "%%~f"
)
echo.
echo ============================
echo Done. Press any key to exit.
pause>nul
You should know better than me if you even need the options.json file.

Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 11:44
by Highlander
Yes the options.json file is important. It does other things besides populating the title field (though it could).
The original code was written by someone to alter various things in the MKV file but it does the same thing to every file it processes. (Things like setting the Language, Default, Forced, etc. for each stream in the container but if I used the. json file to set the title it would put the same text into every file's title field, hence my original problem, lol)

I will try what you've written & let you know how it goes.

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 12:05
by Highlander
That seems to work a treat! lol

I have no idea how, lol.

The "chcp 1252", the "delims= and the "('dir /a-d /b /s *.mkv')" all leave me utterly confused, lol.
I guess I'll just have to go off and learn how all these script commands, syntax & arguments all work

However, your code seems to do exactly what I'm looking for and it saves me having to replace all the original files with the modified ones in the newly created destination folder.

So thank you very much for your excellent code.

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 12:36
by aGerman
chcp 1252 - As the output of info.bat told me, the default ANSI codepage on you machine is 1252. This is to synchronize the charset of the batch process with the ANSI charset that has the best overlap with the characters used in file names in your file system.

"delims=" - Take the output of DIR as is, don't tokenize it. (Run FOR /? in a cmd window an read the help message.)

dir /a-d /b /s *.mkv - List all files with extension .mkv, exclude folder names, but search recursively. (Run DIR /? in a cmd window an read the help message.)

Steffen

Re: Add Filename (minus the .mkv) as a Title to a folder of Matroska videos

Posted: 12 Dec 2020 12:37
by Highlander
Just Googled some of these things and I'm beginning to understand how they work.

Thanks again,