This is definitely hard to decide where to post. I am trying to modify several sequential jpegs into png files with the Created date superimposed on them and then they will be made into a video so the time will be a permanent part and not subject to changes made by copying, moving, or 'touch', etc.
Below is a script that works only if run once and then terminated with CTRL-C and then rerun again in the same Window. Please understand, batch files are not my forte and I have tried MANY variations but none gives results that I need. I have highlighted the areas that fails and the ones that work. If anyone can point me in the right direction or tell me how to fix this I would appreciate it greatly.
tmpfile.txt --> File Creation Date/Time : 2014:12:18 01:22:00-06:00
Funny little characters ---> <colon X>
---------------RUN 1 Script ------------------------------------------------------
for %a in (*.jpeg) DO (
call exiftool.exe -FileCreateDate %a 1>tmpfile.txt
FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %a in (tmpfile.txt) DO (
set string1=%e
set string2=%f
set string3=%g
set string4=%h
set string5=%i
set string6=%j
set final=%e\:%f\:%g %h\:%i\:%j
setlocal enabledelayedextension
)
"ffmpeg.exe" -i "%a" -vf "format=yuv420p,drawtext=fontfile='Arial':fontsize =20:
text='Created '=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontcolor=white@0.5:box=1:
boxcolor=0x00000000@0.5" "%a.png"
pause
setlocal disabledelayedextension
)
-----------------------RESULT FROM RUN 1-------------------------------
C:\Users\Bud\Desktop\EXIFsubsInImages>(
call exiftool.exe -FileCreateDate image-0000.jpeg 1>tmpfile.txt
FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %a in (tmpfile.txt) DO (
set string1=%e
set string2=%f
set string3=%g
set string4=%h
set string5=%i
set string6=%j
set final=%e\:%f\:%g %h\:%i\:%j
setlocal enabledelayedextension
)
"ffmpeg.exe" -i "image-0000.jpeg" -vf "format=yuv420p,drawtext=fontfile='Arial'
:fontsize=20:text='Created '=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontcolor=whi
te@0.5:box=1:boxcolor=0x00000000@0.5" "image-0000.jpeg.png"
pause
setlocal disabledelayedextension
)
C:\Users\Bud\Desktop\EXIFsubsInImages>(
set string1=12
set string2=18
set string3=01
set string4=22
set string5=00
set string6=06
set final=12\:18\:01 22\:00\:06
setlocal enabledelayedextension
)
Invalid parameter to SETLOCAL command
--------AFTER CTRL-C and RERUN SAME SCRIPT-----------------------------
for %a in (*.jpeg) DO (
call exiftool.exe -FileCreateDate %a 1>tmpfile.txt
FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %a in (tmpfile.txt) DO (
set string1=%e
set string2=%f
set string3=%g
set string4=%h
set string5=%i
set string6=%j
set final=%e\:%f\:%g %h\:%i\:%j
setlocal enabledelayedextension
)
"ffmpeg.exe" -i "%a" -vf "format=yuv420p,drawtext=fontfile='Arial':fontsize =20:
text='Created 12\:18\:01 22\:00\:06'=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontc
olor=white@0.5:box=1:boxcolor=0x00000000@0.5" "%a.png"
pause
setlocal disabledelayedextension
)
------------RESULT FROM RUN 2------------------------------------
C:\Users\Bud\Desktop\EXIFsubsInImages>(
call exiftool.exe -FileCreateDate image-0000.jpeg 1>tmpfile.txt
FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %a in (tmpfile.txt) DO (
set string1=%e
set string2=%f
set string3=%g
set string4=%h
set string5=%i
set string6=%j
set final=%e\:%f\:%g %h\:%i\:%j
setlocal enabledelayedextension
)
"ffmpeg.exe" -i "image-0000.jpeg" -vf "format=yuv420p,drawtext=fontfile='Arial'
:fontsize=20:text='Created 12\:18\:01 22\:00\:06'=(w-(tw*1.05)):y=main_h-(text
_h*1.2):fontcolor=white@0.5:box=1:boxcolor=0x00000 000@0.5" "image-0000.jpeg.png"
pause
setlocal disabledelayedextension
)
C:\Users\Bud\Desktop\EXIFsubsInImages>(
set string1=12
set string2=18
set string3=01
set string4=22
set string5=00
set string6=06
set final=12\:18\:01 22\:00\:06
setlocal enabledelayedextension
)
Invalid parameter to SETLOCAL command
+ Reply to Thread
Results 1 to 21 of 21
-
-
I do not know what is correct syntax for that drawtext filter, it seems to show in wrong way without putting it into Code tags, I just guessed how it should be, but in your script you create variables , like "final" and do not use them later. You can get variable from text file right away , something like that, this is batch script:
Code:for %%a in (*.jpeg) DO ( exiftool.exe -FileCreateDate %%a 1>tmpfile.txt set /p text= < tmpfile.txt "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext="fontfile='Arial':fontsize=20:text='%text%':x=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontcolor=white@0.5:box=1:boxcolor=0x00000000@0.5" "%%~na.png" ) pause
-
I forgot about that enabledelayedextension looking at it , to avoid setting that setlocal enabledelayedextension, without it it would not even work above,
Code:for %%a in (*.jpeg) DO ( exiftool.exe -FileCreateDate %%a 1>tmpfile.txt set /p text= < tmpfile.txt call :ffmpeg "%%a" "%text%" ) pause :ffmpeg "ffmpeg.exe" -i "%~1" -vf "format=yuv420p,drawtext="fontfile='Arial':fontsize=20:text='%~2':x=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontcolor=white@0.5:box=1:boxcolor=0x00000000@0.5"" "%%~n1.png" goto :eof
Code:setlocal enabledelayedextension for %%a in (*.jpeg) DO ( exiftool.exe -FileCreateDate %%a 1>tmpfile.txt set /p text= < tmpfile.txt "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext="fontfile='Arial':fontsize=20:text='!text!':x=(w-(tw*1.05)):y=main_h-(text_h*1.2):fontcolor=white@0.5:box=1:boxcolor=0x00000000@0.5"" "%%~na.png" ) pause
-
The syntax is correct for the drawtext and actually works for the second try after CTRL-C and then rerunning it and actually produces the image below. I have changed setlocal EnableExtensions, EnableDelayedExtensions, disableDelayed extensions and disable extensions. Somehow the script works the second try but not the first. It is a mystery to me...
The Below simply outputs the text from EXIFTOOL to a file called tmpfile.txt which can be called by FFMPEG as a parsed variable (string1, string2, string3, etc.) to be used by FFMPEG:
exiftool.exe -FileCreateDate %%a 1>tmpfile.txt
set /p text= < tmpfile.txt
File Creation Date/Time : 2014 :12:18 01:22:00-06:00
%%a=File
%%b=Creation
%%c=Date/Time
%%d= :
%%e string 1=2014
...
Blue begins %%e and The colons must be escaped with'\' in order to work in ffmpeg drawtext use.
set final=%e\:%f\:%g %h\:%i\:%j
SECOND RUN Image Produced:
Anybody know why it fails on initial run?Last edited by Budman1; 24th Dec 2014 at 01:40.
-
-
More than likely it's the nested FOR commands mixed with the multiple lines of commands in brackets. There must be some temporary file being generated by the batch and for some reason it jams on the first run, but on the second run the batch can just skip over the problem hence why you don't get the results you want. Go to folder options in Control Panel and enable display of file extension, then go to the system temp folder (default is Window\Temp) and look for files with odd characters and no extension like $#G!. It might help if you delete everything in the temp first, then run your batch once. If you delete those files then try to run the batch again and it fails you'll know that's what's happening. You would be better off to change the nested FOR's to a call to a secondary batch or a subroutine
-
yes, sorry, I just copied it blindly, SETLOCAL ENABLEDELAYEDEXPANSION it is
, all variables that are used again within parentheses, if they were created somewhere above (within those parentheses), have this syntax: !variable! as oppose normal: %variable% syntax, batch script works in a way that all within parentheses is being read first not as one would suggest where a line is being read and executed right away, so you cannot create variable and then use it in other line (within same parentheses) , only SETLOCAL ENABLEDELAYEDEXPANSION fixes that, but you cannot use ! character in script for printing etc., ... because ! character it is used to mark variables
best to avoid it sometimes is to use subroutines - using call, as in that example, where you get out of that parentheses trap and use normal %variable% syntax, this way, scripts become much more readable if it gets more elaborate ... but sometimes one has to use it for calculations etc.Last edited by _Al_; 24th Dec 2014 at 13:53.
-
Sorry so long to reply but I never got notice of posting to remind me...
I could never get this to work for repetitive images so I Use a GUI that builds and calls the following , with WaitForExit() and then exits the DOS/CMD screen before starting another loop.
Thanks to everyone that helped and I used a little of each suggestion for the final result:
::1.bat
setlocal()
Call :2bat
ffmpeg.exe -i "[foundfile built by GUI]" -vf drawtext="'fontfile=Times New Roman':fontsize=20:text='Created %final%'=(w-tw)/2: y=(h-h)/2: fontcolor=white@0.5: box=1:boxcolor=0x00000000@0.5" "[foundfile built by GUI].png"
exit
:2bat
Call :3bat
FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %%a in (tmpfile.txt) DO (
set string1=%%d
set string2=%%e
set string3=%%f
set string4=%%g
set string5=%%h
set string6=%%j
ENDLOCAL & SET final=%%d\:%%e\:%%f %%g\:%%h\:%%i&SET imagepath="[foundfile built by GUI]"&set outpath="[foundfile built by GUI].png"
echo %final% %imagepath% %outpath%
)
:3bat
call "C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2\bin\De bug\exiftool.exe" -CreateDate -FileCreateDate "[foundfile built by GUI]" > tmpfile.txt
The result is:
-
-
I will give the start wait a shot since itvwould be better if it ran all in a batch file. Report more later
thanks! -
I had some time to sit and get my eyes crossed looking at what you had and I think this might do what you want
Code:@ECHO OFF FOR %%a IN (*.jpeg) DO ( FOR /F "usebackq tokens=4,5,6,7,8,9,10,11 delims=-: " %%b IN (`"C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2\bin\De bug\exiftool.exe" -FileCreateDate "%a"`) DO CALL ffmpeg.exe -i "%a" -vf drawtext="'fontfile=Times New Roman':fontsize=20:text='Created %b-%c-%d %e\:%f\:%g-%h\:%i':x=(w-tw)/2: y=(h-h)/2: fontcolor=white@0.5: box=1:boxcolor=0x00000000@0.5" "%~dpna.png" )
Code:@ECHO OFF FOR %%a IN (*.jpeg) DO ( FOR /F "tokens=4,5,6,7,8,9,10,11 delims=-: " %%b IN ('"C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2\bin\De bug\exiftool.exe" -FileCreateDate "%a"') DO CALL ffmpeg.exe -i "%a" -vf drawtext="'fontfile=Times New Roman':fontsize=20:text='Created %b-%c-%d %e\:%f\:%g-%h\:%i':x=(w-tw)/2: y=(h-h)/2: fontcolor=white@0.5: box=1:boxcolor=0x00000000@0.5" "%~dpna.png" )
If you want to post code use the advanced post button and click on the HASH button to surround your code with tags, that way things like a colon followed by an X won't get converted to a sickly smiley: : x => -
Nic2k4, Thanks for that but either way I get:
Code:C:\Users\Bud\Desktop\EXIFsubsInImages>C:\Users\Bud\Desktop\EXIFsubsInImages\combo2.bat The following usage of the path operator in batch-parameter substitution is invalid: %~dpna.png"
For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.
Looks good to me but it always has same error? -
Okay ... New closer... ran:
@ECHO OFF
FOR %%a IN (*.jpeg) DO (
FOR /F "usebackq tokens=4,5,6,7,8,9,10,11 delims=-: " %%b IN ('"C:\Users\Bud\Desktop
\MEGA_Trimmed_Tabbed_2\bin\Debug\exiftool.exe" -FileCreateDate "%%a"') DO CALL ffmpeg.exe -i "%%a" -vf
drawtext="'fontfile=Times New Roman':fontsize=20:text='Created %%b-%%c-%%d %%e\:%%f\:%%g-%%h\:%%i'=
(w-tw)/2:y=(h-h)/2: fontcolor=white@0.5:box=1:boxcolor=0x00000000@0.5" "%%~dpna.png"
)
It created all files but the date embedded in the image says:
Created image-001.jpeg- ::-:
I think it is close though. -
Doh! Where was my head, of course you need to add one % to compensate the one you lose in a batch.
You get blanks because the b to i variables didn't get expanded, I don't know where the image and 001.jpeg came from. Maybe the exiftool output is not as expected or it didn't work. I don't think this needs delayed expansion, best to tackle one issue at a time. Copy a couple image files to a test folder and run this batch
Code:FOR %%a IN (*.jpeg) DO CALL "C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2\bin\De bug\exiftool.exe" -FileCreateDate "%%a"
Oops, just noticed you replaced the back quotes with regular quotes in the exiftool command, so exiftool didn't run and only the file name got passed through the %a variable. BTW, did all images get embedded with image-001.jpeg or did the number increase? I'd still like you to run the batch to see the exiftool output.Last edited by nic2k4; 1st Jan 2015 at 10:54. Reason: one more thing
-
WHoopee (Almost. this code works but I have been unable to get the results into another directory instead of intermixing into the originals. No amount of changing the final %%~dpna will get it into a folder in this path.
Code:for %%a in (image*.jpeg) do ( "exiftool" -CreateDate -FileCreateDate "%%a" > single.txt FOR /F "tokens=1, 2, 3, 4, 5, 6, 7-15* delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial':fontsize=20:text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j': x=(w-(tw*1.05)): y=main_h-(text_h*1.2): fontcolor=white@0.5: box=1: boxcolor=0x00000000@0.5" "%%~dpna.png" ) )
This is as close as I have gotten thanks to all the help. Either Batch language is awfully picky or else mine is. LOL
AND Yes I know that 3 minutes until 10:00PM is not that light out but this is an advertisement and not a video from a camera that encodes it's CreateDate. I had to settle for the FileCreateDate for testing. I pull both so if the Createdate displays on the first line, the program will use it. If both are sent to the temporary file, the batch will use the first line or TRUE CreateDate.
-
Does this do what you want
Code:for %%a in (image*.jpeg) do ( IF NOT EXIST "%%~dpa\processed" MD "%%~dpa\processed" "exiftool" -CreateDate -FileCreateDate "%%a" > single.txt FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial':fontsize=20:text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j': x=(w-(tw*1.05)): y=main_h-(text_h*1.2): fontcolor=white@0.5: box=1: boxcolor=0x00000000@0.5" "%%~dpaprocessed\%%~na.png" ) )
-
nic2k4, No it makes a folder named %~dpa and then a folder inside named process and says DO was unexpected ( second one) but...
Code:for %%a in (image*.jpeg) do ( IF NOT EXIST "%%~dpa\processed" MD "%%~dpa\processed" "exiftool" -CreateDate -FileCreateDate "%%a" > single.txt FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial': fontsize=20: text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j': x=(w-(tw*1.05)): y=main_h- (text_h*1.2): fontcolor=white@0.5: box=1: boxcolor=0x00000000@0.5" "%%~dpa\processed\%%~na.png" ) )
This one works Great with a New directory. Thanks very much and this will get me there!!!! -
Excellent. Sorry, another slip up I forgot the backslash on the ffmpeg destination path. Good of you to have fixed it by your lonesome. Just to clear up a little bit of confusion, the \ is not an escape character for CMD or Command, it's a normal path character. The escape character is the caret ^. FFMPEG uses the backslash to escape characters, that's something unique to it.
Also, your usage of tokens is a little bit off:
Code:FOR /F "tokens=1,2,3,4,5,6,7-15* delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial': fontsize=20: text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j'
To properly use tokens and have full control of the results you should change the tokens statement to match only what you need and know is there. If we take the exiftool output you posted before
File Creation Date/Time : 2014 :12:18 01:22:00-06:00
Code:FOR /F "tokens=4,5,6,7,8,9,10,11 delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial': fontsize=20: text='Created %%b/%%c/%%d %%e\:%%f\:%%g-%%h\:%%i'
Last edited by nic2k4; 1st Jan 2015 at 22:05.
-
Yes you are 100% correct and a lot of that I found the hard way. UGH! but:
You enumerate all the tokens from 1 to (15+the remainder), but you're only using the 4th (%e), 5th (%f). 6th (%g) and 7th (%h). 7-15 means the 7th to the 15th is one token, add the * and that says everything else that follow the 15th character is also part of that one token %h. So %i and %j will always be blank and not needed and %h could include things you don't expect.
To properly use tokens and have full control of the results you should change the tokens statement to match only what you need and know is there.
Code:%%a=File %%b=Creation %%c=Date/Time %%d=: %%e=2014 %%f=12 %%g=17 %%h=21 %%i=57 %%j=19
Luckily I added the delim of - dash and the 7-15 have delims between them so they end where I needed them. I will put my working BAT file in a safe place and then fix the tokens as you mentioned. LOL
Just to clear up a little bit of confusion, the \ is not an escape character for CMD or Command, it's a normal path character. The escape character is the caret ^. FFMPEG uses the backslash to escape characters, that's something unique to it.
I might also point out the help was invaluable from everyone because I needed to have a routine that can be called as a batch file in a cmd window and also that can be sent to a CMD window after it is is opened. Trust me the same batch file doesn't work the same both ways due to Visual Studios. Thanks Microsoft.!
In order to make it work, for anyone that may be interested, with the final working version the coding is like:
Code:If cbCrtImgs2Vid.Checked = True Then strFileText = "for %%a in (""" & filepath & "\image*.jpeg"") do (" & vbCrLf strFileText = strFileText & "IF NOT EXIST ""%%~dpa\processed"" MD ""%%~dpa\processed""" & vbCrLf strFileText = strFileText & """exiftool"" -CreateDate -FileCreateDate ""%%a"" > tmpfile.txt" & vbCrLf strFileText = strFileText & "FOR /F ""tokens=1, 2, 3, 4, 5, 6, 7-15* delims=-: "" %%b in (tmpfile.txt) DO (" & vbCrLf strFileText = strFileText & """ffmpeg.exe"" -i ""%%a"" -vf ""format=yuv420p,drawtext=fontfile='Arial':" _ & "fontsize=20:text='Created %%e\:%%f\:%%g %%h\:%%i\:%%j': x=(w-(tw*1.05)): y=main_h-(text_h*1.2): fontcolor=white@0.5:" _ & " box=1: boxcolor=0x00000000@0.5"" ""%%~dpa\processed\%%~na.png""" & vbCrLf strFileText = strFileText & ")" & vbCrLf strFileText = strFileText & ")" & vbCrLf Debug.Print(strFileText) '------------write to file.bat Dim fnum = FreeFile() FileOpen(fnum, "combo.bat", OpenMode.Output) Print(fnum, strFileText) FileClose(fnum) '----------writing to cmd screen? Dim MyProcess As New Process With MyProcess.StartInfo .FileName = "CMD.EXE" .Arguments = "/k C:\Users\Bud\Desktop\MEGA_Trimmed_Tabbed_2\bin\Debug\combo.bat" .UseShellExecute = True .CreateNoWindow = False '.RedirectStandardInput = True '.RedirectStandardOutput = True '.RedirectStandardError = True End With MyProcess.Start() End If
-
%a is declared in the first FOR command which lists all the files with a jpeg extension. %b is declared in the second FOR command and is associated to the first token in the list, thus the second token gets associated to %c... Microsoft uses %i in its FOR command help examples, so the first token goes with %i, the second %j...
this batch would produce those results
Code:FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=-: " %%a in (single.txt) DO ( ECHO %%a ECHO %%b ECHO %%c ECHO %%d ECHO %%e ECHO %%f ECHO %%g ECHO %%h ECHO %%i ECHO %%j )
Code:FOR /F "tokens=5,6,7,8,9,10,11,12 delims=-: " %%b in (single.txt) DO ( "ffmpeg.exe" -i "%%a" -vf "format=yuv420p,drawtext=fontfile='Arial': fontsize=20: text='Created %%b/%%c/%%d %%e\:%%f\:%%g-%%h\:%%i'