Page 1 of 1

Automatically insert .mkv filename into .bat file

Posted: Sun May 09, 2021 12:13 am
by allomere
I'm trying to write a quick 'n dirty .bat file that will detect the filename of an mkv file in a directory and automatically insert that name into the .bat file (am piping the .mkv through ffmpeg). The .bat file will be in my path so that I can use it in any directory. Does anyone know how to do this?

Re: Automatically insert .mkv filename into .bat file

Posted: Sun May 09, 2021 12:26 am
by Woodstock
Assuming by "bat file" you mean Windows... This might be a good start.

Re: Automatically insert .mkv filename into .bat file

Posted: Sun May 09, 2021 7:39 pm
by allomere
Thanks! Looks kinda tricky, but I'll give it a shot!

Re: Automatically insert .mkv filename into .bat file

Posted: Sun May 09, 2021 8:23 pm
by d00zah
Here's an example I had archived. Just modify the path to the ffmpeg executable & ffmpeg arg defs ('SET ...') to your preference:

Code: Select all

@ECHO OFF
SET "FFMPEG=C:\Users\username\AppData\Roaming\Emby-Server\system\ffmpeg.exe"
SET "VCODEC=-vcodec copy"
SET "ACODEC=-acodec copy"
REM SET "SCODEC=-scodec copy"

FOR /r %%F IN (*.mkv) DO (

	ECHO "%FFMPEG%" -i "%%F" %VCODEC% %ACODEC% %SCODEC% "%%~dpnF.mp4"
	REM IF NOT ERRORLEVEL 1 IF EXIST "%%~dpnF.mp4" DEL /Q "%%F"

)
Currently, it'll do nothing when executed except...

- Find ANY .mkv below wherever you execute the .bat
- Print the ffmpeg command line to convert the .mkv to.mp4 in the same folder (remove 'ECHO ' to actually execute the conversion)
- The next line will remove the original .mkv IF the conversion is successful (remove 'REM ' if deletion is desired)

& let you experiment w/ some of those strange args the tutorial describes. Hope this helps?