Skip to main content

How can I restore file timestamps of video recordings using the embedded metadata? [Resolved]

I'm looking for a command that will read the metadata of a ‘*.mp4’ file and touch the file's timestamp with the creation time of the video. The command should be read-only with respect to the file contents, i.e. hashing the file before and after should yield the same result.

Situation: I made the mistake of moving pictures and videos from the internal storage of my phone to an SD card using Android's stock tool to do so (‘Settings’ ? ‘Storage’ ? ‘Transfer data to SD card’). Unfortunately, this bumped the timestamps of all of the files, and also messed around with directories other than DCIM/ (pictures and videos were also moved from the Download/, image/, Pictures/, and video/ directories). In hindsight it seems silly that I used such a tool (I'd normally use Ghost Commander), but it appeared as a notification when running low on space and it just looked so easy. I'm now trying to piece back together the timestamps of various pictures and videos.

I'm effectively looking for a video equivalent to the following command, which I used to sort out the images in DCIM/100ANDRO/:

exiv2 -T mv *.JPG

I skimmed through some FFmpeg documentation, but I see no mention of read-only commands or printing timestamps.

(Unfortunately for my situation, this approach does not offer a solution for files I didn't create, such as those in Download/, for which I want to appear in the media collection in chronological order of when I obtained them.)

Question Credit: James Haigh
Question Reference
Asked April 14, 2019
Posted Under: Unix Linux
4 Answers

You can use MediaInfo for that:

$ mediainfo | grep 'Recorded date'
Recorded date                            : 2014-2-23T09:00:00Z

Getting from that date format to a touch command should be a small matter of programming. Personally, I'd use Perl's Date::Manip module for this. It can almost cope with the above format; it requires 2-digit months with zero padding to understand this particular date format. Fixing that only requires a trivial regexp, which is of course easy in Perl.

$ perl -M'Date::Manip' -e 'print ParseDate("2014-02-23T09:00:00Z")'

The fact that it prints shows that it's parsing. If you drop the 0, you'll see that it doesn't print anything, because ParseDate() returns undef.

credit: Warren Young
Answered April 14, 2019

Follow James' advice above for MP4 and most QuickTime files. For AVI files, mediainfo will output Mastered date but the format is mostly un-parseable. Install ffmpeg to get ffprobe (also known as avprobe) then use:

for file in *.avi; do touch -t "$(ffprobe "$file" 2>&1 | grep -m 1 'creation_time' | sed -r 's/.*([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}).*/\1\2\3\4\5.\6/')" "$file"; done

Note the mod before piping to grep. This is because ffprobe uses stderr not stdout.

credit: Glenn Batuyong
Answered April 14, 2019


exiftool "-CreateDate>FileModifyDate" FILES or FOLDERS

The name of the value you want may differ according to file format and other factors. Use below to print them:

exiftool -time:all -s FILE

credit: Gringo Suave
Answered April 14, 2019
Your Answer