From 3ce332a5ad9017a23ba3351ca35f075bf43da569 Mon Sep 17 00:00:00 2001 From: JackDandy Date: Wed, 12 Oct 2016 00:52:28 +0100 Subject: [PATCH] Change make sure avi files are closed if an error occurs. Change refactor hachoir scan_index for non RIFF file processing. --- CHANGES.md | 2 ++ lib/hachoir_metadata/metadata.py | 18 +++++++++++++++--- lib/hachoir_metadata/riff.py | 10 +++++++--- sickbeard/common.py | 5 ++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a5f437a3..f14c11c6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -203,6 +203,8 @@ * Change add rate limit handler for info source * Change improve security of cached image use * Change add helper function to validate acceptable image file extension +* Change make sure avi files are closed if an error occurs +* Change refactor hachoir scan_index for non RIFF file processing ### 0.11.16 (2016-10-16 17:30:00 UTC) diff --git a/lib/hachoir_metadata/metadata.py b/lib/hachoir_metadata/metadata.py index bb444db4..da180d4d 100644 --- a/lib/hachoir_metadata/metadata.py +++ b/lib/hachoir_metadata/metadata.py @@ -270,7 +270,8 @@ def registerExtractor(parser, extractor): assert issubclass(extractor, RootMetadata) extractors[parser] = extractor -def extractMetadata(parser, quality=QUALITY_NORMAL, scan_index=True): + +def extractMetadata(parser, quality=QUALITY_NORMAL, **kwargs): """ Create a Metadata class from a parser. Returns None if no metadata extractor does exist for the parser class. @@ -280,14 +281,25 @@ def extractMetadata(parser, quality=QUALITY_NORMAL, scan_index=True): except KeyError: return None metadata = extractor(quality) + meta_extract_error = True try: - metadata.extract(parser, scan_index) + if 'scan_index' in kwargs: + metadata.extract(parser, scan_index=kwargs['scan_index']) + else: + metadata.extract(parser) + meta_extract_error = False except HACHOIR_ERRORS, err: error("Error during metadata extraction: %s" % unicode(err)) - return None except Exception, err: error("Error during metadata extraction: %s" % unicode(err)) + + if meta_extract_error: + try: + parser.stream._input.close() + except: + pass return None + if metadata: metadata.mime_type = parser.mime_type metadata.endian = endian_name[parser.endian] diff --git a/lib/hachoir_metadata/riff.py b/lib/hachoir_metadata/riff.py index e9100505..3dba4460 100644 --- a/lib/hachoir_metadata/riff.py +++ b/lib/hachoir_metadata/riff.py @@ -23,7 +23,7 @@ class RiffMetadata(MultipleMetadata): "IDIT": "creation_date", } - def extract(self, riff, scan_index=True): + def extract(self, riff, **kwargs): type = riff["type"].value if type == "WAVE": self.extractWAVE(riff) @@ -32,7 +32,10 @@ class RiffMetadata(MultipleMetadata): computeAudioComprRate(self, size*8) elif type == "AVI ": if "headers" in riff: - self.extractAVI(riff["headers"], scan_index) + if 'scan_index' in kwargs: + self.extractAVI(riff["headers"], scan_index=kwargs['scan_index']) + else: + self.extractAVI(riff["headers"]) self.extractInfo(riff["headers"]) elif type == "ACON": self.extractAnim(riff) @@ -142,7 +145,7 @@ class RiffMetadata(MultipleMetadata): self.width = header["width"].value self.height = header["height"].value - def extractAVI(self, headers, scan_index=True): + def extractAVI(self, headers, **kwargs): audio_index = 1 for stream in headers.array("stream"): if "stream_hdr/stream_type" not in stream: @@ -167,6 +170,7 @@ class RiffMetadata(MultipleMetadata): self.bit_rate = float(headers["/movie/size"].value) * 8 / timedelta2seconds(self.get('duration')) # Video has index? + scan_index = (True, kwargs['scan_index'])['scan_index' in kwargs] if scan_index and "/index" in headers: self.comment = _("Has audio/video index (%s)") \ % humanFilesize(headers["/index"].size/8) diff --git a/sickbeard/common.py b/sickbeard/common.py index b69f71cd..95766705 100644 --- a/sickbeard/common.py +++ b/sickbeard/common.py @@ -261,7 +261,10 @@ class Quality: logger.log(traceback.format_exc(), logger.DEBUG) if parser: - extract = extractMetadata(parser, scan_index=False) + if '.avi' == filename[-4::].lower(): + extract = extractMetadata(parser, scan_index=False) + else: + extract = extractMetadata(parser) if extract: try: height = extract.get('height')