From 2c94f9dd82bd243ab3e6e3d2b7df9d9a2cf8e080 Mon Sep 17 00:00:00 2001 From: tcavallari Date: Sun, 6 Jul 2014 12:02:02 +0200 Subject: [PATCH] Patches the enzyme lib to allow the detection of subtitles embedded into mp4/m4v files --- lib/enzyme/__init__.py | 2 +- lib/enzyme/mp4.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/enzyme/__init__.py b/lib/enzyme/__init__.py index c283dd59..b11c633f 100644 --- a/lib/enzyme/__init__.py +++ b/lib/enzyme/__init__.py @@ -27,7 +27,7 @@ from exceptions import * PARSERS = [('asf', ['video/asf'], ['asf', 'wmv', 'wma']), ('flv', ['video/flv'], ['flv']), ('mkv', ['video/x-matroska', 'application/mkv'], ['mkv', 'mka', 'webm']), - ('mp4', ['video/quicktime', 'video/mp4'], ['mov', 'qt', 'mp4', 'mp4a', '3gp', '3gp2', '3g2', 'mk2']), + ('mp4', ['video/quicktime', 'video/mp4'], ['mov', 'qt', 'mp4', 'mp4a', 'm4v', '3gp', '3gp2', '3g2', 'mk2']), ('mpeg', ['video/mpeg'], ['mpeg', 'mpg', 'mp4', 'ts']), ('ogm', ['application/ogg'], ['ogm', 'ogg', 'ogv']), ('real', ['video/real'], ['rm', 'ra', 'ram']), diff --git a/lib/enzyme/mp4.py b/lib/enzyme/mp4.py index c53f30d3..e8a2c329 100644 --- a/lib/enzyme/mp4.py +++ b/lib/enzyme/mp4.py @@ -300,6 +300,13 @@ class MPEG4(core.AVContainer): trackinfo['length'] = mdhd[4] / mdhd[3] if mdhd[5] in QTLANGUAGES: trackinfo['language'] = QTLANGUAGES[mdhd[5]] + elif mdhd[5] == 0x7FF: + trackinfo['language'] = 'und' + elif mdhd[5] >= 0x400: + # language code detected as explained in: + # https://developer.apple.com/library/mac/documentation/QuickTime/qtff/QTFFChap4/qtff4.html#//apple_ref/doc/uid/TP40000939-CH206-35103 + language = bytearray([ ((mdhd[5] & 0x7C00) >> 10) + 0x60, ((mdhd[5] & 0x3E0) >> 5) + 0x60, (mdhd[5] & 0x1F) + 0x60]) + trackinfo['language'] = str(language) # mdhd[6] == quality self.length = max(self.length, mdhd[4] / mdhd[3]) elif mdia[1] == 'minf': @@ -312,11 +319,13 @@ class MPEG4(core.AVContainer): datasize += (mdia[0] - 8) elif mdia[1] == 'hdlr': hdlr = struct.unpack('>I4s4s', atomdata[pos + 8:pos + 8 + 12]) - if hdlr[1] == 'mhlr': + if hdlr[1] == 'mhlr' or hdlr[1] == '\0\0\0\0': if hdlr[2] == 'vide': tracktype = 'video' if hdlr[2] == 'soun': tracktype = 'audio' + if hdlr[2] == 'subt' or hdlr[2] == 'sbtl' or hdlr[2] == 'subp' or hdlr[2] == 'text': + tracktype = 'subtitle' elif mdia[1] == 'stsd': stsd = struct.unpack('>2I', atomdata[pos + 8:pos + 8 + 8]) if stsd[1] > 0: @@ -369,6 +378,9 @@ class MPEG4(core.AVContainer): if tracktype == 'audio': info = core.AudioStream() self.audio.append(info) + if tracktype == 'subtitle': + info = core.Subtitle() + self.subtitles.append(info) if info: for key, value in trackinfo.items(): setattr(info, key, value)