ExifTool Forum

General => Metadata => Topic started by: esdoublelef on January 27, 2022, 11:35:30 AM

Title: FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)
Post by: esdoublelef on January 27, 2022, 11:35:30 AM
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
Title: Re: FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)
Post by: StarGeek on January 27, 2022, 02:11:10 PM
Matching StackOverflow question (https://stackoverflow.com/questions/70873158/ffmpeg-batchfile-to-compress-images-jpg-jpegs-and-keep-exif-metadata?noredirect=1#comment125308009_70873158)

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 (https://exiftool.org/exiftool_pod.html#ext-EXT---ext-EXT--extension) and Common Mistake #2 (https://exiftool.org/mistakes.html#M2)), 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 (https://exiftool.org/exiftool_pod.html#w-EXT-or-FMT--textOut) 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.
Title: Re: FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)
Post by: esdoublelef on January 27, 2022, 08:21:33 PM
@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
Title: Re: FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)
Post by: Phil Harvey on January 27, 2022, 10:01:09 PM
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 (https://exiftool.org/exiftool_pod.html#tagsFromFile-SRCFILE-or-FMT) for an explanation.)

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

- Phil
Title: Re: FFMPEG batchfile to compress images JPG JPEGs with EXIFTOOL keep EXIF (metadata)
Post by: esdoublelef on January 28, 2022, 07:25:41 AM
@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!