Batch renaming mkv files with original video title name isntead of filename

Hi to ya’ all.

I have about 1500 mkv files, mostly >15GB. Recently my drives crashed. I have been able to recover most of the mkv’s. The problem I’m having is that the recovery program I used, DiskDrill, didn’t safe the mkv filenames, but instead replaced them with serial numbers (i.e. file000001, file000002, file000003 etc). Most of the mkv’s still have their original file names in the video track file title properties

I’m trying to write a batch script bat file that can a) extract the original video title name, and then b) rename the mkv file with that extracted title.

I’m not very good at scripting, but usual grasp some of the basics (of the very basics LOL).

This is what I’ve tried:

@echo off
REM — Set the path to mkvpropedit.exe —
SET MKVPROPEDIT_PATH=“C:\Program Files\MKVToolNix\mkvpropedit.exe”
REM — Loop through all MKV files in the current directory —
for %%m in (*.mkv) do (
echo Changing %%m Title to “%%~nm”
REM point to location of mkvpropedit.exe in following line
“C:\Program Files\MKVToolNix\mkvpropedit.exe” “%%m” -e info -s title=“%%~nm”
echo -----
)
echo ***** Finished *****
pause

Running this, all I get is this:

Changing file000001.mkv Title to “file000001”
The file is being analyzed.
The changes are written to the file.
Done.

Changing file000002.mkv Title to " file000002"
The file is being analyzed.
The changes are written to the file.
Done.

Changing file000003.mkv Title to " file000003"
The file is being analyzed.
The changes are written to the file.
Done.

All help would be very much appreciated indeed :slightly_smiling_face: :waving_hand:

First thing you need is Karolj Kočmaroš Subtitle and Video File Renamer https://karoljkocmaros.blogspot.com/p/subtitle-video-renamer.html

Second thing is to get the episode list from epguides, for example https://epguides.com/Art21/
Click List as .csv Select the lines for the episodes you want to rename. Copy to clipboard.

number,season,episode,airdate,title,tvmaze link <-don’t select line

1,1,1,21 Sep 01,“Place”," Place - Art21 - Art in the 21st Century 1x01 | TVmaze "
2,1,2,28 Sep 01,“Identity”," Identity - Art21 - Art in the 21st Century 1x02 | TVmaze "
3,1,3,05 Oct 01,“Spirituality”," Spirituality - Art21 - Art in the 21st Century 1x03 | TVmaze "
4,1,4,12 Oct 01,“Consumption”," Consumption - Art21 - Art in the 21st Century 1x04 | TVmaze "
5,2,1,09 Sep 03,“Stories”," Stories - Art21 - Art in the 21st Century 2x01 | TVmaze "

(five lines as an example, there are 44 episodes)

Create a new text file then paste into it and save with the extension changed to .csv If your text editor doesn’t allow changing that, rename it after saving.

Open the CSV file in Excel or another spreadsheet program. In Excel, the " marks are ignored when opening a CSV.

You’ll have columns A B C D E F
Delete column F and clear the contents of A B C D

In column C enter ART21 in row 1. With Excel you can automatically fill the column by dragging the lower right corner of the cell down the column. Since this show name ends with a number, Excel will auto-increment it. The way to stop that is add a letter to the end, so for example make it ART21P. (P for Placeholder.)

In column F, enter .srt in row 1. Drag down to fill all of the column with .srt (any subtitle file name extension Subtitle and Video File Renamer recognizes will do here)

In column B, fill with echo

Right click column D, Format Cells, select Text. Otherwise Excel will interpret this as formulas with an exponent.
In row 1 enter S01E01 or however you prefer your season and episode numbering, as long as it ends with a number. (I never use the S.)
Drag down to the last episode of Season 1, then in the next cell down, enter S02E01
Repeat until all your seasons and episodes are numbered.

Delete column A, which should be empty.

Save the CSV file. Note that if you re-open the CSV, Excel will interpret the series and episode number column as formulas since the plain text CSV format has no way to denote the data types.

Change the .csv file name extension to .txt and open it.

The contents will be like this

echo,ART21P,S01E01,Place,.srt
echo,ART21P,S01E02,Identity,.srt
echo,ART21P,S01E03,Spirituality,.srt
echo,ART21P,S01E04,Consumption,.srt
echo,ART21P,S01E05,Stories,.srt

Do find and replace all to change ,.srt to .srt"

Find and replace all echo, with echo “” >"

Extra step due to having to change the show name, replace all ART21P with ART21

Lastly, replace all , with a space.

The lines should now be like this

echo “” >“ART21 S01E01 Place.srt”
echo “” >“ART21 S01E02 Identity.srt”
echo “” >“ART21 S01E03 Spirituality.srt”
echo “” >“ART21 S01E04 Consumption.srt”
echo “” >“ART21 S01E05 Stories.srt”

These characters are not allowed in Windows file names
< > : " / \ | ? *
If an episode title contains any of them (most often it will be a : or ?) you’ll need to remove or change it.

Change the .txt file name extension to .cmd or .bat then doubleclick it. It generates an empty subtitle file for every episode. It’s best to run this in an empty folder.

Use them in the subtitle side of the renamer to rename all of your video files. Then you can delete the empty subtitle files.

When you have a large number of video files to rename, this is much quicker than renaming the files one by one, typing or copying and pasting the names and other information.

If Epguides doesn’t have your list of titles then you’ll have to find another source to build the list. If you’re using Windows, install the mega version of K-Lite Codec Pack. Then in Details view in Explorer, right click the header then click More. Scroll down to find the Filename box and check it. Hopefully that reads the file name embedded in the metadata. If not there may be another option in that list.

Hi Galane;

Thanks for your detailed reply / advice. Unfortunately, this doesn’t apply to my question / issue. To clarify:

I have hundreds of mkv files that I have recovered from a failed hard disk. The recovery program I used did not recover the file names, but replaced the file names of the recovered mkv’s with serials (i.e. file000003.mkv …). Most of the recovered mkv’s play ok, but without relevant filenames I’ve got no way to identify the movies.

I’m looking for a script that can a) extract the original video title name, and then b) rename the mkv file with that extracted title.

Thanks for all advice. Cheers

Here’s a Python script that iterates over all *.mkv in the current directory, uses mkvmerge to identify its properties including its title (if set) & if there is a title set, renames the file by their title & places the renamed file in a sub-directory called renamed.

#!/usr/bin/env python3

import glob
import json
import os
import shlex
import subprocess

# create target directory if it doesn't exist
os.makedirs("renamed", exist_ok=True)

# loop over all *.mkv files in current directory

for mkv in glob.glob("*.mkv"):
    # run "mkvmerge -J thefile.mkv" for identification, this yields
    # the file title among other properties
    # command = "mkvmerge -J {}".format(shlex.quote(mkv))
    result = subprocess.run([ "mkvmerge", "-J", mkv ], capture_output=True, text=True)

    # print error message if identification failed
    if result.returncode != 0:
        print("ERROR: {}: could not identify file".format(mkv))
        continue

    # parse the JSON produced by the identification into a Python
    # structure
    properties = json.loads(result.stdout.strip())

    # print warning message if mkvmerge could not handle the file
    if not properties.get("container", {}).get("supported", False):
        print("WARNING: {}: unknown/unsupported/defective file, skipping".format(mkv))
        continue

    # see if there's a file title set
    title = properties.get("container", {}).get("properties", {}).get("title", None)

    if title is None or title == "":
        print("WARNING: {}: No title set, skipping".format(mkv))
        continue

    new_name = "renamed/{}.mkv".format(title)
    print("{}: renaming to: {}".format(mkv, new_name))

    os.rename(mkv, new_name)

Thanks for that. This should do the job nicey. Three Big Cheers :waving_hand: