Possible to inject capture timestamp for M2TS files?

Started by somy, July 16, 2019, 03:54:01 AM

Previous topic - Next topic

somy

Hi all,
I'm trying to use ExifTool to fix timestamps of my video library - now I can see it's not a trival task as different tools (LR, Photos etc.) seem to interprete tags in different ways. As an example for MP4 with CreateDate in GMT, LR treat it as local time while Photos seems to be able to read timezone info. It's a total mass but my biggest problem is with MTS/M2TS files captured by Sony NEX-7. As an example below shows all existing tags:
C:\Users\BB9137\Desktop\Project>exiftool -s "2013-09-01 20130901122746.MTS"
ExifToolVersion                 : 11.55
FileName                        : 2013-09-01 20130901122746.MTS
Directory                       : .
FileSize                        : 8.7 MB
FileModifyDate                  : 2013:09:01 12:27:46+02:00
FileAccessDate                  : 2013:09:01 12:27:46+02:00
FileCreateDate                  : 2019:07:15 14:25:57+02:00
FilePermissions                 : rw-rw-rw-
FileType                        : M2TS
FileTypeExtension               : mts
MIMEType                        : video/m2ts
VideoStreamType                 : H.264 Video
AudioStreamType                 : A52/AC-3 Audio
AudioBitrate                    : 256 kbps
SurroundMode                    : Not indicated
AudioChannels                   : 2
ImageWidth                      : 1920
ImageHeight                     : 1080
DateTimeOriginal                : 2013:09:01 12:27:46+02:00 DST
ApertureSetting                 : Auto
Gain                            : 0 dB
ExposureProgram                 : Program AE
WhiteBalance                    : Auto
Focus                           : Auto (0.065)
ImageStabilization              : On (0x3f)
ExposureTime                    : 1/50
FNumber                         : 7.1
Make                            : Sony
Model                           : NEX-7
Warning                         : [minor] The ExtractEmbedded option may find more tags in the video data
AudioSampleRate                 : 48000
Duration                        : 2.88 s
Aperture                        : 7.1
ImageSize                       : 1920x1080
Megapixels                      : 2.1
ShutterSpeed                    : 1/50


For capture timestamp, LR seems to use FileModifyDate and Photos seems to use the import date. Is it possible to use ExifTool to add a tag (like CreateDate) in MTS files and if so can LR/Photos read such tag?
Another option is to re-pack the video (without decoding/encoding) in MP4 format and then I can add the timestamps. Does anybody know any tool can do this losslessly? Thank you in advance!

Phil Harvey

ExifTool can not write M2TS files.  This file type doesn't support any standard type of metadata (only metadata embedded in the video stream).  In fact, it isn't designed as a file type at all (read here).

- 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 ($).

somy

Quote from: Phil Harvey on July 16, 2019, 07:56:55 AM
ExifTool can not write M2TS files.  This file type doesn't support any standard type of metadata (only metadata embedded in the video stream).  In fact, it isn't designed as a file type at all (read here).

- Phil
Hi Phil,

Thanks for your reply!
Sounds like I should convert MTS to MP4 for compatibility. I have found a tool called MkvToMp4 which converts the format losslessly, but it doesn't copy all the tags I need.
So I plan to do in the following steps:
a) Batch convert all MTS files to MP4 format
b) Copy tags from MTS to MP4 files
c) Delete all MTS files

Step a) and c) are trival, but I have some questions regarding step b):
1) I'd like the following information about the camera to be stored in MP4, but I couldn't seem to find corresponding tags in the generated MP4 file:
ApertureSetting                 : Auto
Gain                            : 0 dB
ExposureProgram                 : Program AE
WhiteBalance                    : Auto
Focus                           : Auto (0.065)
ImageStabilization              : On (0x3f)
ExposureTime                    : 1/50
FNumber                         : 7.1
Make                            : Sony
Model                           : NEX-7


2) I find the command "exiftool -TagsFromFile {SOURCE_FILE} "-CreateDate<DateTimeOriginal" {TARGET_FILE}" to copy tags from one file to another. But is there a way that I can recursively copy tags from all MTS files to transcided MP4 files? The source and target will have the same file name with different extensions (.mts and .mp4)

3) In the original MTS file the tag below contains a valid local timestamp. How can I substring to only contain the date and time part (I know in MP4 I should store GMT time but there is no standard tag for time zone)?
DateTimeOriginal: 2013:09:01 12:27:46+02:00 DST

Thanks in advance!

Phil Harvey

I would try something like this:

exiftool -tagsfromfile %d%f.mts -all "-description<ApertureSetting $Aperturesetting, Gain $gain, ExposureProgram $exposureprogram, WhiteBalance $whitebalance, Focus $focus, ImageStabilization $imagestabilization, ExposureTime $exposuretime, FNumber $fnumber, Make $make, Model $model" -ext mp4 DIR

This will copy all same-named tags to their preferred groups, plus write a Description with the tags you mentioned (in case they aren't copied elsewhere).  The DateTimeOriginal should get copied to UserData and XMP without the " DST" on the end.

But note that the Description will only get written if all the mentioned tags exist.  Add -f to write the Description anyway and set missing values to "-".

- 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 ($).

somy

Quote from: Phil Harvey on July 16, 2019, 09:58:49 AM
I would try something like this:

exiftool -tagsfromfile %d%f.mts -all "-description<ApertureSetting $Aperturesetting, Gain $gain, ExposureProgram $exposureprogram, WhiteBalance $whitebalance, Focus $focus, ImageStabilization $imagestabilization, ExposureTime $exposuretime, FNumber $fnumber, Make $make, Model $model" -ext mp4 DIR

This will copy all same-named tags to their preferred groups, plus write a Description with the tags you mentioned (in case they aren't copied elsewhere).  The DateTimeOriginal should get copied to UserData and XMP without the " DST" on the end.

But note that the Description will only get written if all the mentioned tags exist.  Add -f to write the Description anyway and set missing values to "-".

- Phil
Hi Phil,
Appreciated!
I just tried the command and it works great. I don't quite understand what do you mean by "The DateTimeOriginal should get copied to UserData and XMP without the " DST" on the end"? I want the vakue to be copied to "CreateDate" of MP4 in format "2013:09:01 12:27:46" (no +2:00 DST).
And is it possible to add a filter condition for MTS source files such like only MTS with "Model=NEX-7"?



Phil Harvey

Ah, OK.  Add "-createdate<${datetimeoriginal;s/[-+].*//}" to the command then.  But ideally QuickTime dates should be written with a time zone and the -api QuickTimeUTC option should be used.

To only process NEX-7 files, add this:  -if "$model eq 'NEX-7'"

- 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 ($).

somy

Quote from: Phil Harvey on July 16, 2019, 11:03:26 AM
Ah, OK.  Add "-createdate<${datetimeoriginal;s/[-+].*//}" to the command then.  But ideally QuickTime dates should be written with a time zone and the -api QuickTimeUTC option should be used.

To only process NEX-7 files, add this:  -if "$model eq 'NEX-7'"

- Phil

Ya, I understand MP4 wants GMT time but two problems:
1) There is no standard tag to store timezone info - I can see MP4 file from my Sony A7RII camera contains timezone in a non-standard tag, LR ignores it while Photos reads it (Shame on LR). The only thing works both in LR and Photos is MOV files from my iPhone :(
2) I have no idea how to calculate GMT time from ExifTool - say 2013:09:01 12:27:46+02:00 DST, the GMT should be 2013:09:01 10:27:46, can ExifTool calculate this?

Phil Harvey

ExifTool converts to UTC automatically when writing if you use the QuickTimeUTC option. There is no need to store the timezone if the times are stored in UTC.

- 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 ($).

somy

Quote from: Phil Harvey on July 16, 2019, 11:34:04 AM
ExifTool converts to UTC automatically when writing if you use the QuickTimeUTC option. There is no need to store the timezone if the times are stored in UTC.

- Phil
I'll try the API option when I get home. I still want to save the time zone somewhere so I can derive local time. Is it possible?
And when writing to MP4 with additional camera info, can ExifTool log warning message when target tag is not found?
Thanks

Phil Harvey

The original time zone should be stored in the XMP DateTimeOriginal that would be written by this command.

ExifTool will warn on the first tag not found when interpolating a string containing tag names, but adding -f defeats the warning.

- 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 ($).

somy

Quote from: Phil Harvey on July 16, 2019, 11:55:59 AM
The original time zone should be stored in the XMP DateTimeOriginal that would be written by this command.

ExifTool will warn on the first tag not found when interpolating a string containing tag names, but adding -f defeats the warning.

- Phil
Hi Phil,
Thanks for the help!
I have tried to put all bits together and it seems to work to some extend, but I also have a few issues. The command I use is as below:
exiftool -tagsfromfile %d%f.mts  -all -r -P "-description<ApertureSetting $Aperturesetting, Gain $gain, ExposureProgram $exposureprogram, WhiteBalance $whitebalance, Focus $focus, ImageStabilization $imagestabilization, ExposureTime $exposuretime, FNumber $fnumber, Make $make, Model $model, Aperture $aperture, ShutterSpeed $shutterSpeed" "-createdate<$datetimeoriginal" -ext mp4 -api QuickTimeUTC=1 .
And the result MP4 file with the new tags:
Description                     : ApertureSetting Auto, Gain 0 dB, ExposureProgram Program AE, WhiteBalance Auto, Focus Auto (0.065), ImageStabilization On (0x3f), ExposureTime 1/50, FNumber 7.1, Make Sony, Model NEX-7, Aperture 7.1, ShutterSpeed 1/50
DateTimeOriginal                : 2013:09:01 12:27:46+02:00
ExposureProgram                 : Program AE
ExposureTime                    : 1/50
FNumber                         : 7.1
WhiteBalance                    : Auto
Make                            : Sony
Model                           : NEX-7
CreateDate                      : 2013:09:01 12:27:46+02:00
Aperture                        : 7.1
ShutterSpeed                    : 1/50


There are a few issues:
1) The filter condition -if "$model eq 'NEX-7'" doesn't seem to work - I suspect it filters the target MP4 file (with no model info written yet), is there a way to specify the filter condition for MTS source file?
2) It creates a new tag "DateTimeOriginal", and the createDate still contains time zone even though I set the flag "-api QuickTimeUTC=1", maybe I'm doing something wrong here?


Phil Harvey

Quote from: somy on July 17, 2019, 04:30:49 AM
1) The filter condition -if "$model eq 'NEX-7'" doesn't seem to work - I suspect it filters the target MP4 file (with no model info written yet), is there a way to specify the filter condition for MTS source file?

Right.  Unfortunately there is no easy way to filter based on the -tagsFromFile file.

Quote2) It creates a new tag "DateTimeOriginal", and the createDate still contains time zone even though I set the flag "-api QuickTimeUTC=1", maybe I'm doing something wrong here?

ExifTool will extract CreateDate in the local time zone when you read it back with QuickTimeUTC.  But it is stored as UTC.

- 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 ($).

somy

Quote from: Phil Harvey on July 17, 2019, 07:55:34 AM
Quote from: somy on July 17, 2019, 04:30:49 AM
1) The filter condition -if "$model eq 'NEX-7'" doesn't seem to work - I suspect it filters the target MP4 file (with no model info written yet), is there a way to specify the filter condition for MTS source file?

Right.  Unfortunately there is no easy way to filter based on the -tagsFromFile file.

Quote2) It creates a new tag "DateTimeOriginal", and the createDate still contains time zone even though I set the flag "-api QuickTimeUTC=1", maybe I'm doing something wrong here?

ExifTool will extract CreateDate in the local time zone when you read it back with QuickTimeUTC.  But it is stored as UTC.

- Phil

Hi Phil,
Regarding 2) I'm confused.
I thought DateTimeOriginal should be the local time with time zone and CreateDate should be UTC time without time zone.
In my example with the DateTimeOriginal=2013:09:01 12:27:46+02:00 DST (in original MTS file) I'd like to write the following values:

  • DateTimeOriginal=2013:09:01 12:27:46+02:00
  • CreateDate=2013:09:01 10:27:46
Is it possible?

Phil Harvey

> exiftool tmp/a.mp4 -DateTimeOriginal="2013:09:01 12:27:46+02:00" -CreateDate="2013:09:01 10:27:46"
    1 image files updated
> exiftool tmp/a.mp4 -DateTimeOriginal -CreateDate -G1 -a
[UserData]      Date Time Original              : 2013:09:01 12:27:46+02:00
[XMP-exif]      Date/Time Original              : 2013:09:01 12:27:46+02:00
[QuickTime]     Create Date                     : 2013:09:01 10:27:46
[XMP-xmp]       Create Date                     : 2013:09:01 10:27:46
...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 ($).

somy

Quote from: Phil Harvey on July 17, 2019, 08:35:53 AM
> exiftool tmp/a.mp4 -DateTimeOriginal="2013:09:01 12:27:46+02:00" -CreateDate="2013:09:01 10:27:46"
    1 image files updated
> exiftool tmp/a.mp4 -DateTimeOriginal -CreateDate -G1 -a
[UserData]      Date Time Original              : 2013:09:01 12:27:46+02:00
[XMP-exif]      Date/Time Original              : 2013:09:01 12:27:46+02:00
[QuickTime]     Create Date                     : 2013:09:01 10:27:46
[XMP-xmp]       Create Date                     : 2013:09:01 10:27:46


Got it! Thanks a lot!