Running convert from bat file

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
martinkk
Posts: 2
Joined: 2016-01-07T03:37:03-07:00
Authentication code: 1151

Running convert from bat file

Post by martinkk »

Hi,
I would like to create some mp4 videos from webcam images I take every 15mins.

That's how I would like to do it (works fine when I run the commands manually in cmd).

convert *.JPG -delay 10 -morph 10 %05d.morphed.jpg
ffmpeg -r 25 -i %05d.morphed.jpg video.mp4

The idea was to run these two commands on all sub-folders, where my webcam images are stored:

Thjat's my bat file:

Code: Select all

set rootdir="C:\Users\martin\Pictures\_Webcam\test"

for /d %%i in (%rootdir%\*) do (
  convert *.JPG -delay 10 -morph 10 %05d.morphed.jpg
  ffmpeg -r 25 -i %05d.morphed.jpg video.mp4
)
pause
The problem is, that %05d is not recognized properly as a command of "convert" and ffmpeg, so I tried doing the following:

Code: Select all

set rootdir="C:\Users\martin\Pictures\_Webcam\test"
set counter=0

for /d %%i in (%rootdir%\*) do (
  ::echo %%i
  mkdir %%i\video
  convert %%i\*.JPG -delay 10 -morph 10 %%i\video\morph.jpg
  ffmpeg -r 25 -i %%i\video\*.jpg %%i\video\webcam.mp4
)
pause

Windows now creates lots of morph-0....morph-#####.jpg files. That's fine!
Unfortunately, the "convert" command does not work from the bat file. it says:

Could find no file with path 'C:\Users\martin\Pictures\_Webcam\test\2014-10-29\video\*.jpg' and index in the range 0-4
C:\Users\martin\Pictures\_Webcam\test\2014-10-29\video\*.jpg: No such file or directory

Any ideas how to mask(?)the %0 properly?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Running convert from bat file

Post by snibgo »

Code: Select all

Unfortunately, the "convert" command does not work from the bat file. it says:

Could find no file with path 'C:\Users\martin\Pictures\_Webcam\test\2014-10-29\video\*.jpg' and index in the range 0-4
That message is from ffmpeg, not convert. Try ffmpeg with the name "morph-%d.jpg".
snibgo's IM pages: im.snibgo.com
martinkk
Posts: 2
Joined: 2016-01-07T03:37:03-07:00
Authentication code: 1151

Re: Running convert from bat file

Post by martinkk »

@snibgo
Yes I know, but isn't there a way to easily mask the "%05d" somehow, so that the two commands are working the same way as entered manually?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Running convert from bat file

Post by snibgo »

I don't understand hte question, but I see that in ...

Code: Select all

for /d %%i in (%rootdir%\*) do (
  convert *.JPG -delay 10 -morph 10 %05d.morphed.jpg
  ffmpeg -r 25 -i %05d.morphed.jpg video.mp4
)
... you didn't double the % in "%05d.morphed.jpg". So in a BAT file, it won't get passed to IM.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Running convert from bat file

Post by GeeMack »

martinkk wrote:I would like to create some mp4 videos from webcam images I take every 15mins.

That's how I would like to do it (works fine when I run the commands manually in cmd).

convert *.JPG -delay 10 -morph 10 %05d.morphed.jpg
ffmpeg -r 25 -i %05d.morphed.jpg video.mp4

The idea was to run these two commands on all sub-folders, where my webcam images are stored:
Using Windows 7 64, ImageMagick 7 64, and ffmpeg 64 bit binary built for Windows, version N-72443-g2e15f07 released in November of 2015, I can run this command without errors...

Code: Select all

convert *.jpg -strip -morph 10 JPEG:- | ffmpeg -r 25 -i pipe:0 video.mp4
It takes all the JPGs in the directory, morphs 10 intermediate images between each adjacent pair, pipes them out to ffmpeg which takes the sequence of images as its input, sets a frame rate of 25fps, and outputs an MP4 video file.

The "JPEG:-" tells IM to output the results as JPG formatted images and send them to the pipe. In the ffmpeg command the "-i pipe:0" tells it to use the contents of the pipe as its input.

I had to add the "-strip" operator to the IM command because there's something in my JPGs that made ffmpeg complain, and that seemed to cure the problem.

I would probably specify a bitrate to the ffmpeg command by adding "-b:v 4000k" before the output file name to get a better quality than ffmpeg's default. Of course ffmpeg provides several methods to output particular quality, size, frame rates, formats, etc. Add any filters/etc. you need after the input "-i pipe:0".

If you want to process subdirectories, in the folder that contains the subdirectories you can make a batch file that looks something like this...

Code: Select all

for /d /r %%D in ( * ) do (
   pushd "%%D"
   convert *.jpg -strip -morph 10 JPEG:- | ffmpeg -r 25 -i pipe:0 video.mp4
   popd
)
Running that script will CHDIR into each subdirectory (and sub-sub-etc-directory), run the combined IM and ffmpeg command, leave a "video.mp4" file, return to the top directory, and go on to the next one. If you're running this on a regular basis and there's already a "video.mp4" in the directory, and if you want ffmpeg to overwrite the file without prompting, put a "-y" in the ffmpeg part of your command.

EDITED TO ADD: If you want to run the batch file from somewhere other than the top level directory where you're going to convert the images to videos, you can specify the working directory by naming it in the "for" command like this.

Code: Select all

set ROOTDIR=C:\webcamfiles\

for /d /r %ROOTDIR% %%D in ( * ) do (
   pushd "%%D"
   convert *.jpg -strip -morph 10 JPEG:- | ffmpeg -r 25 -i pipe:0 video.mp4
   popd
)
It will start in "C:\webcamfiles\" and process all the JPGs in the subdirectories under there.
Post Reply