forked from shawnrivers/gif-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin.py
265 lines (213 loc) · 8.71 KB
/
win.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
255
256
257
258
259
260
261
262
263
264
265
from moviepy.editor import *
import sys
import os
import numpy as np
min_fps = 10
min_colors = 40
min_dimension = 160
limit_size = 1000000
# FUNCTIONS DEFINATION
# Get the width of the clip.
def getClipWidth(clip):
# Get the first frame of the clip.
frame = clip.get_frame(0)
return np.size(frame, 1)
# Get the height of the clip.
def getClipHeight(clip):
# Get the first frame of the clip.
frame = clip.get_frame(0)
return np.size(frame, 0)
# Tell the side with the least pixels ("width"/"height").
def getClipSide(clip):
# Return the dimension with the smallest value.
if (getClipWidth(clip) < getClipHeight(clip)):
return 'width'
else:
return 'height'
# Get the dimension of the smallest side.
def getClipDimension(clip):
return min(getClipWidth(clip), getClipHeight(clip))
# Get the total frames count of the clip.
def getClipFramesCount(clip):
return int(clip.fps * clip.duration)
# Get the size of a file.
def getFileSize(path):
return os.path.getsize(path)
# Show the info of the original clip.
def showOrigInfo(width, height, framesCount, fps, duration, colors, size):
print(' Dimension: %d * %d' % (width, height))
print(' Frames Count: %(fr)d (%(fps)d fps * %(du).2f s)' %
{'fr': framesCount, 'fps': fps, 'du': duration})
print(' File Size: %d KB\n' % (size / 1000))
# Show the changes after compression.
def showChangedInfo(width, height, framesCount, fps, duration, colors, size,
orig_width, orig_height, orig_framesCount,
orig_fps, orig_duration, orig_size):
print(' Dimension: %(orig_wid)d * %(orig_hei)d -> %(curr_wid)d * %(curr_hei)d' %
{'curr_wid': width, 'curr_hei': height, 'orig_wid': orig_width, 'orig_hei': orig_height})
print(' Frames Count: %(orig_fr)d (%(orig_fps)d fps * %(orig_du).2f s) -> %(curr_fr)d (%(curr_fps)d fps * %(curr_du).2f s)' %
{'orig_fr': orig_framesCount, 'orig_fps': orig_fps, 'orig_du': orig_duration,
'curr_fr': framesCount, 'curr_fps': fps, 'curr_du': duration})
print(' Colors Count: %d' % colors)
print(' Size: %(orig)d KB -> %(curr)d KB\n' %
{'orig': (orig_size / 1000), 'curr': (size / 1000)})
# Compress the clip.
def compressClip(clip_path):
# Output file name and path setting.
separatePaths = clip_path.split('\\')
output_filename = separatePaths[-1].split('.')[0] + '.gif'
# output_path = os.path.join(os.getcwd(), 'output\\', output_filename)
output_folder = os.path.join(os.getcwd(), 'output\\')
temp_path = os.path.join(output_folder, output_filename)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
clip = VideoFileClip(clip_path)
# Store original clip information
shortest_side = getClipSide(clip)
original_dimension = getClipDimension(clip)
original_width = getClipWidth(clip)
original_height = getClipHeight(clip)
original_fps = clip.fps
original_duration = clip.duration
original_framesCount = getClipFramesCount(clip)
original_size = getFileSize(clip_path)
print('\nOriginal Info:')
showOrigInfo(original_width, original_height, original_framesCount,
original_fps, original_duration, 0, original_size)
# PRE-COMPRESSION
# Change color count.
current_colorsCount = 64
# Set a variable for changing dimension.
if original_dimension > 300:
current_dimension = 300
else:
current_dimension = original_dimension
# Change dimension based on the shortest side.
if shortest_side == 'width':
temp_clip = clip.resize(width=current_dimension)
else:
temp_clip = clip.resize(height=current_dimension)
# Change fps.
if original_fps > 15:
current_fps = 15
else:
current_fps = original_fps
# Compress to a gif file.
temp_clip.write_gif(temp_path, fps=current_fps, program='ffmpeg',
colors=current_colorsCount, tempfiles=True)
temp_clip = VideoFileClip(temp_path)
current_size = getFileSize(temp_path)
current_framesCount = getClipFramesCount(temp_clip)
current_duration = temp_clip.duration
print('\n\n1-time compression finished.')
showChangedInfo(getClipWidth(temp_clip), getClipHeight(temp_clip),
current_framesCount, temp_clip.fps, current_duration,
current_colorsCount, current_size, original_width,
original_height, original_framesCount, original_fps,
original_duration, original_size)
# COMPRESSION
compression_counter = 1
real_counter = 1
while True:
if (current_size < limit_size) or (current_fps <= min_fps and current_dimension <= min_dimension and current_colorsCount <= min_colors):
# os.rename(temp_path, output_path)
print('Ouput file saved to %s\n' % temp_path)
break
# Compression settings
if compression_counter == 0:
if original_dimension > 300:
current_dimension = 300
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 1:
if original_dimension > 260:
current_dimension = 260
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 2:
if original_fps > 12:
current_fps = 12
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 3:
current_colorsCount = 56
real_counter += 1
compression_counter += 1
elif compression_counter == 4:
if original_dimension > 220:
current_dimension = 220
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 5:
current_colorsCount = 48
real_counter += 1
compression_counter += 1
elif compression_counter == 6:
if original_dimension > 200:
current_dimension = 200
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 7:
current_colorsCount = 40
real_counter += 1
compression_counter += 1
elif compression_counter == 8:
if original_fps > 10:
current_fps = 10
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
elif compression_counter == 9:
if original_dimension > 160:
current_dimension = 160
real_counter += 1
compression_counter += 1
else:
compression_counter += 1
continue
# Execute the compression
# Change dimension based on the shortest side.
if shortest_side == 'width':
temp_clip = clip.resize(width=current_dimension)
else:
temp_clip = clip.resize(height=current_dimension)
# Compress to a gif file.
temp_clip.write_gif(temp_path, fps=current_fps, program='ffmpeg',
colors=current_colorsCount, tempfiles=True)
temp_clip = VideoFileClip(temp_path)
current_size = getFileSize(temp_path)
current_framesCount = getClipFramesCount(temp_clip)
current_duration = temp_clip.duration
print('\n\n%d-time compression finished.' % (real_counter))
showChangedInfo(getClipWidth(temp_clip), getClipHeight(temp_clip),
current_framesCount, temp_clip.fps, current_duration,
current_colorsCount, current_size, original_width,
original_height, original_framesCount, original_fps,
original_duration, original_size)
# MAIN EXECUTION
files_count = len(sys.argv) - 1
for i in range(files_count):
clip_path = str(sys.argv[i + 1])
print('\n----------------------------------------------\nCurrent job: %s' % clip_path)
print('\nOverall progress: %(current)d/%(overall)d Started.' %
{'current': (i + 1), 'overall': files_count})
compressClip(clip_path)
print('\nOverall progress: %(current)d/%(overall)d Finished.' %
{'current': (i + 1), 'overall': files_count})