6

After restoring my whatsapp backup I encountered the problem, that the image creation date is set to the current date. This resulted in a mess in the android library.

Mr.Sheep
  • 1,368
  • 1
  • 15
  • 32

3 Answers3

3

Based on the file name here is a parser to set the creation date for the image and video files as well. Source code is written in python.

Credits for the first part goes to Joachim Holwech. Thanks.

from datetime import datetime
import piexif

import os
import time

folder = './'

def get_datetime(filename):
    date_str = filename.split('-')[1]
    return datetime.strptime(date_str, '%Y%m%d')

def get_date(filename):
    date_str = filename.split('-')[1]
    return datetime.strptime(date_str, '%Y%m%d').strftime("%Y:%m:%d %H:%M:%S")


allowedFileEndings = ['mp4','jpg','3gp','jpeg']

filenames = [fn for fn in os.listdir(folder) if fn.split('.')[-1] in allowedFileEndings]

l = len(filenames)
print(l)

for i, filename in enumerate(filenames):

    if filename.endswith('mp4') or filename.endswith('3gp'):
        date = get_datetime(filename)
        modTime = time.mktime(date.timetuple())
        os.utime(folder + filename, (modTime, modTime))

    elif filename.endswith('jpg') or filename.endswith('jpeg'):
        exif_dict = {'Exif': {piexif.ExifIFD.DateTimeOriginal: get_date(filename)}}
        exif_bytes = piexif.dump(exif_dict)
        piexif.insert(exif_bytes, folder + filename)

    print('{}: {}/{}'.format(filename, i + 1, l))
print('\nDone!')
``

Src.: https://holwech.github.io//blog/Fixing-WhatsApp-Backup/
Mr.Sheep
  • 1,368
  • 1
  • 15
  • 32
3

I connected the smartphone (Android 8.1) to a windows 10 laptop, device was recognized (with Mac it does not).
I went to the directory containing misdated Whatsapp photos and download them to my Mac (via network, flash drive, etc)
Filenames are like IMG-20190128-WA0011.jpg.
In Terminal (Mac) I simply ran

exiftool "-alldates<filename" /path/to/dir

To finally reverse the copy procedure and overwrote misdated files.

exiftool is a nice metadata tool. It can be used in all most common operation systems.

dstonek
  • 945
  • 1
  • 20
  • 33
0

This project is much more solid and offers also a duplicate finder.

https://github.com/ikaruswill/whatsapp-media-tools

  • Install Python 3
  • Simply clone the project (or download zip)
  • Turn wifi & mobile data off on your phone
  • Copy "WhatsApp Images" & "WhatsApp Video" from %device%\Android\media\com.whatsapp\WhatsApp\Media to a local folder

Execute the following in a terminal / cmd / powershell (from within the project source folder)

pip install piexif # must be done once
python restore-exif.py -r "local folder"

(on windows, put the path in " if you have spaces in the path) The -r flag will ensure that both folders, wich are in your local folder are processed, including the "Sent" folders.

Once everything is processed, simply check your files and copy (and overwrite) the files on your phone. In my case, this did not worked and all copy actions were aborted. Also on windows the created / modified dates did not copy over (don't even ask, it's windows...). Solution: zip the folders and copy the zip on your phone. Than use the android file browser, go the the zip and extract it. Then rename (or delete) the old folders and rename / move the new folders to the correct location and then hopefully everything should be alright.

The reason to turn off wifi / mobile data is that there is no conflict while doing this. Just a precaution step.


This helped me to restore proper dates (kind of surprised that a giant company is not able do do this properly and the community has to step in :D)

Delta
  • 721
  • 8
  • 5