-
Notifications
You must be signed in to change notification settings - Fork 5
/
convert_to_gif.py
55 lines (45 loc) · 1.58 KB
/
convert_to_gif.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
import os
import subprocess
def gif_converter(
load_path=None,
save_path=None,
start_time='00:00:00',
duration='5.',
fps='10',
frame_width='320',
):
# Setup path
dir_path = os.path.dirname(os.path.abspath(__file__))
if load_path is None:
load_path = os.path.join(dir_path, 'sample/sample_2.mp4')
if save_path is None:
save_path = os.path.join(dir_path, 'output/')
os.makedirs(save_path, exist_ok=True)
subprocess.call(['mkdir', 'tmp/'])
# Extract frames
subprocess.call([
'ffmpeg',
'-ss', start_time, # start time
'-t', duration, # duration
'-i', load_path, # original file dir
'-vf', "fps=" + fps + ", scale=" + frame_width + ":-1",
'-pix_fmt', 'rgb24', # pixel formats
# output file dir
'-y', 'tmp/frame%04d.png',
])
# Create GIF
out_file_name = os.path.basename(load_path) + '_converted.gif'
ex = subprocess.Popen("gifski --fps " + fps + " -o " +
os.path.join(save_path, out_file_name) +
" tmp/frame*.png",
stdout=subprocess.PIPE,
universal_newlines=True,
shell=True)
while ex.poll() == None:
yield ex.stdout.readline()
# print(ex.stdout.readline())
status = ex.wait()
subprocess.call(['rm', '-rf', 'tmp/'])
return True
if __name__ == "__main__":
gif_converter()