-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
254 lines (222 loc) · 8.52 KB
/
server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os
import re
from sanic import Sanic, exceptions
from sanic.response import json, stream
from sanic.log import log
import asyncio
from functools import wraps
PYTHUMBIO_PORT = int(os.environ.get('PYTHUMBIO_PORT', (8000)))
PYTHUMBIO_WORKERS = int(os.environ.get('PYTHUMBIO_WORKERS', 2))
PYTHUMBIO_CONCURRENCY_PER_WORKER = int(os.environ.get('PYTHUMBIO_CONCURRENCY_PER_WORKER', 4))
PYTHUMBIO_CHUNKSIZE = int(os.environ.get('PYTHUMBIO_CHUNKSIZE', (1024 * 32)))
sem = None
app = Sanic(__name__)
def required_args(*expected_args):
"""Ensure url parameters exist before processing request"""
def decorator(f):
@wraps(f)
async def wrapper(request, *args, **kwargs):
for expected_arg in expected_args:
if not request.args.get(expected_arg, None):
return json({'Error': '{} parameter is required'.format(expected_arg)})
response = await f(request, *args, **kwargs)
return response
return wrapper
return decorator
@app.listener('before_server_start')
async def init(sanic, loop):
global sem
sem = asyncio.Semaphore(PYTHUMBIO_CONCURRENCY_PER_WORKER, loop=loop)
log.info('Creating semaphore with max concurrency of {} for this worker thread.'
.format(PYTHUMBIO_CONCURRENCY_PER_WORKER))
@app.route('/webm')
@required_args('url')
async def webm(request):
"""Stream webm"""
async def stream_fn(response):
async with sem:
cmd = ['ffmpeg',
'-v',
'quiet',
'-i',
request.args.get('url'),
'-c:v',
'libvpx-vp9',
'-b:v',
'256k',
'-tile-columns',
'6',
'-frame-parallel',
'1',
'-threads',
'2',
'-deadline',
'realtime',
'-speed',
'5',
'-lag-in-frames',
'1',
'-vf',
'scale=w=426:h=240:force_original_aspect_ratio=decrease',
'-c:a',
'libvorbis',
'-b:a',
'64k',
'-f',
'webm',
'-'
]
proc = await asyncio.create_subprocess_exec(*cmd,
stdout=asyncio.subprocess.PIPE
)
while True:
chunk = await proc.stdout.read(PYTHUMBIO_CHUNKSIZE)
if not chunk:
break
response.write(chunk)
return stream(stream_fn, content_type='video/webm')
@app.route('/preview')
@required_args('url')
async def preview(request):
"""Stream webm preview"""
async def stream_fn(response):
async with sem:
cmd = ['ffmpeg',
'-v',
'quiet',
'-i',
request.args.get('url'),
'-c:v',
'libvpx-vp9',
'-b:v',
'256k',
'-tile-columns',
'6',
'-frame-parallel',
'1',
'-threads',
'2',
'-deadline',
'realtime',
'-speed',
'5',
'-lag-in-frames',
'1',
'-vf',
'select=isnan(prev_selected_t)+gt(t-prev_selected_t\,4),setpts=(1/10)*PTS,scale=w=426:h=240:force_original_aspect_ratio=decrease',
'-an',
'-f',
'webm',
'-'
]
proc = await asyncio.create_subprocess_exec(*cmd,
stdout=asyncio.subprocess.PIPE
)
while True:
chunk = await proc.stdout.read(PYTHUMBIO_CHUNKSIZE)
if not chunk:
break
response.write(chunk)
return stream(stream_fn, content_type='video/webm')
@app.route('/video')
@app.route('/thumb')
@required_args('url')
async def thumb(request):
"""Return jpg thumbnail with optional watermark"""
watermark = request.args.get('watermark', None)
width = request.args.get('width', -1)
height = request.args.get('height', -1)
alpha = request.args.get('alpha', 0.5)
scale = request.args.get('scale', 0.20)
offset = request.args.get('offset', 0.1)
log.info(watermark)
async def stream_fn(response):
async with sem:
if watermark:
cmd = ['ffmpeg',
'-v',
'quiet',
'-i',
request.args.get('url'),
'-i', watermark,
'-ss', '00:00:03',
'-frames:v', '1',
'-f', 'image2',
'-filter_complex',
'[1:v]colorchannelmixer=aa={alpha}[translogo]; \
[translogo][0:v]scale2ref={scale}*min(iw\,ih):{scale}*min(iw\,ih)[logo1][base]; \
[base][logo1]overlay=W-w-{offset}*min(W\,H):H-h-{offset}*min(W\,H)[prev]; \
[prev]scale=w={width}:h={height}:force_original_aspect_ratio=decrease[out]'
.format(width=width, height=height, alpha=alpha, offset=offset, scale=scale),
'-map', '[out]',
'-'
]
else:
cmd = ['ffmpeg',
'-v',
'quiet',
'-i',
request.args.get('url'),
'-vf',
'select=(isnan(prev_selected_t)*gt(t\,2.0))+gt(scene\,0.5),scale=w=426:h=240:force_original_aspect_ratio=decrease,tile=1x1',
'-frames:v',
'1',
'-f',
'image2',
'-'
]
proc = await asyncio.create_subprocess_exec(*cmd,
stdout=asyncio.subprocess.PIPE
)
while True:
chunk = await proc.stdout.read(PYTHUMBIO_CHUNKSIZE)
if not chunk:
break
response.write(chunk)
return stream(stream_fn, content_type='image/jpeg')
@app.route('/meta')
@required_args('url')
async def meta(request):
"""Return ffprobe metadata"""
async def stream_fn(response):
async with sem:
cmd = ['ffprobe',
'-v',
'quiet',
'-i',
request.args.get('url'),
'-print_format',
'json',
'-show_format',
'-show_streams'
]
proc = await asyncio.create_subprocess_exec(*cmd,
stdout=asyncio.subprocess.PIPE
)
while True:
chunk = await proc.stdout.read(PYTHUMBIO_CHUNKSIZE)
if not chunk:
break
response.write(chunk)
return stream(stream_fn, content_type='application/json')
@app.route('/version')
async def version(request):
"""Return the output of `ffmpeg -version`"""
async def stream_fn(response):
async with sem:
proc = await asyncio.create_subprocess_exec('ffmpeg',
'-version',
stdout=asyncio.subprocess.PIPE,
)
response.write('{"ver":"')
while True:
chunk = await proc.stdout.read(5)
if not chunk:
break
response.write(chunk)
response.write('"}')
return stream(stream_fn, content_type='application/json')
@app.exception(exceptions.NotFound)
def ignore_404s(request, exception):
return json({'Error': '404: {}'.format(request.url)})
app.run(host='0.0.0.0', port=PYTHUMBIO_PORT, workers=PYTHUMBIO_WORKERS)