FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)

Started by esdoublelef, January 27, 2022, 11:35:30 AM

Previous topic - Next topic

esdoublelef

I tried searching everywhere for a possible solution but I really can't find it. Hope someone can help me out here.

I have written a batch file to use FFMPEG to compress and sharpen JPGs in a folder, then output with a date 2022 01 22 affixed to the final output jpg file.

But the EXIF data is gone. (because of FFMPEG) hence i am hoping to integrate EXIFTOOL into my batch file to create a one-step solution to write the EXIF back onto the output jpg file.


@ECHO ON

FOR %%a in (*.jpg) DO (ffmpeg -i "%%a" -q:v 8 -vf unsharp=5:5:1.0:5:5:0.0  "2022 01 22 %%~na".jpg

exiftool -overwrite_Original -TagsFromFile "%%f.pef" -All:All )

PAUSE


I understand the advise is to not put exiftool into a batch loop, at least that's what i've read. but can anyone help me tweak the code for it to work? I'm stuck at trying to get EXIFTOOL to get the metadata from the original jpg into the output jpg.

I have an existing solution using ImageMagick but i'm trying to get this FFMPEG to work.

Really appreciate any help i can get here!

Regards,
Samuel

StarGeek

Matching StackOverflow question

Yes, you don't want to run exiftool in a loop.  Place it after the loop and just pass the directory to process to exiftool.  Since it appears to be the current directory, you would use a dot .

But based upon this, some more clarification is needed.  The command you list here shows you are copying from a .pef file, but your script is working only on jpegs.

The one thing I did miss was to remove the leading prefix when looking for the source file.
exiftool -overwrite_Original -ext jpg -TagsFromFile "%%d%%.11f.pef" -All:All .

This will parse the current directory, represented by the dot ., for any jpegs (-ext jpg, see the -ext (-extension) option and Common Mistake #2), then look for a matching pef file in the same directory (%%d) with the same base name without the first 11 characters (%%.11f) and copy all data from that file to the exact same location in the jpg.  Change the .pef to .jpg to copy from the source jpgs instead.  See the -w (-TextOut) option for details on the % tokens.

Since it appears that the original jpgs are in the same directory, there will be a file not found error for those files, but that can be ignored.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

esdoublelef

@stargeek, how does one send a virtual coffee over to wherever you are? thank you so much for spending time on this.

i updated my batch file and the EXIF works like a charm! i used the %%d%%.11f to help find the original file after the 11 date characters

but, one last issue if you could help me out:

how to i prevent the portrait photos from being rotated to landscape? I understand it's something do with the orientation tag, but it always seems like a conflict between windows and exiftool reading the orientation tag.


This is my updated batch file:

@ECHO ON


  FOR %%a in (*.jpg) DO (ffmpeg -i "%%a" -q:v 8 -vf unsharp=5:5:1.0:5:5:0.0 "2022 01 22 %%~na".jpg)

@ECHO ON

exiftool -overwrite_Original -TagsFromFile  %%.11f.jpg -ext JPG .


PAUSE

Phil Harvey

I'm guessing that you should exclude Orientation from being copied:

exiftool -overwrite_Original -TagsFromFile %%.11f.jpg --orientation -ext JPG .

Also, I would recommend copying into the same locations as the source.  You do this by adding -all:all (See the 2nd paragraph in the -tagsFromFile section of the application documentation for an explanation.)

exiftool -overwrite_Original -TagsFromFile %%.11f.jpg -all:all --orientation -ext JPG .

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

esdoublelef

@phil @stargeek thank you guys so much for your help. hopefully those who are looking for a similar batch file as mine will find this useful:

FFMPEG loop 
-q:v 8 reduces quality of jpg
unsharp=5:5:1.0:5:5:0.0 sharpens image (default values)
"2022 01 22 %%~na".jpg appends date to file name (i change it to the date i want)


@ECHO ON
  FOR %%a in (*.jpg) DO (ffmpeg -i "%%a" -q:v 8 -vf unsharp=5:5:1.0:5:5:0.0 "2022 01 22 %%~na".jpg)
@ECHO ON
exiftool -overwrite_Original -TagsFromFile %%.11f.jpg -all:all --orientation -ext JPG .
PAUSE


it works perfectly now, thank you all!