Commits

Anonymous committed eecdd27

Metadata RIFF: avoid division per zero in duration computation

  • Participants
  • Parent commits 19d1aab

Comments (0)

Files changed (3)

hachoir-metadata/hachoir_metadata/filter.py

+class Filter:
+    def __init__(self, valid_types, min=None, max=None):
+        self.types = valid_types
+        self.min = min
+        self.max = max
+
+    def __call__(self, value):
+        if not isinstance(value, self.types):
+            return True
+        if self.min is not None and value < self.min:
+            return False
+        if self.max is not None and self.max < value:
+            return False
+        return True
+
+class NumberFilter(Filter):
+    def __init__(self, min=None, max=None):
+        Filter.__init__(self, (int, long, float), min, max)
+

hachoir-metadata/hachoir_metadata/metadata.py

 from hachoir_core.i18n import _
 from hachoir_core.error import warning
 from datetime import datetime
+from hachoir_metadata.filter import Filter, NumberFilter
 
 MAX_STR_LENGTH = 80*10
 MAX_SAMPLE_RATE = 192000
 MIN_YEAR = 1900
 MAX_YEAR = 2030
 MAX_FRAME_RATE = 150
+DATETIME_FILTER = Filter(datetime, datetime(MIN_YEAR, 1, 1), datetime(MAX_YEAR, 12, 31))
 
 extractors = {}
 
-class Filter:
-    def __init__(self, valid_types, min=None, max=None):
-        self.types = valid_types
-        self.min = min
-        self.max = max
-
-    def __call__(self, value):
-        if not isinstance(value, self.types):
-            return True
-        if self.min is not None and value < self.min:
-            return False
-        if self.max is not None and self.max < value:
-            return False
-        return True
-
-class NumberFilter(Filter):
-    def __init__(self, min=None, max=None):
-        Filter.__init__(self, (int, long, float), min, max)
-
-class DatetimeFilter(Filter):
-    def __init__(self, min=None, max=None):
-        Filter.__init__(self, datetime, min, max)
-
 class Data:
     def __init__(self, key, priority, description,  handler=None, filter=None):
         """
 
         self.register("subtitle_author", 400, _("Subtitle author"))
 
-        datetime_filter = DatetimeFilter( datetime(MIN_YEAR, 1, 1), datetime(MAX_YEAR, 12, 31))
         self.register("creation_date", 500, _("Creation date"),
-            filter=datetime_filter)
+            filter=DATETIME_FILTER)
         self.register("last_modification", 501, _("Last modification"),
-            filter=datetime_filter)
+            filter=DATETIME_FILTER)
         self.register("country", 502, _("Country"))
 
         self.register("camera_aperture", 520, _("Camera aperture"))

hachoir-metadata/hachoir_metadata/riff.py

         meta.compression = "%s (fourcc:\"%s\")" \
             % (header["fourcc"].display, header["fourcc"].value)
         if header["rate"].value and header["scale"].value:
-            meta.frame_rate = float(header["rate"].value) / header["scale"].value
-            self.duration = meta.duration = int(header["length"].value * 1000 // meta.frame_rate[0])
+            fps = float(header["rate"].value) / header["scale"].value
+            meta.frame_rate = fps
+            if 0 < fps:
+                self.duration = meta.duration = int(header["length"].value * 1000 // fps)
 
         if "stream_fmt/width" in video:
             format = video["stream_fmt"]