1
|
|
|
""" |
2
|
|
|
ffmpeg_streaming.process |
3
|
|
|
~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Run FFmpeg commands and monitor FFmpeg |
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
|
|
|
import shlex |
15
|
|
|
import subprocess |
16
|
|
|
import threading |
17
|
|
|
import logging |
18
|
|
|
import time |
19
|
|
|
|
20
|
|
|
from ffmpeg_streaming._hls_helper import HLSKeyInfoFile |
21
|
|
|
from ffmpeg_streaming._utiles import get_time, time_left |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def _p_open(commands, **options): |
25
|
|
|
""" |
26
|
|
|
@TODO: add documentation |
27
|
|
|
""" |
28
|
|
|
logging.info("ffmpeg running command: {}".format(commands)) |
29
|
|
|
return subprocess.Popen(shlex.split(commands), **options) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class Process(object): |
33
|
|
|
out = None |
34
|
|
|
err = None |
35
|
|
|
|
36
|
|
|
def __init__(self, media, commands: str, monitor: callable = None, **options): |
37
|
|
|
""" |
38
|
|
|
@TODO: add documentation |
39
|
|
|
""" |
40
|
|
|
self.is_monitor = False |
41
|
|
|
self.input = options.pop('input', None) |
42
|
|
|
self.timeout = options.pop('timeout', None) |
43
|
|
|
default_proc_opts = { |
44
|
|
|
'stdin': None, |
45
|
|
|
'stdout': subprocess.PIPE, |
46
|
|
|
'stderr': subprocess.STDOUT, |
47
|
|
|
'universal_newlines': False |
48
|
|
|
} |
49
|
|
|
default_proc_opts.update(options) |
50
|
|
|
options.update(default_proc_opts) |
51
|
|
|
if callable(monitor) or isinstance(getattr(media, 'key_rotation'), HLSKeyInfoFile): |
52
|
|
|
self.is_monitor = True |
53
|
|
|
options.update({ |
54
|
|
|
'stdin': subprocess.PIPE, |
55
|
|
|
'universal_newlines': True |
56
|
|
|
}) |
57
|
|
|
|
58
|
|
|
self.process = _p_open(commands, **options) |
59
|
|
|
self.media = media |
60
|
|
|
self.monitor = monitor |
61
|
|
|
|
62
|
|
|
def __enter__(self): |
63
|
|
|
return self |
64
|
|
|
|
65
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb): |
66
|
|
|
self.process.kill() |
67
|
|
|
|
68
|
|
|
def _monitor(self): |
69
|
|
|
""" |
70
|
|
|
@TODO: add documentation |
71
|
|
|
""" |
72
|
|
|
duration = 1 |
73
|
|
|
_time = 0 |
74
|
|
|
log = [] |
75
|
|
|
start_time = time.time() |
76
|
|
|
|
77
|
|
|
while True: |
78
|
|
|
line = self.process.stdout.readline().strip() |
79
|
|
|
if line == '' and self.process.poll() is not None: |
80
|
|
|
break |
81
|
|
|
|
82
|
|
|
if line != '': |
83
|
|
|
log += [line] |
84
|
|
|
|
85
|
|
|
if isinstance(getattr(self.media, 'key_rotation'), HLSKeyInfoFile): |
86
|
|
|
getattr(self.media, 'key_rotation').rotate_key(line) |
87
|
|
|
|
88
|
|
|
if callable(self.monitor): |
89
|
|
|
duration = get_time('Duration: ', line, duration) |
90
|
|
|
_time = get_time('time=', line, _time) |
91
|
|
|
self.monitor(line, duration, _time, time_left(start_time, _time, duration), self.process) |
92
|
|
|
|
93
|
|
|
Process.out = log |
94
|
|
|
|
95
|
|
|
def _thread_mon(self): |
96
|
|
|
""" |
97
|
|
|
@TODO: add documentation |
98
|
|
|
""" |
99
|
|
|
thread = threading.Thread(target=self._monitor) |
100
|
|
|
thread.start() |
101
|
|
|
|
102
|
|
|
thread.join(self.timeout) |
103
|
|
|
if thread.is_alive(): |
104
|
|
|
self.process.terminate() |
105
|
|
|
thread.join() |
106
|
|
|
error = 'Timeout! exceeded the timeout of {} seconds.'.format(str(self.timeout)) |
107
|
|
|
logging.error(error) |
108
|
|
|
raise RuntimeError(error) |
109
|
|
|
|
110
|
|
|
def run(self): |
111
|
|
|
""" |
112
|
|
|
@TODO: add documentation |
113
|
|
|
""" |
114
|
|
|
if self.is_monitor: |
115
|
|
|
self._thread_mon() |
116
|
|
|
else: |
117
|
|
|
Process.out, Process.err = self.process.communicate(self.input, self.timeout) |
118
|
|
|
|
119
|
|
|
if self.process.poll(): |
120
|
|
|
error = str(Process.err) if Process.err else str(Process.out) |
121
|
|
|
logging.error('ffmpeg failed to execute command: {}'.format(error)) |
122
|
|
|
raise RuntimeError('ffmpeg failed to execute command: ', error) |
123
|
|
|
|
124
|
|
|
logging.info("ffmpeg executed command successfully") |
125
|
|
|
|
126
|
|
|
return Process.out, Process.err |
127
|
|
|
|