summaryrefslogtreecommitdiffhomepage
path: root/libs/enzyme/mkv.py
blob: 1670a692060f551f9ca97c31a7ca196cd3141b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# -*- coding: utf-8 -*-
from .exceptions import ParserError, MalformedMKVError
from .parsers import ebml
from datetime import timedelta
import logging


__all__ = ['VIDEO_TRACK', 'AUDIO_TRACK', 'SUBTITLE_TRACK', 'MKV', 'Info', 'Track', 'VideoTrack',
           'AudioTrack', 'SubtitleTrack', 'Tag', 'SimpleTag', 'Chapter']
logger = logging.getLogger(__name__)


# Track types
VIDEO_TRACK, AUDIO_TRACK, SUBTITLE_TRACK = 0x01, 0x02, 0x11


class MKV(object):
    """Matroska Video file

    :param stream: seekable file-like object

    """
    def __init__(self, stream, recurse_seek_head=False):
        # default attributes
        self.info = None
        self.video_tracks = []
        self.audio_tracks = []
        self.subtitle_tracks = []
        self.chapters = []
        self.tags = []

        # keep track of the elements parsed
        self.recurse_seek_head = recurse_seek_head
        self._parsed_positions = set()

        try:
            # get the Segment element
            logger.info('Reading Segment element')
            specs = ebml.get_matroska_specs()
            segments = ebml.parse(stream, specs, ignore_element_names=['EBML'], max_level=0)
            if not segments:
                raise MalformedMKVError('No Segment found')
            if len(segments) > 1:
                logger.warning('%d segments found, using the first one', len(segments))
            segment = segments[0]

            # get and recursively parse the SeekHead element
            logger.info('Reading SeekHead element')
            stream.seek(segment.position)
            seek_head = ebml.parse_element(stream, specs)
            if seek_head.name != 'SeekHead':
                raise MalformedMKVError('No SeekHead found')
            seek_head.load(stream, specs, ignore_element_names=['Void', 'CRC-32'])
            self._parse_seekhead(seek_head, segment, stream, specs)
        except ParserError as e:
            raise MalformedMKVError('Parsing error: %s' % e)

    def _parse_seekhead(self, seek_head, segment, stream, specs):
        for seek in seek_head:
            element_id = ebml.read_element_id(seek['SeekID'].data)
            element_name = specs[element_id][1]
            element_position = seek['SeekPosition'].data + segment.position
            if element_position in self._parsed_positions:
                logger.warning('Skipping already parsed %s element at position %d', element_name, element_position)
                continue
            if element_name == 'Info':
                logger.info('Processing element %s from SeekHead at position %d', element_name, element_position)
                element = self._load_element(stream, specs, element_position)
                self.info = Info.fromelement(element)
            elif element_name == 'Tracks':
                logger.info('Processing element %s from SeekHead at position %d', element_name, element_position)
                tracks = self._load_element(stream, specs, element_position)
                self.video_tracks.extend([VideoTrack.fromelement(t) for t in tracks if t['TrackType'].data == VIDEO_TRACK])
                self.audio_tracks.extend([AudioTrack.fromelement(t) for t in tracks if t['TrackType'].data == AUDIO_TRACK])
                self.subtitle_tracks.extend([SubtitleTrack.fromelement(t) for t in tracks if t['TrackType'].data == SUBTITLE_TRACK])
            elif element_name == 'Chapters':
                logger.info('Processing element %s from SeekHead at position %d', element_name, element_position)
                element = self._load_element(stream, specs, element_position)
                self.chapters.extend([Chapter.fromelement(c) for c in element[0] if c.name == 'ChapterAtom'])
            elif element_name == 'Tags':
                logger.info('Processing element %s from SeekHead at position %d', element_name, element_position)
                element = self._load_element(stream, specs, element_position)
                self.tags.extend([Tag.fromelement(t) for t in element])
            elif element_name == 'SeekHead' and self.recurse_seek_head:
                logger.info('Processing element %s from SeekHead at position %d', element_name, element_position)
                element = self._load_element(stream, specs, element_position)
                self._parse_seekhead(element, segment, stream, specs)
            else:
                logger.debug('Element %s ignored', element_name)
            self._parsed_positions.add(element_position)
            
    def _load_element(self,stream, specs, position):
        stream.seek(position)
        element = ebml.parse_element(stream,specs)
        element.load(stream, specs, ignore_element_names=['Void', 'CRC-32'])
        return element
    
    def get_srt_subtitles_track_by_language(self):
        """get a dictionary of the SRT subtitles track id's indexed by language"""
        
        subtitles = dict()
        for track in self.subtitle_tracks:

            logger.info("Found subtitle language %s, with codec %s and lacing %s",
                         track.language,track.codec_id,track.lacing)
            
            if not track.is_srt():
                logger.debug("Ignoring subtitle language %s with codec %s",track.language,track.codec_id)
            elif track.lacing:
                logger.info("Ignoring subtitle language %s with lacing %s",track.language,track.lacing)
            else:
                subtitles[track.language] = track

        return subtitles        

    def to_dict(self):
        return {'info': self.info.__dict__, 'video_tracks': [t.__dict__ for t in self.video_tracks],
                'audio_tracks': [t.__dict__ for t in self.audio_tracks], 'subtitle_tracks': [t.__dict__ for t in self.subtitle_tracks],
                'chapters': [c.__dict__ for c in self.chapters], 'tags': [t.__dict__ for t in self.tags]}

    def __repr__(self):
        return '<%s [%r, %r, %r, %r]>' % (self.__class__.__name__, self.info, self.video_tracks, self.audio_tracks, self.subtitle_tracks)


class Info(object):
    """Object for the Info EBML element"""
    def __init__(self, title=None, duration=None, date_utc=None, timecode_scale=None, muxing_app=None, writing_app=None):
        self.title = title
        self.timecode_scale = timecode_scale
        self.duration = timedelta(microseconds=duration * (timecode_scale or 1000000) // 1000) if duration else None
        self.date_utc = date_utc
        self.muxing_app = muxing_app
        self.writing_app = writing_app

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`Info` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the Info element
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        title = element.get('Title')
        duration = element.get('Duration')
        date_utc = element.get('DateUTC')
        timecode_scale = element.get('TimecodeScale',1000000)
        muxing_app = element.get('MuxingApp')
        writing_app = element.get('WritingApp')
        return cls(title, duration, date_utc, timecode_scale, muxing_app, writing_app)

    def __repr__(self):
        return '<%s [title=%r, duration=%s, date=%s]>' % (self.__class__.__name__, self.title, self.duration, self.date_utc)

    def __str__(self):
        return repr(self.__dict__)


class Track(object):
    """Base object for the Tracks EBML element"""
    def __init__(self, type=None, number=None, name=None, language=None, enabled=None, default=None, forced=None, lacing=None,
                 codec_id=None, codec_name=None):
        self.type = type
        self.number = number
        self.name = name
        self.language = language
        self.enabled = enabled
        self.default = default
        self.forced = forced
        self.lacing = lacing
        self.codec_id = codec_id
        self.codec_name = codec_name

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`Track` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the Track element
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        type = element.get('TrackType')
        number = element.get('TrackNumber', 0)
        name = element.get('Name')
        language = element.get('Language','eng')
        enabled = bool(element.get('FlagEnabled', 1))
        default = bool(element.get('FlagDefault', 1))
        forced = bool(element.get('FlagForced', 0))
        lacing = bool(element.get('FlagLacing', 1))
        codec_id = element.get('CodecID')
        codec_name = element.get('CodecName')
        return cls(type=type, number=number, name=name, language=language, enabled=enabled, default=default,
                   forced=forced, lacing=lacing, codec_id=codec_id, codec_name=codec_name)

    def __repr__(self):
        return '<%s [%d, name=%r, language=%s]>' % (self.__class__.__name__, self.number, self.name, self.language)

    def __str__(self):
        return str(self.__dict__)


class VideoTrack(Track):
    """Object for the Tracks EBML element with :data:`VIDEO_TRACK` TrackType"""
    def __init__(self, width=0, height=0, interlaced=False, stereo_mode=None, crop=None,
                 display_width=None, display_height=None, display_unit=None, aspect_ratio_type=None, **kwargs):
        super(VideoTrack, self).__init__(**kwargs)
        self.width = width
        self.height = height
        self.interlaced = interlaced
        self.stereo_mode = stereo_mode
        self.crop = crop
        self.display_width = display_width
        self.display_height = display_height
        self.display_unit = display_unit
        self.aspect_ratio_type = aspect_ratio_type

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`VideoTrack` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the Track element with :data:`VIDEO_TRACK` TrackType
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        videotrack = super(VideoTrack, cls).fromelement(element)
        videotrack.width = element['Video'].get('PixelWidth', 0)
        videotrack.height = element['Video'].get('PixelHeight', 0)
        videotrack.interlaced = bool(element['Video'].get('FlagInterlaced', False))
        videotrack.stereo_mode = element['Video'].get('StereoMode')
        videotrack.crop = {}
        if 'PixelCropTop' in element['Video']:
            videotrack.crop['top'] = element['Video']['PixelCropTop']
        if 'PixelCropBottom' in element['Video']:
            videotrack.crop['bottom'] = element['Video']['PixelCropBottom']
        if 'PixelCropLeft' in element['Video']:
            videotrack.crop['left'] = element['Video']['PixelCropLeft']
        if 'PixelCropRight' in element['Video']:
            videotrack.crop['right'] = element['Video']['PixelCropRight']
        videotrack.display_width = element['Video'].get('DisplayWidth')
        videotrack.display_height = element['Video'].get('DisplayHeight')
        videotrack.display_unit = element['Video'].get('DisplayUnit')
        videotrack.aspect_ratio_type = element['Video'].get('AspectRatioType')
        return videotrack

    def __repr__(self):
        return '<%s [%d, %dx%d, %s, name=%r, language=%s]>' % (self.__class__.__name__, self.number, self.width, self.height,
                                                            self.codec_id, self.name, self.language)

    def __str__(self):
        return str(self.__dict__)


class AudioTrack(Track):
    """Object for the Tracks EBML element with :data:`AUDIO_TRACK` TrackType"""
    def __init__(self, sampling_frequency=None, channels=None, output_sampling_frequency=None, bit_depth=None, **kwargs):
        super(AudioTrack, self).__init__(**kwargs)
        self.sampling_frequency = sampling_frequency
        self.channels = channels
        self.output_sampling_frequency = output_sampling_frequency
        self.bit_depth = bit_depth

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`AudioTrack` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the Track element with :data:`AUDIO_TRACK` TrackType
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        audiotrack = super(AudioTrack, cls).fromelement(element)
        audiotrack.sampling_frequency = element['Audio'].get('SamplingFrequency', 8000.0)
        audiotrack.channels = element['Audio'].get('Channels', 1)
        audiotrack.output_sampling_frequency = element['Audio'].get('OutputSamplingFrequency')
        audiotrack.bit_depth = element['Audio'].get('BitDepth')
        return audiotrack

    def __repr__(self):
        return '<%s [%d, %d channel(s), %.0fHz, %s, name=%r, language=%s]>' % (self.__class__.__name__, self.number, self.channels,
                                                                            self.sampling_frequency, self.codec_id, self.name, self.language)


class SubtitleTrack(Track):
    """Object for the Tracks EBML element with :data:`SUBTITLE_TRACK` TrackType"""
    
    def is_srt(self):
        return self.codec_id == 'S_TEXT/UTF8'

class Tag(object):
    """Object for the Tag EBML element"""
    def __init__(self, targets=None, simpletags=None):
        self.targets = targets if targets is not None else []
        self.simpletags = simpletags if simpletags is not None else []

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`Tag` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the Tag element
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        targets = element['Targets'] if 'Targets' in element else []
        simpletags = [SimpleTag.fromelement(s) for s in element if s.name == 'SimpleTag']
        return cls(targets, simpletags)

    def __repr__(self):
        return '<%s [targets=%r, simpletags=%r]>' % (self.__class__.__name__, self.targets, self.simpletags)


class SimpleTag(object):
    """Object for the SimpleTag EBML element"""
    def __init__(self, name, language='und', default=True, string=None, binary=None):
        self.name = name
        self.language = language
        self.default = default
        self.string = string
        self.binary = binary

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`SimpleTag` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the SimpleTag element
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        name = element.get('TagName')
        language = element.get('TagLanguage', 'und')
        default = element.get('TagDefault', True)
        string = element.get('TagString')
        binary = element.get('TagBinary')
        return cls(name, language, default, string, binary)

    def __repr__(self):
        return '<%s [%s, language=%s, default=%s, string=%s]>' % (self.__class__.__name__, self.name, self.language, self.default, self.string)


class Chapter(object):
    """Object for the ChapterAtom and ChapterDisplay EBML element

    .. note::
        For the sake of simplicity, it is assumed that the ChapterAtom element
        has no more than 1 ChapterDisplay child element and informations it contains
        are merged into the :class:`Chapter`

    """
    def __init__(self, start, hidden=False, enabled=False, end=None, string=None, language=None):
        self.start = start
        self.hidden = hidden
        self.enabled = enabled
        self.end = end
        self.string = string
        self.language = language

    @classmethod
    def fromelement(cls, element):
        """Load the :class:`Chapter` from an :class:`~enzyme.parsers.ebml.Element`

        :param element: the ChapterAtom element
        :type element: :class:`~enzyme.parsers.ebml.Element`

        """
        start = timedelta(microseconds=element.get('ChapterTimeStart') // 1000)
        hidden = element.get('ChapterFlagHidden', False)
        enabled = element.get('ChapterFlagEnabled', True)
        end = element.get('ChapterTimeEnd')
        chapterdisplays = [c for c in element if c.name == 'ChapterDisplay']
        if len(chapterdisplays) > 1:
            logger.warning('More than 1 (%d) ChapterDisplay element in the ChapterAtom, using the first one', len(chapterdisplays))
        if chapterdisplays:
            string = chapterdisplays[0].get('ChapString')
            language = chapterdisplays[0].get('ChapLanguage')
            return cls(start, hidden, enabled, end, string, language)
        return cls(start, hidden, enabled, end)

    def __repr__(self):
        return '<%s [%s, enabled=%s]>' % (self.__class__.__name__, self.start, self.enabled)