This repository has been archived by the owner on Aug 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif2webm.py
125 lines (93 loc) · 3.37 KB
/
gif2webm.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
import os
import shutil
import tempfile
import subprocess
import threading
import base64
import re
import bottle
import datadog
import pathlib
from concurrent.futures import ThreadPoolExecutor
datadog.initialize()
stats = datadog.ThreadStats()
stats.start()
class VideoNotConvertedError(Exception):
pass
def metric_name(suffix):
return "pr0gramm.gif2vid.v2.%s" % suffix
@stats.timed(metric_name("convert"))
def make_video(url, target):
temp_gif = tempfile.mktemp(suffix=".gif")
temp_output = tempfile.mktemp(suffix=".mp4")
try:
# download file
subprocess.check_call(["timeout", "-s", "KILL", "5s",
"curl", "-o", temp_gif, url])
ffprobe_output = subprocess.check_output(
["timeout", "-s", "KILL", "5s", "ffprobe", "-show_packets", temp_gif])
frame_count = ffprobe_output.count(b"codec_type=video")
if frame_count <= 5:
return False
# and convert
subprocess.check_call(["timeout", "-s", "KILL", "30s",
"ffmpeg", "-y", "-i", temp_gif, "-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2",
"-codec:v", "libx264", "-preset", "medium", "-b:v", "350k", "-an",
"-profile:v", "baseline", "-level", "3.0", "-pix_fmt", "yuv420p",
"-qmin", "20", "-qmax", "42", temp_output])
# and move to target
shutil.copy(temp_output, str(target))
return True
except:
stats.increment(metric_name("error"))
raise
finally:
for temp in temp_gif, temp_output:
try:
os.unlink(temp)
except OSError:
pass
class Converter(object):
def __init__(self):
self.lock = threading.RLock()
self.pool = ThreadPoolExecutor(2)
self.jobs = {}
self.images = pathlib.Path("webm")
if not self.images.exists():
self.images.mkdir()
def video(self, url):
with self.lock:
try:
return self.jobs[url]
except KeyError:
future = self.pool.submit(self._convert_in_background, url)
self.jobs[url] = future
return future
def _convert_in_background(self, url):
target = self.images / re.sub("[^a-z0-9]", "_", url.lower())
if not target.exists() or not target.stat().st_size:
if not make_video(url, target):
raise VideoNotConvertedError()
return target
converter = Converter()
def convert(url):
url = base64.urlsafe_b64decode(url.encode("ascii")).strip().decode("utf8")
if not re.match("^https?://[^/]*pr0gramm.com/.*$", url):
return bottle.abort(403)
# use only http
url = url.replace("https://", "http://")
return str(converter.video(url).result())
@bottle.get("/convert/:url")
def convert_route(url):
convert(url)
bottle.response.add_header("Cache-Control", "max-age=31556926")
return dict(path="/webm/{}/video.webm".format(url))
@bottle.get("/webm/:url/video.webm")
def video_route(url):
stats.increment(metric_name("request"))
response = bottle.static_file(convert(url), os.path.abspath("."), mimetype="video/mp4")
response.set_header("Cache-Control", "max-age=31556926")
return response
@bottle.get("/status")
def status():
return dict(alive=True)