aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/livestreamer/stream.py
blob: c2b565824da038706598a64dbcb6c12536905754 (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
from livestreamer.utils import CommandLine

import subprocess, shlex

class Stream(object):
    def __init__(self, params={}):
        self.params = params
        self.process = None

    def open(self):
        if self.process:
            self.close()

        cmdline = self.cmdline().format()
        args = shlex.split(cmdline)

        self.process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    def read(self, *args):
        if self.process:
            return self.process.stdout.read(*args)

    def close(self):
        if self.process:
            self.process.kill()
            self.process = None

    def cmdline(self, out=None):
       raise NotImplementedError

class RTMPStream(Stream):
    def cmdline(self, out=None):
        cmd = CommandLine("rtmpdump")

        for key, value in self.params.items():
            if key == "live":
                if value == 1:
                    cmd.args[key] = True

            cmd.args[key] = value

        if out:
            cmd.args["flv"] = out

        return cmd