-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
156 lines (134 loc) · 4.21 KB
/
__main__.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Python-Raspberry-Contoll-Managment
#######################################################
Created by Ilja Grebel on 08.02.2016
Version 1.005
:| Copyright |: 2016, Ilja Grebel - igrebel@icloud.com
:| license |: Not licensed now
#######################################################
-TODO:
- Set recording to pause
- Sys. configuration
- Save Screenshots in different format. At moment you can change format in python script
- Save recording in differtent raw format. Currently only .h264. At moment you can change format in python script
- Logic
- Stop and start streaming | not FFmpeg udp streaming
- Image effects + params
'''
# Set default encoding to UTF-8
from flask import Flask
import datetime
import io
import time
import sys
import os
from threading import Lock
import picamera
# Not needed at moment - Streaming
#import socket
if sys.version_info[0] == 2:
from cStringIO import StringIO as bio
else:
from io import BytesIO as bio
camera = None # After starting, camera is offline
camlock = Lock() # Needed to block access from multi responses
record_dir = ''
img_dir = ''
record_file = 'video'
img_file = 'image'
app = Flask(__name__)
#################
# CONFIGURATION #
video_fmt = '.h264'
img_fmt = '.jpeg'
# resolution = ''
# img_effect = ''
# Timestamp
def timestamp():
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H:%M:%S')
return st
# Make directory
def mkdir():
now = timestamp()
global record_dir
global img_dir
record_dir = './record-%s/' % now
img_dir = record_dir + '/images/'
os.mkdir(record_dir)
os.mkdir(img_dir)
# START RECORD
@app.route("/start_record", methods=['POST'])
def start_capture():
global camera
with camlock:
if camera:
return 'already recording ' + record_file + video_fmt
mkdir()
camera = picamera.PiCamera()
camera.resolution = (1920, 1080)
camera.start_preview()
camera.start_recording(record_dir + record_file + video_fmt)
return 'Recording to ' + record_file + video_fmt
# PAUSE RECORD - NOT WORKING AT MOMENT
@app.route("/pause_record", methods=['POST'])
def pause_record():
global camera
with camlock:
if camera:
return 'already paused ' + record_file + video_fmt
camera = picamera.PiCamera()
wait_recording(900000)
return 'Recording to ' + record_file + video_fmt + ' paused'
# STOP RECODING
@app.route("/stop_record", methods=['POST'])
def stop_capture():
global camera
with camlock:
if not camera:
return 'already stopped'
camera.stop_recording()
h264_to_mp4()
camera = None
return 'Record Stopped, converting .h264 to .mp4'
# START STREAM
@app.route("/start_stream", methods=['POST'])
def start_stream():
global camera
with camlock:
os.system('raspivid -t 0 -fps 25 -hf -b 2000000 -o - | ffmpeg -i - -vcodec copy -an -r 30 -g 30 -bufsize 2000000 -pix_fmt yuv420p -f mpegts udp://@239.239.2.1:1234')
return 'UPD Stream is started at udp://@239.239.2.1:1234'
# STOP STREAM
# MP4
def h264_to_mp4():
cmd = ('ffmpeg -i %s -vcodec copy -an -f mp4 %s.mp4') % (record_dir + record_file + video_fmt, record_dir + record_file)
os.system(cmd)
return 'Creating .MP4 File'
#os.remove(record_dir + record_file + ".h264")
#return '.h264 File deleted'
# FOR COUNTER
def static_var(varname, value):
def decorate(func):
setattr(func, varname, value)
return func
return decorate
# SCREENSHOT
@app.route("/screenshot", methods=['POST'])
@static_var("counter", 0)
def screenshot():
with camlock:
screenshot.counter += 1
if not camera:
return 'Camera is not started'
camera.capture((img_dir + '%s-' + img_file + img_fmt) % (screenshot.counter), use_video_port=True)
return 'Saved to %d-image.jpeg' % (screenshot.counter)
if __name__ == "__main__":
try:
app.run(host='0.0.0.0', port='8080')
finally:
# After http-server work is finished, shut off the camera
with camlock:
if camera:
camera.close()