Here is my suggestion for the batch file:
@echo off
rem A simple script to convert a png or jpg image sequence to an mp4 file with ffmpeg
cls
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables
echo or this script won't work.
echo/
echo This will convert all image files in the following directory to a single mp4:
echo/
echo %cd%
echo/
%SystemRoot%\System32\choice.exe /C PJC /N /M "Are the files PNGs or JPEGs or Cancel (P/J/C)? "
if errorlevel 3 color & goto :EOF
echo/
if errorlevel 2 (
ffmpeg.exe -i %%04d.jpg out.mp4
) else (
ffmpeg.exe -i %%04d.png out.mp4
)
color
The character %
must be escaped in a batch file with one more %
to be interpreted as literal character which was the main problem causing the batch file not working as expected. %0
references the string used to start the batch file which was img2mp4.bat
. So %04d.jpg
concatenated img2mp4.bat
with 4d.jpg
and the result was running ffmpeg.exe
with img2mp4.bat4d.jpg
as file name instead of the argument string %04d.jpg
.
To reference one or more files/folders in current directory the file/folders can be simply specified in arguments list of a script or application with no path. This is explained in Microsoft documentation about Naming Files, Paths, and Namespaces. This page describes further that on Windows the directory separator is the backslash character \
and not the forward slash /
as on Linux and Mac. /
is used on Windows mainly for options as it can be seen on line with command CHOICE because of this character is not possible in file/folder names. -
is used on Linux/Mac for options which is possible also in file/folder names even as first character of a file/folder name. So on Windows \
should be always used as directory separator although the Windows kernel functions for file system accesses automatically correct /
in file/folder names to \
.
CHOICE is much better for prompting a user to take a choice from several offered options than the command SET with option /P
. set/p
is syntactically not correct at all because of command set
should be separated with a space from option /P
which should be separated with a space from next argument variable=prompt text
. set/p
forces cmd.exe
to automatically correct the command line to set /p
. Batch files should be syntactically correct coded and not depend on automatic error corrections of Windows command processor.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains how to reference batch file arguments.
echo /?
rem /?
cls /?
title /?
color /?
set /?
choice /?
if /?
goto /?
Further I suggest to read: