gh-2 Write time to EXIF for videos if only available from the file properties (modified/access)
This commit is contained in:
		
							parent
							
								
									a7aee65ca0
								
							
						
					
					
						commit
						67a47db73d
					
				@ -87,23 +87,32 @@ class Video(Media):
 | 
				
			|||||||
            return None
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        source = self.source
 | 
					        source = self.source
 | 
				
			||||||
        seconds_since_epoch = min(os.path.getmtime(source), os.path.getctime(source))
 | 
					 | 
				
			||||||
        # We need to parse a string from EXIF into a timestamp.
 | 
					        # We need to parse a string from EXIF into a timestamp.
 | 
				
			||||||
        # we use date.strptime -> .timetuple -> time.mktime to do the conversion in the local timezone
 | 
					        # We use date.strptime -> .timetuple -> time.mktime to do the conversion in the local timezone
 | 
				
			||||||
 | 
					        # If the time is not found in EXIF we update EXIF
 | 
				
			||||||
 | 
					        seconds_since_epoch = min(os.path.getmtime(source), os.path.getctime(source))
 | 
				
			||||||
 | 
					        time_found_in_exif = False
 | 
				
			||||||
        exif_data = self.get_exif()
 | 
					        exif_data = self.get_exif()
 | 
				
			||||||
        for key in ['Creation Date', 'Media Create Date']:
 | 
					        for key in ['Creation Date', 'Media Create Date']:
 | 
				
			||||||
            date = re.search('%s +: +([0-9: ]+)' % key, exif_data)
 | 
					            date = re.search('%s +: +([0-9: ]+)' % key, exif_data)
 | 
				
			||||||
            if(date is not None):
 | 
					            if(date is not None):
 | 
				
			||||||
                date_string = date.group(1)
 | 
					                date_string = date.group(1)
 | 
				
			||||||
                try:
 | 
					                try:
 | 
				
			||||||
                    seconds_since_epoch = time.mktime(datetime.strptime(date_string, '%Y:%m:%d %H:%M:%S').timetuple())
 | 
					                    exif_seconds_since_epoch = time.mktime(datetime.strptime(date_string, '%Y:%m:%d %H:%M:%S').timetuple())
 | 
				
			||||||
                    break
 | 
					                    if(exif_seconds_since_epoch < seconds_since_epoch):
 | 
				
			||||||
 | 
					                        seconds_since_epoch = exif_seconds_since_epoch
 | 
				
			||||||
 | 
					                        time_found_in_exif = True
 | 
				
			||||||
 | 
					                        break
 | 
				
			||||||
                except:
 | 
					                except:
 | 
				
			||||||
                    pass
 | 
					                    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if(seconds_since_epoch == 0):
 | 
					        if(seconds_since_epoch == 0):
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # If the time wasn't found in EXIF we need to add it there since the modified/access times frequently change
 | 
				
			||||||
 | 
					        if(time_found_in_exif == False):
 | 
				
			||||||
 | 
					            self.set_datetime(datetime.fromtimestamp(seconds_since_epoch))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return time.gmtime(seconds_since_epoch)
 | 
					        return time.gmtime(seconds_since_epoch)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
@ -282,10 +291,11 @@ class Video(Media):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            # We create a temporary file to save the modified file to.
 | 
					            # We create a temporary file to save the modified file to.
 | 
				
			||||||
            # If the modification is successful we will update the existing file.
 | 
					            # If the modification is successful we will update the existing file.
 | 
				
			||||||
            metadata = self.get_metadata()
 | 
					            # We can't call self.get_metadata else we will run into infinite loops
 | 
				
			||||||
 | 
					            # metadata = self.get_metadata()
 | 
				
			||||||
            temp_movie = None
 | 
					            temp_movie = None
 | 
				
			||||||
            with tempfile.NamedTemporaryFile() as temp_file:
 | 
					            with tempfile.NamedTemporaryFile() as temp_file:
 | 
				
			||||||
                temp_movie = '%s.%s' % (temp_file.name, metadata['extension'])
 | 
					                temp_movie = '%s.%s' % (temp_file.name, self.get_extension())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # We need to block until the child process completes.
 | 
					            # We need to block until the child process completes.
 | 
				
			||||||
            # http://stackoverflow.com/a/5631819/1318758
 | 
					            # http://stackoverflow.com/a/5631819/1318758
 | 
				
			||||||
@ -300,7 +310,7 @@ class Video(Media):
 | 
				
			|||||||
            # Before we do anything destructive we confirm that the file is in tact.
 | 
					            # Before we do anything destructive we confirm that the file is in tact.
 | 
				
			||||||
            check_media = Video(temp_movie)
 | 
					            check_media = Video(temp_movie)
 | 
				
			||||||
            check_metadata = check_media.get_metadata()
 | 
					            check_metadata = check_media.get_metadata()
 | 
				
			||||||
            if(check_metadata['latitude'] is None or check_metadata['longitude'] is None or check_metadata['date_taken'] is None):
 | 
					            if(('latitude' in kwargs and 'longitude' in kwargs and check_metadata['latitude'] is None and check_metadata['longitude'] is None) or ('time' in kwargs and check_metadata['date_taken'] is None)):
 | 
				
			||||||
                if(constants.debug == True):
 | 
					                if(constants.debug == True):
 | 
				
			||||||
                    print 'Something went wrong updating video metadata'
 | 
					                    print 'Something went wrong updating video metadata'
 | 
				
			||||||
                return False
 | 
					                return False
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user