0
cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4

A script with just this in it works completely fine and outputs exactly what I need it to, but:

:: A simple script to convert a png or jpg image sequence to an         
mp4 file with ffmpeg
cls
@echo off
title PNG2MP4
color C
echo Ensure you have ffmpeg installed and setup in your environment variables or this script won't work.


:QUERY
echo This will convert all image files in the following directory to a single mp4, 
echo %cd%
echo are the files PNGs or JPEGs(PNG/P/JPG/J/CANCEL)?
set/p "ch=>"
if /I %ch%==PNG goto CONVERTPNG
if /I %ch%==P goto CONVERTPNG
if /I %ch%==JPG goto CONVERTJPG
if /I %ch%==J goto CONVERTJPG
if /I %ch%==CANCEL goto :eof
echo Invalid choice & goto QUERY

:CONVERTPNG
cd %cd%
ffmpeg -i %cd%/%04d.png out.mp4

:CONVERTJPG
cd %cd%
ffmpeg -i %cd%/%04d.jpg out.mp4

This more complex version of the script fails, outputting:

C:\tmp/img2mp4.bat4d.jpg: No such file or directory

Why is it no longer calling the files that it did before and is there an easy fix for this?

R. Elias
  • 1
  • 2
  • Running ffmpeg -i %04d.png out.mp4 from the command line gave me the output desired and I wanted to flesh this out in a script so I could automate doing this, I pulled the %04d syntax from a post to do with batch ffmpeg commands, and this sounds unsuitable for my use case. I have a directory of files that will be 0001.png and so on, how would I tell ffmpeg to take these in with the batch script? – R. Elias Nov 26 '18 at 10:44
  • Unrelated to issue in question, but I recommend adding `-vf format=yuv420p` to output a compatible chroma subsample scheme. Otherwise you may get yuv444p which dumb players can't decode (assuming you are encoding with libx264). – llogan Nov 26 '18 at 18:48

1 Answers1

1

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143