-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
65 lines (53 loc) · 1.75 KB
/
utils.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
64
65
import os
from glob import glob
from subprocess import Popen, PIPE
from datetime import datetime
from threading import Thread
from notifications import send_sms
def delete_files():
"""Remove files matching OUTPUT_FILE_PREFIX"""
files = glob(os.environ['OUTPUT_FILE_PREFIX'] + '*')
for f in files:
os.remove(f)
class VideoWriter(object):
def __init__(self, *args, **kwargs):
self.ffmpeg_process = None
def initialize_ffmpeg(self):
"""Create a process to run ffmpeg"""
self.video_filename = '{}_{}.mp4'.format(
os.environ['OUTPUT_FILE_PREFIX'],
datetime.now().strftime('%Y-%m-%d-%H-%M-%s')
)
self.ffmpeg_process = Popen([
'ffmpeg',
'-y',
'-f',
'image2pipe',
'-vcodec', 'mjpeg',
'-r', '24',
'-i',
'-',
'-vcodec', 'mpeg4',
'-q', '5',
'-fs', '590000',
'-r', '24',
self.video_filename
],
stdin=PIPE,
stdout=PIPE)
def start_recording(self, frame):
"""Write image frame to stdin of subprocess"""
if not self.ffmpeg_process:
self.initialize_ffmpeg()
try:
# write the frame to ffmpeg process' stdin
self.ffmpeg_process.stdin.write(frame)
self.ffmpeg_process.stdin.flush()
except BrokenPipeError:
self.finish_recording()
def finish_recording(self):
"""Close subprocess stdin"""
if self.ffmpeg_process and not self.ffmpeg_process.stdin.closed:
self.ffmpeg_process.stdin.close()
self.ffmpeg_process = None
Thread(target=send_sms, args=(self.video_filename,)).start()