Copying timestamp from one file to another

Discussion in 'macOS' started by andrewp, Jan 19, 2013.

  1. andrewp macrumors member

    Joined:
    Oct 24, 2008
    #1
    Heya,

    I just converted 400+ of my videoes from MTS to mp4. However all the timestamps on the new files indicate that the videos were created today. So what I want is to copy the timestamps from the old files and apply them to the new ones.
    I have read quite a few posts about this and it shouldn't be too difficult but I could not find a working mac solution so far.
     
  2. andrewp thread starter macrumors member

    Joined:
    Oct 24, 2008
    #2
    or least does anybody know a video converter that preserver the original date of the videos?
     
  3. kevink2 macrumors 65816

    Joined:
    Nov 2, 2008
    #3
    Are you wanting to set the timestamp in some field WITHIN the video? Or just the timestamp you see in finder folder?

    For the actual file, the touch command from the terminal app will work.
     
  4. charlieegan3 macrumors 68020

    charlieegan3

    Joined:
    Feb 16, 2012
    Location:
    U.K
    #4
  5. switon, Jan 19, 2013
    Last edited: Jan 19, 2013

    switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #5
    RE: touch, stat,...

    Hi andrewp,

    If you wish to use some bash commands, then the following commands will extract the date from one file and apply it to another file:

    Code:
    timestamp=`stat -t "%c%m%d%H%M.%S" testdate1 | cut -d : -f3- | cut -d " " -f2- | cut -d . -f1`
    touch -t $timestamp testdate2
    
    The "stat" command gets all of the various times associated with the file "testdate1". The three "cut" commands then extract one particular date in the format required for the "touch" command. If you wish to select a different date, then you would change the first "cut" to select whichever date you want. The "touch" command then uses the date from the "testdate1" file and resets the modification and access times of file "testdate2" to those of "testdate1".

    Below is an example execution of these commands (the commands are on the lines beginning with a "pound sign" #, the output is on the following lines):

    Code:
    # stat testdate1
    16777218 16291081 -rw-r--r-- 1 username staff 0 0 "Jan 14 05:52:02 2013" "Jan 14 05:52:02 2013" "Jan 19 08:14:29 2013" "Jan 14 05:52:02 2013" 4096 0 0 testdate1
    # stat testdate2
    16777218 16291082 -rw-r--r-- 1 username staff 0 0 "Jan 19 08:40:56 2013" "Jan 19 08:40:56 2013" "Jan 19 08:40:56 2013" "Jan 15 03:14:15 2010" 4096 0 0 testdate2
    # timestamp=`stat -t "%c%m%d%H%M.%S" testdate1 | cut -d : -f3- | cut -d " " -f2- | cut -d . -f1`
    # echo $timestamp
    201301140552
    # touch -t $timestamp testdate2
    # stat testdate2
    16777218 16291082 -rw-r--r-- 1 username staff 0 0 "Jan 14 05:52:00 2013" "Jan 14 05:52:00 2013" "Jan 19 08:42:23 2013" "Jan 15 03:14:15 2010" 4096 0 0 testdate2
    
    As you see from the above output, the stat testdate1 returns a modification date of Jan 14 while the initial stat testdate2 returns Jan 19. The timestamp variable is set to 201301140552 (Jan 14, 2013 05:52) and the touch changes the modification and access times to this date. The final stat testdate2 shows that its modification time has been reset to the earlier date.


    Regards,
    Switon
     
  6. andrewp thread starter macrumors member

    Joined:
    Oct 24, 2008
    #6
    Thanks for the replies guys!
    -Switon, is that code for one file at the time or can it convert all the 400 files in one process?
     
  7. kevink2 macrumors 65816

    Joined:
    Nov 2, 2008
    #7
    The code as written is for 1 file. But a for loop can easily be written to support wildcards and do the command on each file individually, assuming of course the files have the same name other than extension.

    If the names don't easily correlate, then it is harder.
     
  8. switon macrumors 6502a

    Joined:
    Sep 10, 2012
    #8
    RE: one file...


    Hi andrewp,

    Yes, the code I included runs on just one file, as I didn't know what your video filenames looked like. This code is meant as an executable example of what your actual code would look like. In other words, this is the "guts" of a shell script that would readjust the timestamps of your mp4 videos that you are interested in doing. This code, with a surrounding FOR statement whose variable is used for the filenames plus a little extra coding to generate the proper mp4 output filename, would perform the reset of the modification dates on all 400+ of your videos from a simple script with only about four lines of bash code.

    I thank kevink2 for pointing this out before me, as I've been busy and haven't been able to reply as often as I should.

    Regards,
    Switon
     
  9. andrewp thread starter macrumors member

    Joined:
    Oct 24, 2008
    #9
    Thanks a lot guys it makes good sense but I still struggle executing the command.
    I created a .sh file with the lines Switon provided, but I am not sure how I should treat the "testdate" files. I renamed them to bear the same name as the video files, i.e 00216.MTS->00216.mp4, but this give me an error message:

    touch: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]

    I also tried to specify the location (/user/name/video/00216.MTS) but I still get the same error.

    What am I missing?
     

Share This Page