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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp


Posts: 6
Joined: 21 Oct 2020 08:54

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

#19 Post by isidroco » 22 Dec 2020 15:26

Some time ago, I made a batch VID2MKV.BAT for merging all Video files with corresponding Audio files on a folder and generating MKVs (small side part of VirtualDub batch deshake project). Just added filename as title now. It can take any video extension for video input, and any video/audio file extension for audio input. So in this case one should put MKV and MKV as input. Output will be ZZfilename.MKV, it won't overwrite previous ZZfiles if it was used previously, and it won't process any files starting with ZZ. In that way it can save all work on same folder, afterwards one can rename the output files. So for this specific case, usage would be: VID2MKV imkv amkv
Which will take video and audio from each MKV file and generate ZZxxxxx.MKV with title on it.

Code: Select all

@echo off
set audioAltPrefix=AUDIO\
set mkvloc=C:\DVD\MKVToolNix\&REM ***mkvmerge path goes here***

set errorStatus=ok& REM (No error)-----
verify other 2>nul & REM sets errorlevel to 1 (check if enableextensions works)
setlocal enableextensions enabledelayedexpansion & REM read vars at exec with !varNm!
if errorlevel==1 ( set errorStatus= Newer cmd.exe is needed & goto :error )

title VID2MKV v3.1 by isidrococo
echo/& echo/VID2MKV.BAT v3.1 2020/12/22 by isidrococo at gmail& echo/

GOTO :skiphelp

echo/Multiplex all Videos and Audios from current dir, outputs to: ZZfilename.MKV
echo/  Doesn't redo already done files, doesn't process any ZZ*.* file
echo/Usage: VID2MKV [InputExtension] AudioExtension [Tasks] [Zprefs]
echo/  InputExtension (default .AVI): iAVI^|iAVC^|iFLV^|iH264^|iMKV^|iMOV^|iMP4
echo/  AudioExtension (MUST be specified): aAVI^|aFLAC^|aFLV^|aMKV^|aMOV^|aMP3^|aMP4^|aWAV
echo/  Tasks: [tNoTit]
echo/    tNoTit: don't add title to each mkv
echo/  Zprefs: [zNoPau][zOff][zNoDel][zNoErr]
echo/    zNoPau: No Pause; zOff: Turn off pc; zNoDel: Keep incomplete MKVs on error
echo/    zNoErr: No Abort batch on vdub error
echo/ *** Audio files CAN optionally be placed in subfolder named: AUDIO\ (useful
echo/           to extract audio from files with same name but different content)
echo/ *** Needs mkvmerge.exe from MkvToolNix and DShutdown.exe for power off.
echo/  VID2MKV aflac (Joins all AVIs with FLACs of same name to MKV)
echo/  VID2MKV imov amov (generates direct copy MKVs from MOVs)
echo/  VID2MKV aAVI (direct copy AVIs to MKVs)

GOTO :eof

  isidrococo at gmail
    v3.1 201222 T_notit, minor cleanup
    v3.0 190108 changed syntax, bug fixes, added alternate audio path prefix, FLV
    v2.4 190108 aavi wasn't working
    v2.3 180902 no params: show help before searching for MKVtoolnix
    v2.21 added c:\DVD\MKVToolNix_x64
    v2.2 180406 corrected bug: REM after ( goes without &
    v2.1 180403 error handling; handle same extension for A/V
    v2.0 180326 added MOV, avi audio; changed params: a,c,h,v; w,f,m,k; n,o to vavi,vavc,vh264,vmkv; awav,aflac,amp3,amkv; znp,zof
    v1.3 160818 added a, c, h, v, k
    v1.2 better help
    v1.1 added [m] for MP3 mux

  *** Examples for MKVMERGE commands:
    "C:\DVD\MKVToolNix\mkvmerge.exe" -o "E:\\V2\\071209_2204_27.mkv"
    --title "071209_2204_27"
    --default-language "eng"
		"--forced-track" "0:no" "-d" "0" "-A" "-S" "-T" "--no-global-tags"
    "--no-chapters" "(" "E:\\V2\\071209_2204_27.avi" ")"
    "--forced-track" "0:no" "-a" "0" "-D" "-S" "-T"
    "--no-global-tags" "--no-chapters"
    "(" "E:\\V2\\071209_2204_27.flac" ")" "--track-order" "0:0,1:0"

    "(" "file.avi" ")"  is the same as: = file.avi
    -y, --sync TID:d[,o[/p]]  o/p: adjust the timestamps by o/p to fix linear drifts.
    	p defaults to 1 if omitted. Both o and p can be floating point numbers.
    	Defaults: no manual sync correction (which is the same as d = 0 and o/p = 1.0).
    --default-duration TID:x   The argument x must be postfixed with 's', 'ms', 'us',
    	'ns', 'fps', 'p' or 'i' to specify the default duration
    -M, --no-attachments
    --compression TID:n  Selects the compression method to be used for the track.
    	Note that the player also has to support this method. Valid values are 'none',
    	'zlib', 'lzo'/'lxo1x', 'bz2'/'bzlib' and 'mpeg4_p2'/'mpeg4p2'.

set PARAM=x%*& REM avoid empty: PARAM="x"+parameters
set PARAM=%PARAM: =%& REM delete spaces

set mkVidExt=AVI
IF not %PARAM:iavc=% == %PARAM% set mkVidExt=AVC
IF not %PARAM:iavi=% == %PARAM% set mkVidExt=AVI
IF not %PARAM:iflv=% == %PARAM% set mkVidExt=FLV
IF not %PARAM:ih264=% == %PARAM% set mkVidExt=H264
IF not %PARAM:imkv=% == %PARAM% set mkVidExt=MKV
IF not %PARAM:imov=% == %PARAM% set mkVidExt=MOV
IF not %PARAM:imp4=% == %PARAM% set mkVidExt=MP4

set mkAudioExt=help
IF not %PARAM:aavi=% == %PARAM% set mkAudioExt=AVI
IF not %PARAM:aflac=% == %PARAM% set mkAudioExt=flac
IF not %PARAM:aflv=% == %PARAM% set mkAudioExt=flv
IF not %PARAM:amkv=% == %PARAM% set mkAudioExt=MKV
IF not %PARAM:amov=% == %PARAM% set mkAudioExt=MOV
IF not %PARAM:amp3=% == %PARAM% set mkAudioExt=MP3
IF not %PARAM:amp4=% == %PARAM% set mkAudioExt=MP4
IF not %PARAM:awav=% == %PARAM% set mkAudioExt=WAV

if !mkAudioExt!==help GOTO :fin

  rem Find mkvmerge's path:
FOR %%S in ( "!mkvloc!" "%ProgramFiles(x86)%\MKVtoolnix\" "%ProgramFiles%\MKVtoolnix\"^
  ,"C:\DVD\MKVtoolnix\","C:\DVD\MKV\MKVtoolnix\" "C:\MKVtoolnix\" "c:\DVD\MKVToolNix_x64\" ) do (
  set mkvloc=%%~S& REM eliminate " (which are needed for spaces in dirs)
  IF exist !mkvloc!mkvmerge.exe GOTO :mkvfound
  set errorStatus= mkvmerge.exe not found: Modify mkvloc path in batch or install mkvtoolnix
  GOTO :error

echo !time:~,-3! - mkvmerge:!mkvloc!, vExt: !mkVidExt!, aExt:!mkAudioExt!
echo .
FOR %%I in (*.!mkVidExt!) do (
  set FileName=%%~nI
  set FilePath=%%~dpI
  set FileBeginChars=!FileName:~0,2!
  rem Don't process ZZ*.*, skips if already deshaked or missing audio
  IF not "!FileBeginChars!" == "ZZ" IF not exist !FilePath!ZZ!FileName!.MKV ( REM skips if exist MKV
  	set mkvTitle=--title "!FileName!"
  	IF not %PARAM:tnotit=% == %PARAM% set mkvTitle= &REM dont add titles to MKVs
  	set audioFile=!FilePath!!audioAltPrefix!!FileName!.!mkAudioExt!
  	IF not exist !audioFile! set audioFile=!FilePath!!FileName!.!mkAudioExt!
    IF exist !audioFile! (
      echo !mkvloc!mkvmerge.exe -o "!FilePath!ZZ!FileName!.mkv" ^
        --forced-track 0:no -A = "!FilePath!!FileName!.!mkVidExt!" ^
        --forced-track 0:no -D -S -B -T --no-chapters -M ^
        !mkvTitle! --no-global-tags = "!audioFile!"
      start /wait /belownormal /min !mkvloc!mkvmerge.exe -o "!FilePath!ZZ!FileName!.mkv" ^
        --forced-track 0:no -A = "!FilePath!!FileName!.!mkVidExt!" ^
        --forced-track 0:no -D -S -B -T --no-chapters -M ^
        !mkvTitle! --no-global-tags = "!audioFile!"
      set doneFile=!FilePath!ZZ!FileName!.mkv& set doneMsg=!time:~,-3! - Status: !errorlevel! - File: !doneFile!
      if %PARAM:znoerr=% == %PARAM% if not !errorlevel!==0 goto :delDoneFile& REM exit on error
      echo !doneMsg!
      echo .
    ) ELSE echo *** !mkAudioExt! missing: !audioFile!
  ) ELSE echo .mkv exists, to reprocess del: !FilePath!ZZ!FileName!.mkv

GOTO :fin

  IF %PARAM:znod=% == %PARAM% del "!doneFile!" >nul 2>nul& REM delete half processed file
  set errorStatus=!doneMsg!
if !mkAudioExt!==help call :helpMakeMkv&REM shows help if no audio command received (f/w)
IF not %PARAM:zof=% == %PARAM% DShutdown.exe /m8 /PowerOff /ForceAfterwait & REM shutdown computer
	REM psshutdown.exe -c -t 360& REM shutdown computer
IF not "!errorStatus!"=="ok" ( echo *&echo *&echo *** Error: !errorStatus!&echo *&echo * )
IF %PARAM:znop=% == %PARAM% pause
title %comspec%
rem The End

Post Reply