1

I have an actioncam that saves my video in a folder into the SD Card. Using linux, here is the path:

/media/mattiapdo/EOS_DIGITAL/_REC/100MEDIA

Files are saved in the REC_0001.AVI format

I would write a script that renames each file using the writing date.

Furthermore I notice that for some strange reason, the date and the hour are different from the effective: for example, 12/07/2017 10:30 is written as 09/02/2011 07:55 As the camera is very old and minimal, I can't reset the correct date and the correct hour so I would prefer to manipulate them in aftermath.

The goal would be to rename REC_0001.AVI in 2017_07_12__10_30.AVI

Does anyone have any ideas?

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
Mattia Podio
  • 71
  • 1
  • 4
  • as pointed out by @user5250644 -- see https://stackoverflow.com/questions/23733669/rename-file-command-in-unix-with-timestamp – Fredrik Pihl Jul 12 '17 at 10:44
  • Usually, you can't trust file creation date to determine when a video has been shot. You need to extract relevant metadata from video header (see, e.g., [exiftool](https://en.wikipedia.org/wiki/ExifTool)). – mouviciel Jul 12 '17 at 10:58
  • 1
    @FredrikPihl The linked question refers to the use of the **current** date, while the OP is asking for converting an inaccurate date of recording to a correct one and the use of the **corrected date of recording** to rename their file. – gboffi Jul 12 '17 at 12:22

1 Answers1

0

You can use the date command to print the elapsed seconds since, Unix lingo, the Epoch, aka 1970-01-01 UTC. Assuming that the camera date is in Anglo format, and that by default date likes the Anglo format, you have to swap the month and day in your date

$ date --date='09/02/2011 07:55' +%s
1314942900
$ date --date='07/12/2017 10:30' +%s
1499848200
$ 

so that you can compute a Delta between the real date and the camera idea of time

$ Delta=$(($(date --date='07/12/2017 10:30' +%s)-$(date --date='02/09/2011 07:55' +%s)))
$ echo $Delta
184905300
$

You haven't (yet?) told us how you fetch the date from the camera, but let's say that

$ camera=$(fetch_date $current_file_name)

and assuming that $camera is in a format that date likes,

$ fromEpoch=$(($(date --date="$camera" +%s)+$Delta))

the last step is to get back the date in a format that you like , I suggest the ISO 8601 format, so that your files are correctly sorted by ls

$ corrected_date=$(date --date="@$fromEpoch" +%Y-%m-%dT%H:%M)
$ cp $current_file_name other_directory/$corrected_date.AVI

The boring details about the date command, that is indeed VERY flexible and useful, are available using

$ man date

I hope that you can write your script with the info that I gave you, thank you for the question.


Addendum

Caveat emptor: totally untested

$ cat script
Delta=$(($(date --date='07/12/2017 10:30' +%s)-$(date --date='02/09/2011 07:55' +%s)))

mkdir -p ATTIC
mv *AVI ATTIC
for file in ./ATTIC/*.AVI ;  do
    ########## fetch_date command is a placeholder for the real command
    cam_date=$(fetch_date "$file")
    cam_fromEpoch=$(date --date="$cam_date" +%s)
    correct_fromEpoch=$(($cam_fromEpoch+$Delta))
    ISO_8601=$(date --date="@$correct_fromEpoch" +%Y-%m-%dT%H:%M)
    cp $file $ISO_8601.AVI
done
# cleanup, e.g. list current directory and ATTIC and ask if ATTIC is to be removed
$
gboffi
  • 22,939
  • 8
  • 54
  • 85