"Fixing Timestamp in Pictures"
As per my routine, I downloaded a bunch of podcast on to my Android phone in the morning. Upon playback of the first podcast, after 5 mins, the audio from another podcast cuts in. This happens on other recently downloaded podcasts. After some troubleshooting I concluded that my 128Gb microsd card was having write issues. This was a Samsung microsd which I thought would have a longer life than the sandisk it replaced.
I did a backup of the contents of the microsd card but I knew it will change the time stamp of the picture files in the DCIM folder. The gallery app will no longer show the photos in chronological order. My solution was to reset the time stamp of the photos using the time/date information stored in the exif metadata in the photos. So after restoring all the data from the failing microsd card to a new microsd card, the next step was to find a script/application to reset the timestamp of the photos.
I found this blog entry with the script that will do the job.
Unfortunately the python script was for python2. I modified it to the python3 syntax so it would run on my archlinux system. Modified script photodate.py as follows:
{{< highlight python >}}
!/usr/bin/env python
import os, re, time, argparse, exifread
def date_correction(args):
'''Method for Android date correction of /sdcard/DCIM/Camera'''
for dirpath, dirnames, filenames in os.walk(args.directory):
for filename in [f for f in filenames if f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.mp4') or f.endswith('.3gp') or f.endswith('.JPG')]:
fn = os.path.join(dirpath, filename)
f = open(fn, 'rb')
tags = exifread.process_file(f)
if 'EXIF DateTimeOriginal' in tags.keys():
ts = str(tags['EXIF DateTimeOriginal'])
else:
ts = None
if ts is not None:
print ('Found date in EXIF metadata: ' + fn + ' (' + ts + ')')
ft = time.mktime(time.strptime(ts, '%Y:%m:%d %H:%M:%S'))
os.utime(fn, (ft, ft))
else:
match = re.search(r'((?P
def main(): try: print ('\033[92m' + '\nAndroid Photo Date Correction\n' + '\033[0m') parser = argparse.ArgumentParser() required = parser.add_argument_group('required arguments') required.add_argument('-d', '--directory', metavar='[directory]', required=True, help='Photo source directory') args = parser.parse_args() date_correction(args) except ImportError as e: print ('There must be something missing\n') print (e.args[0])
if name == "main": main()
{{< /highlight >}}
As per instructions in the blogpost, I ran sshdroid as the ssh server on the phone. I used sshfs to mount the phone folder with pictures onto a mountpoint on my system. I run the script and processed thousands of photos.
There were bunch of photo which did not have the time/date information in the exif. However the filename of the photos had the date information so I wrote a script to use the filename to set the timestamp. For example, this is the script to fix a photo with this filename structure 2011-05-02 20.08.07.jpg.
fix_timestamp.sh
{{< highlight bash >}}
IFS=$'\n'; for i in $(ls *.jpg) ; do touch --date $(echo $i | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' | tr -d '-' ) $i; done
{{< /highlight >}}
The change in the IFS variable is to fix the issue of the space in the photo filename.
Used similar scripts to correct the timestamp of videos as they also have the date in the filename.
So now the photos/videos will display correctly on the phone gallery app.