Passed
Push — master ( 79d402...4ebce0 )
by Amin
01:15
created

ffmpeg_streaming/_media_streams.py (1 issue)

1
"""
2
ffmpeg_streaming.streams
3
~~~~~~~~~~~~
4
5
Parse streams that is the output of the FFProbe
6
7
8
:copyright: (c) 2020 by Amin Yazdanpanah.
9
:website: https://www.aminyazdanpanah.com
10
:email: [email protected]
11
:license: MIT, see LICENSE for more details.
12
"""
13
14
15 View Code Duplication
class Streams:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
16
    def __init__(self, streams):
17
        self.streams = streams
18
19
    def video(self, ignore_error=True):
20
        """
21
        @TODO: add documentation
22
        """
23
        return self._get_stream('video', ignore_error)    
24
        
25
    def audio(self, ignore_error=True):
26
        """
27
        @TODO: add documentation
28
        """
29
        return self._get_stream('audio', ignore_error)
30
31
    def first_stream(self):
32
        """
33
        @TODO: add documentation
34
        """
35
        return self.streams[0]
36
37
    def all(self):
38
        """
39
        @TODO: add documentation
40
        """
41
        return self.streams
42
43
    def videos(self):
44
        """
45
        @TODO: add documentation
46
        """
47
        return self._get_streams('video')
48
49
    def audios(self):
50
        """
51
        @TODO: add documentation
52
        """
53
        return self._get_streams('audio')
54
55
    def _get_stream(self, media, ignore_error):
56
        """
57
        @TODO: add documentation
58
        """
59
        media_attr = next((stream for stream in self.streams if stream['codec_type'] == media), None)
60
        if media_attr is None and not ignore_error:
61
            raise ValueError('No ' + str(media) + ' stream found')
62
        return media_attr if media_attr is not None else {}
63
64
    def _get_streams(self, media):
65
        """
66
        @TODO: add documentation
67
        """
68
        for stream in self.streams:
69
            if stream['codec_type'] == media:
70
                yield stream
71