简体   繁体   English

Bash linux,JPG图像的小时,分​​钟和秒

[英]Bash linux, Hour, Minutes and Seconds of a JPG Image

I am trying to get the complete date of an image in JPG format. 我正在尝试获取JPG格式图像的完整日期。 The format I want to get is 14:25:38 (Hour:Minutes:Seconds). 我想要的格式是14:25:38 (小时:分钟:秒)。

I tried with the commands: 我尝试了以下命令:

$ stat -c %y DSC_0002.JPG | sed 's/^\([0-9\-]*\).*/\1/'
=> 2017-05-19  -Not that way

$ file DSC_0002.JPG
=> DSC_0002.JPG: JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=11, manufacturer=NIKON CORPORATION, model=NIKON D5200, orientation=upper-left, xresolution=180, yresolution=188, resolutionunit=2, software=Ver.1.01 , datetime=2017:05:19 13:30:34, GPS-Data], baseline, precision 8, 6000x4000, frames 3

This last ( file DSC_0002.JPG ) command displays datetime=2017:05:19 13:30:34 , but I need to get only 13:30:34 最后一个( file DSC_0002.JPG )命令显示datetime=2017:05:19 13:30:34 ,但是我只需要获取13:30:34

Preferably without using add-ons or programs external to Linux bash. 最好不要使用Linux bash外部的附件或程序。

Thank you very much for any help. 非常感谢您的帮助。

My 2 cents... 我的2分钱

As exif is a big part of my job and because I already spent a lot of time building my own scripts, time is comming to make a little bench. 由于exif是我工作的重要组成部分,并且因为我已经花了很多时间来构建自己的脚本,所以时间来了,腾出了一些空间。

Getting result of jhead using : 使用获取jhead结果:

This use jhead because it's my prefered. 使用jhead是因为它是我的首选。 See further to know why... 进一步了解为什么...

DateTime=$(jhead -nofinfo DSC_0002.JPG)
DateTime=${DateTime#*Date/Time*: }
DateTime=${DateTime%%$'\n'*}

This is the quickest way, (a lot quicker than using bash regex)! 这是最快的方法(比使用bash regex快得多)!

echo $DateTime 
2011:02:27 14:53:32

Different ways 不同的方式

Inquiring a jpeg file for knowing his date is possible by using at least 4 different tools: 使用至少4种不同的工具可以查询jpeg文件以了解其日期:

  1. file The magic file recognition command make some light inquiring on any file for determining is nature and print some more informations. file 神奇的 file识别命令使您可以轻而易举地查询任何文件来确定其性质,并打印更多信息。

     file DSC_0002.JPG DSC_0002.JPG: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=10, manufacturer=CANIKON, model=CANIKON AB12, orientation=upper- left, xresolution=168, yresolution=176, resolutionunit=2, software=Ver.1.00 , datetime=2011:02:27 14:53:32], baseline, precision 8, 3872x2592, frames 3 

    file print size, resolution and dateTime from a jpeg file. jpeg文件中的file打印大小,分辨率和dateTime。

  2. jhead is a jpeg's header dedicated tool: jheadjpeg标头的专用工具:

     jhead DSC_0002.JPG File name : DSC_0002.JPG File size : 4940925 bytes File date : 2011:02:27 14:53:32 Camera make : CANIKON Camera model : CANIKON AB12 Date/Time : 2011:02:27 14:53:32 Resolution : 3872 x 2592 Flash used : No Focal length : 55.0mm (35mm equivalent: 82mm) Exposure time: 0.0080 s (1/125) Aperture : f/5.6 ISO equiv. : 180 Whitebalance : Auto Metering Mode: pattern JPEG Quality : 97 
  3. identify is part of ImageMagick package wich is a kind of all purpose tool for bitmap images... (Due to some security bugs in past and overall perfomance, I'ts not my personal choice). identifyImageMagick程序包的一部分,但它是一种用于位图图像的通用工具...(由于过去存在一些安全性漏洞以及整体性能,我不是我个人的选择)。

     identify DSC_0002.JPG DSC_0002.JPG JPEG 3872x2592 3872x2592+0+0 8-bit sRGB 4.941MB 0.010u 0:00.020 identify -format "%[EXIF:DateTime]\\n" DSC_0002.JPG 2011:02:27 14:53:32 
  4. exiftool is a dedicated tool using libimage . exiftool是使用libimage的专用工具。

     exiftool DSC_0002.JPG ExifTool Version Number : 9.74 File Name : DSC_0002.JPG Directory : . File Size : 4.7 MB File Modification Date/Time : 2011:02:27 14:53:32+01:00 File Access Date/Time : 2017:06:05 08:40:26+02:00 File Inode Change Date/Time : 2017:06:05 08:40:04+02:00 File Permissions : rw-r--r-- File Type : JPEG MIME Type : image/jpeg Exif Byte Order : Big-endian (Motorola, MM) ... Modify Date : 2011:02:27 14:53:32 ... Thumbnail Image : (Binary data 8965 bytes, use -b option to extract) Circle Of Confusion : 0.020 mm Depth Of Field : 15.07 m (8.58 - 23.65) Field Of View : 24.7 deg (5.50 m) Focal Length : 55.0 mm (35 mm equivalent: 82.0 mm) Hyperfocal Distance : 26.80 m Light Value : 11.1 

    The default output of this is 185 lines length, I've dropped a lot. 默认输出是185行,我掉了很多。

  5. pure bash As requested and because Toby Speight recommend to not doing this : bash根据要求,并且因为Toby Speight建议不要这样做

     FDate=;while IFS= read -d $'\\0' -n 1024 raw;do [ "$raw" ] && \\ [ -z "${raw#[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]}" ] && FDate=$raw && break done <DSC_0002.JPG echo $FDate 2011:02:27 14:53:32 

    Ok, this a far away a perfect function: first date found in file is considered, regardless of fieldname. 好的,这是一个很完美的功能:无论文件名如何,都将考虑在文件中找到的第一个日期。

Little bench 小板凳

As the goal is to extract date time part of exif infos, the bench would only do this. 由于目标是提取exif信息的日期时间部分,因此工作人员只会这样做。

time for i in {1..100};do ... done >/dev/null
export TIMEFORMAT="R:%4R u:%4U s:%4S"

Like this (from quicker to slower): 像这样(从快到慢):

# quicker: jhead
time for i in {1..100};do jhead DSC_0002.JPG ;done >/dev/null
R:0.115  u:0.000  s:0.028

# 2nd: file
time for i in {1..100};do file DSC_0002.JPG; done >/dev/null
R:0.226  u:0.000  s:0.044

# 3nd: pure bash
time for i in {1..100};do 
    while IFS= read -d  $'\0' -n 1024 raw ;do
        [ "$raw" ] && 
        [ -z "${raw#[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]}" ] &&
         ftim=$raw && break
      done <DSC_0002.JPG
   done >/dev/null
R:0.393  u:0.380  s:0.012

# 4nd: best dedicated: exiftool
time for i in {1..100};do exiftool -time:CreateDate DSC_0002.JPG ;done >/dev/null
R:14.921  u:13.064  s:0.956

# slower: imagemagick's identify
time for i in {1..100};do identify -format "%[EXIF:DateTime]\n" DSC_0002.JPG ;done >/dev/null
R:21.609  u:15.712  s:5.060

Sorry, I agree with Toby Speight : Doing this in pure bash is a not so good idea! 抱歉,我同意Toby Speight的观点:用纯bash这样做不是一个好主意!

Setting file date 设定档案日期

Doing date manipulation is very easy under pure bash, but working on file date. 在纯bash下执行日期操作非常容易,但是可以处理文件日期。 For this, I asked my system to set all photos files datetime to creation date found in exif. 为此,我要求系统将所有照片文件的日期时间设置为exif中的创建日期。

For this, exiftool offer a specific syntax: 为此, exiftool提供了一种特定的语法:

exiftool '-FileModifyDate<DateTimeOriginal /path/to/myphotos

and jhead too: 还有jhead

jhead -q -ft /path/to/myphotos/*.JPG

This will set all creation file date based on creation date in exif infos. 这将根据exif信息中的创建日期设置所有创建文件的日期。 Once done, you could use standard tools for filesystem inquiry: 完成后,您可以使用标准工具进行文件系统查询:

ls -l DSC_0002.JPG
stat DSC_0002.JPG

find /path/to/myphotos -type f -name 'DSC*JPG' -mtime +400 -mtime -410 -ls

And so on... 等等...

You can use cut command to parse output. 您可以使用cut命令来解析输出。

In your case stat gives output 在你的情况下stat给出输出

2017-05-05 06:12:37.228033281 -0500 2017-05-05 06:12:37.228033281 -0500

So to get desire output you can use stat -c %y popen.c | cut -f2 -d' ' | cut -f1 -d'.' 因此,要获得期望输出,可以使用stat -c %y popen.c | cut -f2 -d' ' | cut -f1 -d'.' stat -c %y popen.c | cut -f2 -d' ' | cut -f1 -d'.'

Refer: man cut 参考:

Not all images have metadata, but those who have them can get them in the following ways: 并非所有图像都有元数据,但是拥有它们的人可以通过以下方式获取它们:

identify -format "%[EXIF:DateTime]\n" image.jpg | awk '{print $2}

As said Toby Speight: 正如Toby Speight所说:

exiftool -time:CreateDate -a -G0:1 -s image.jpg | awk '{print $5}'

and

jhead image.jpg | awk '/^Date\/Time/{print $4}'

I'm sure there are other options, but I did not practice them 我确定还有其他选择,但是我没有练习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM