def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords(path) elif mime == HC.AUDIO_MP3: duration = HydrusAudioHandling.GetMP3Duration(path) elif mime == HC.AUDIO_OGG: duration = HydrusAudioHandling.GetOGGVorbisDuration(path) elif mime == HC.AUDIO_FLAC: duration = HydrusAudioHandling.GetFLACDuration(path) elif mime == HC.AUDIO_WMA: duration = HydrusAudioHandling.GetWMADuration(path) return (size, mime, width, height, duration, num_frames, num_words)
def GenerateThumbnail( path, dimensions = HC.UNSCALED_THUMBNAIL_DIMENSIONS ): mime = GetMime( path ) f = cStringIO.StringIO() if mime in HC.IMAGES: pil_image = HydrusImageHandling.GeneratePILImage( path ) SaveThumbnailToStream( pil_image, dimensions, f ) elif mime == HC.APPLICATION_FLASH: ( os_file_handle, temp_path ) = HydrusPaths.GetTempPath() try: HydrusFlashHandling.RenderPageToFile( path, temp_path, 1 ) pil_image = HydrusImageHandling.GeneratePILImage( temp_path ) SaveThumbnailToStream( pil_image, dimensions, f ) except: flash_default_path = os.path.join( HC.STATIC_DIR, 'flash.png' ) pil_image = HydrusImageHandling.GeneratePILImage( flash_default_path ) SaveThumbnailToStream( pil_image, dimensions, f ) finally: del pil_image HydrusPaths.CleanUpTempPath( os_file_handle, temp_path ) else: ( size, mime, width, height, duration, num_frames, num_words ) = GetFileInfo( path ) cropped_dimensions = HydrusImageHandling.GetThumbnailResolution( ( width, height ), dimensions ) renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, cropped_dimensions ) numpy_image = renderer.read_frame() if numpy_image is None: raise Exception( 'Could not create a thumbnail from that video!' ) pil_image = HydrusImageHandling.GeneratePILImageFromNumpyImage( numpy_image ) SaveThumbnailToStream( pil_image, dimensions, f ) f.seek( 0 ) thumbnail = f.read() f.close() return thumbnail
def GenerateThumbnail(path, mime, dimensions=HC.UNSCALED_THUMBNAIL_DIMENSIONS, percentage_in=35): if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): thumbnail = GenerateThumbnailFromStaticImage(path, dimensions, mime) else: f = cStringIO.StringIO() if mime == HC.APPLICATION_FLASH: (os_file_handle, temp_path) = HydrusPaths.GetTempPath() try: HydrusFlashHandling.RenderPageToFile(path, temp_path, 1) pil_image = HydrusImageHandling.GeneratePILImage(temp_path) SaveThumbnailToStreamPIL(pil_image, dimensions, f) except: flash_default_path = os.path.join(HC.STATIC_DIR, 'flash.png') pil_image = HydrusImageHandling.GeneratePILImage( flash_default_path) SaveThumbnailToStreamPIL(pil_image, dimensions, f) finally: del pil_image HydrusPaths.CleanUpTempPath(os_file_handle, temp_path) else: (size, mime, width, height, duration, num_frames, num_words) = GetFileInfo(path) cropped_dimensions = HydrusImageHandling.GetThumbnailResolution( (width, height), dimensions) renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, cropped_dimensions) renderer.read_frame( ) # this initialises the renderer and loads the first frame as a fallback desired_thumb_frame = int((percentage_in / 100.0) * num_frames) renderer.set_position(desired_thumb_frame) numpy_image = renderer.read_frame() if numpy_image is None: raise Exception( 'Could not create a thumbnail from that video!') pil_image = HydrusImageHandling.GeneratePILImageFromNumpyImage( numpy_image) SaveThumbnailToStreamPIL(pil_image, dimensions, f) f.seek(0) thumbnail = f.read() f.close() return thumbnail
def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords(path) elif mime in HC.AUDIO: ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path) duration_in_s = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines) duration = int(duration_in_s * 1000) if width is not None and width < 0: width *= -1 if height is not None and height < 0: width *= -1 if duration is not None and duration < 0: duration *= -1 if num_frames is not None and num_frames < 0: num_frames *= -1 if num_words is not None and num_words < 0: num_words *= -1 return (size, mime, width, height, duration, num_frames, num_words)
def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: if mime == HC.TEXT_HTML: raise HydrusExceptions.MimeException( 'Looks like HTML -- maybe the client needs to be taught how to parse this?' ) elif mime == HC.APPLICATION_UNKNOWN: raise HydrusExceptions.MimeException('Unknown filetype!') else: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path, mime) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords( path) # this now give None until a better solution can be found elif mime in HC.AUDIO: ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path) duration_in_s = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines) duration = int(duration_in_s * 1000) if width is not None and width < 0: width *= -1 if height is not None and height < 0: width *= -1 if duration is not None and duration < 0: duration *= -1 if num_frames is not None and num_frames < 0: num_frames *= -1 if num_words is not None and num_words < 0: num_words *= -1 return (size, mime, width, height, duration, num_frames, num_words)