mymeco.files.video

mymeco/files/video.py
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
"""Video file module."""
import logging
import json
import ffmpeg


class Video:
    """
    Video class.

    Aim to read a video file to extract technical information such as video
    codec, resolution, etc…
    """

    _log: logging.Logger = logging.getLogger(__file__)

    def __init__(self, filename):
        self.filename = filename
        self.probe = ffmpeg.probe(self.filename)
        self._log.debug(json.dumps(self.probe, indent=4))

    @property
    def audio(self):
        """Retrieve list of audio stream in file."""
        return [
            {
                'codec': audio.get('codec_long_name', 'Unknown'),
                'language': audio.get(
                    'tags', {'language': 'und'}
                ).get('language', 'und'),
                'channels': audio.get('channels', 0)
            }
            for audio in self.probe['streams']
            if audio.get('codec_type', '') == 'audio'
        ]

    @property
    def subtitle(self):
        """Retrieve list of subtitles."""
        return [
            {
                'language': sub.get(
                    'tags', {'language': 'und'}
                ).get('language', 'und')
            }
            for sub in self.probe['streams']
            if sub.get('codec_type', '') == 'subtitle'
        ]

    @property
    def video(self):
        """Retrieve list of video stream."""
        return [
            {
                'codec': vid.get('codec_long_name', 'Unknown'),
                'aspect': vid.get('display_aspect_ratio', '1:1'),
                'width': vid.get('coded_width', vid.get('width', 0)),
                'height': vid.get('coded_height', vid.get('height', 0)),
                'durationinseconds': float(self.probe['format']['duration']),
            }
            for vid in self.probe['streams']
            if vid.get('codec_type', '') == 'video'
        ]