-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMediaHelper.java
More file actions
448 lines (391 loc) · 16 KB
/
MediaHelper.java
File metadata and controls
448 lines (391 loc) · 16 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package com.github.stickerifier.stickerify.media;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_DURATION_SECONDS;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_ANIMATION_FRAME_RATE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_IMAGE_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_SIDE_LENGTH;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_DURATION_MILLIS;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_FRAMES;
import static com.github.stickerifier.stickerify.media.MediaConstraints.VP9_CODEC;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.github.stickerifier.stickerify.exception.CorruptedVideoException;
import com.github.stickerifier.stickerify.exception.FileOperationException;
import com.github.stickerifier.stickerify.exception.MediaException;
import com.github.stickerifier.stickerify.exception.MediaOptimizationException;
import com.github.stickerifier.stickerify.exception.ProcessException;
import com.github.stickerifier.stickerify.process.PathLocator;
import com.github.stickerifier.stickerify.process.ProcessHelper;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.sksamuel.scrimage.ImmutableImage;
import com.sksamuel.scrimage.webp.WebpWriter;
import org.apache.tika.Tika;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.process.ProcessLocator;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.zip.GZIPInputStream;
public final class MediaHelper {
static {
System.setProperty("java.awt.headless", "true");
ImageIO.setUseCache(false);
}
private static final Logger LOGGER = LoggerFactory.getLogger(MediaHelper.class);
private static final Tika TIKA = new Tika();
private static final Gson GSON = new Gson();
static final ProcessLocator FFMPEG_LOCATOR = new PathLocator();
private static final int PRESERVE_ASPECT_RATIO = -2;
private static final List<String> SUPPORTED_VIDEOS = List.of("image/gif", "video/quicktime", "video/webm",
"video/mp4", "video/x-m4v", "application/x-matroska", "video/x-msvideo");
/**
* Based on the type of passed-in file, it converts it into the proper media.
* If no conversion was needed, {@code null} is returned.
*
* @param inputFile the file to convert
* @return a resized and converted file
* @throws MediaException if the file is not supported or if the conversion failed
*/
public static File convert(File inputFile) throws MediaException {
var mimeType = detectMimeType(inputFile);
try {
if (isSupportedVideo(mimeType)) {
return convertToWebm(inputFile);
}
if (isAnimatedStickerCompliant(inputFile, mimeType)) {
LOGGER.atInfo().log("The animated sticker doesn't need conversion");
return null;
}
var image = toImage(inputFile);
if (image != null) {
boolean isFileSizeCompliant = isFileSizeLowerThan(inputFile, MAX_IMAGE_FILE_SIZE);
return convertToWebp(image, mimeType, isFileSizeCompliant);
}
} catch (MediaException e) {
LOGGER.atWarn().setCause(e).log("The file with {} MIME type could not be converted", mimeType);
throw e;
}
throw new MediaException("The file with {} MIME type is not supported", mimeType);
}
/**
* Analyzes the file to detect its media type.
*
* @param file the file sent to the bot
* @return the MIME type of the passed-in file
*/
private static String detectMimeType(File file) {
String mimeType = null;
try {
mimeType = TIKA.detect(file);
LOGGER.atDebug().log("The file has {} MIME type", mimeType);
} catch (IOException _) {
LOGGER.atError().log("Unable to retrieve MIME type for file {}", file.getName());
}
return mimeType;
}
/**
* Checks if the file is a {@code gzip} archive, then it reads its content and verifies if it's a valid JSON.
* Once JSON information is retrieved, they are validated against Telegram's requirements.
*
* @param file the file to check
* @param mimeType the MIME type of the file
* @return {@code true} if the file is compliant
* @throws FileOperationException if an error occurred retrieving the size of the file
*/
private static boolean isAnimatedStickerCompliant(File file, String mimeType) throws FileOperationException {
if ("application/gzip".equals(mimeType)) {
var uncompressedContent = "";
try (var gzipInputStream = new GZIPInputStream(new FileInputStream(file))) {
uncompressedContent = new String(gzipInputStream.readAllBytes(), UTF_8);
} catch (IOException _) {
LOGGER.atError().log("Unable to retrieve gzip content from file {}", file.getName());
}
try {
var sticker = GSON.fromJson(uncompressedContent, AnimationDetails.class);
boolean isAnimationCompliant = isAnimationCompliant(sticker);
if (isAnimationCompliant) {
return isFileSizeLowerThan(file, MAX_ANIMATION_FILE_SIZE);
}
LOGGER.atWarn().log("The {} doesn't meet Telegram's requirements", sticker);
} catch (JsonSyntaxException _) {
LOGGER.atInfo().log("The archive isn't an animated sticker");
}
}
return false;
}
private record AnimationDetails(@SerializedName("w") int width, @SerializedName("h") int height, @SerializedName("fr") int frameRate, @SerializedName("ip") float start, @SerializedName("op") float end) {
private float duration() {
return (end - start) / frameRate;
}
@NotNull
@Override
public String toString() {
return "animated sticker [" +
"width=" + width +
", height=" + height +
", frameRate=" + frameRate +
", duration=" + duration() +
']';
}
}
/**
* Checks if passed-in animation is already compliant with Telegram's requisites.
* If so, conversion won't take place and no file will be returned to the user.
*
* @param animation the animation to check
* @return {@code true} if the animation is compliant
*/
private static boolean isAnimationCompliant(AnimationDetails animation) {
return animation != null
&& animation.frameRate() <= MAX_ANIMATION_FRAME_RATE
&& animation.duration() <= MAX_ANIMATION_DURATION_SECONDS
&& animation.width() == MAX_SIDE_LENGTH
&& animation.height() == MAX_SIDE_LENGTH;
}
/**
* Checks that passed-in file's size does not exceed the specified threshold.
*
* @param file the file to check
* @param threshold max allowed file size
* @return {@code true} if file's size is compliant
* @throws FileOperationException if an error occurred retrieving the size of the file
*/
private static boolean isFileSizeLowerThan(File file, long threshold) throws FileOperationException {
try {
return Files.size(file.toPath()) <= threshold;
} catch (IOException e) {
throw new FileOperationException(e);
}
}
/**
* Retrieve the image from the passed-in file.
* If the file isn't a supported image, {@code null} is returned.
*
* @param file the file to read
* @return the image, if supported by {@link ImageIO}
*/
private static ImmutableImage toImage(File file) {
try (var inputStream = new FileInputStream(file)) {
return ImmutableImage.loader().fromStream(inputStream);
} catch (IOException _) {
return null;
}
}
/**
* Checks if the MIME type corresponds to one of the supported video formats.
*
* @param mimeType the MIME type to check
* @return {@code true} if the MIME type is supported
*/
private static boolean isSupportedVideo(String mimeType) {
return SUPPORTED_VIDEOS.stream().anyMatch(format -> format.equals(mimeType));
}
/**
* Given an image file, it converts it to a WebP file of the proper dimension (max 512 x 512).
*
* @param image the image to convert to WebP
* @param mimeType the MIME type of the file
* @param isFileSizeCompliant {@code true} if the file does not exceed Telegram's limit
* @return converted image, {@code null} if no conversion was needed
* @throws MediaException if an error occurred processing passed-in image
*/
private static File convertToWebp(ImmutableImage image, String mimeType, boolean isFileSizeCompliant) throws MediaException {
if (isImageCompliant(image, mimeType) && isFileSizeCompliant) {
LOGGER.atInfo().log("The image doesn't need conversion");
return null;
}
return createWebpFile(image);
}
/**
* Checks if passed-in image is already compliant with Telegram's requisites.
* If so, conversion won't take place and no file will be returned to the user.
*
* @param image the image to check
* @param mimeType the MIME type of the file
* @return {@code true} if the file is compliant
*/
private static boolean isImageCompliant(ImmutableImage image, String mimeType) {
return ("image/png".equals(mimeType) || "image/webp".equals(mimeType)) && isSizeCompliant(image.width, image.height);
}
/**
* Checks that either width or height is 512 pixels
* and the other is 512 pixels or fewer.
*
* @param width the width to be checked
* @param height the width to be checked
* @return {@code true} if the video has valid dimensions
*/
private static boolean isSizeCompliant(int width, int height) {
return (width == MAX_SIDE_LENGTH && height <= MAX_SIDE_LENGTH) || (height == MAX_SIDE_LENGTH && width <= MAX_SIDE_LENGTH);
}
/**
* Creates a new <i>.webp</i> file from passed-in {@code image}, resizing it with sides of max 512 pixels each.
*
* @param image the image to convert to WebP
* @return converted image
* @throws MediaException if an error occurred creating the temp file, or
* if the image size could not be reduced enough to meet Telegram's requirements
*/
private static File createWebpFile(ImmutableImage image) throws MediaException {
var webpImage = createTempFile("webp");
var deleteTempFile = false;
try {
image.max(MAX_SIDE_LENGTH, MAX_SIDE_LENGTH).output(WebpWriter.MAX_LOSSLESS_COMPRESSION, webpImage);
if (!isFileSizeLowerThan(webpImage, MAX_IMAGE_FILE_SIZE)) {
deleteTempFile = true;
throw new MediaOptimizationException("The image size could not be reduced enough to meet Telegram's requirements");
}
} catch (IOException e) {
deleteTempFile = true;
throw new FileOperationException("An unexpected error occurred trying to create resulting image", e);
} finally {
if (deleteTempFile) {
deleteFile(webpImage);
}
}
return webpImage;
}
/**
* Creates a new temp file with the desired extension.
*
* @param fileExtension the extension of the new file
* @return a new temp file
* @throws FileOperationException if an error occurs creating the temp file
*/
private static File createTempFile(String fileExtension) throws FileOperationException {
try {
return File.createTempFile("Stickerify-", "." + fileExtension);
} catch (IOException e) {
throw new FileOperationException("An error occurred creating a new temp file", e);
}
}
/**
* Deletes passed-in {@code file}.
*
* @param file the file to be removed
* @throws FileOperationException if an error occurs deleting the file
*/
private static void deleteFile(File file) throws FileOperationException {
try {
if (!Files.deleteIfExists(file.toPath())) {
LOGGER.atInfo().log("Unable to delete file {}", file.toPath());
}
} catch (IOException e) {
throw new FileOperationException("An error occurred deleting the file", e);
}
}
/**
* Given a video file, it converts it to a WebM file of the proper dimension (max 512 x 512),
* based on the requirements specified by <a href="https://core.telegram.org/stickers/webm-vp9-encoding">Telegram documentation</a>.
*
* @param file the file to convert
* @return converted video, {@code null} if no conversion was needed
* @throws MediaException if file conversion is not successful
*/
private static File convertToWebm(File file) throws MediaException {
var mediaInfo = retrieveMultimediaInfo(file);
if (isVideoCompliant(file, mediaInfo)) {
LOGGER.atInfo().log("The video doesn't need conversion");
return null;
}
return convertWithFfmpeg(file, mediaInfo);
}
/**
* Convenience method to retrieve multimedia information of a file.
*
* @param file the video to check
* @return passed-in video's multimedia information
* @throws CorruptedVideoException if an error occurred retrieving video information
*/
private static MultimediaInfo retrieveMultimediaInfo(File file) throws CorruptedVideoException {
try {
return new MultimediaObject(file, FFMPEG_LOCATOR).getInfo();
} catch (EncoderException e) {
throw new CorruptedVideoException("The video could not be processed successfully", e);
}
}
/**
* Checks if passed-in file is already compliant with Telegram's requisites.
* If so, conversion won't take place and no file will be returned to the user.
*
* @param file the file to check
* @param mediaInfo video's multimedia information
* @return {@code true} if the file is compliant
* @throws FileOperationException if an error occurred retrieving the size of the file
*/
private static boolean isVideoCompliant(File file, MultimediaInfo mediaInfo) throws FileOperationException {
var videoInfo = mediaInfo.getVideo();
var videoSize = videoInfo.getSize();
return isSizeCompliant(videoSize.getWidth(), videoSize.getHeight())
&& videoInfo.getFrameRate() <= MAX_VIDEO_FRAMES
&& videoInfo.getDecoder().startsWith(VP9_CODEC)
&& mediaInfo.getDuration() > 0L
&& mediaInfo.getDuration() <= MAX_VIDEO_DURATION_MILLIS
&& mediaInfo.getAudio() == null
&& MATROSKA_FORMAT.equals(mediaInfo.getFormat())
&& isFileSizeLowerThan(file, MAX_VIDEO_FILE_SIZE);
}
/**
* Converts the passed-in file using FFmpeg applying Telegram's video stickers' constraints.
*
* @param file the file to convert
* @param mediaInfo video's multimedia information
* @return converted video
* @throws MediaException if file conversion is not successful
*/
private static File convertWithFfmpeg(File file, MultimediaInfo mediaInfo) throws MediaException {
var webmVideo = createTempFile("webm");
var videoDetails = getResultingVideoDetails(mediaInfo);
var ffmpegCommand = new String[] {
"ffmpeg",
"-v", "error",
"-i", file.getAbsolutePath(),
"-vf", "scale=" + videoDetails.width() + ":" + videoDetails.height() + ",fps=" + videoDetails.frameRate(),
"-c:v", "libvpx-" + VP9_CODEC,
"-b:v", "256k",
"-crf", "32",
"-g", "60",
"-an",
"-t", videoDetails.duration(),
"-y", webmVideo.getAbsolutePath()
};
try {
ProcessHelper.executeCommand(ffmpegCommand);
} catch (ProcessException e) {
deleteFile(webmVideo);
throw new MediaException(e.getMessage());
}
return webmVideo;
}
/**
* Convenience method to group resulting video's details,
* calculated checking passed-in media info against Telegram's constraints.
*
* @param mediaInfo video's multimedia information
* @return resulting video's details
*/
private static ResultingVideoDetails getResultingVideoDetails(MultimediaInfo mediaInfo) {
var videoInfo = mediaInfo.getVideo();
float frameRate = Math.min(videoInfo.getFrameRate(), MAX_VIDEO_FRAMES);
long duration = Math.min(mediaInfo.getDuration(), MAX_VIDEO_DURATION_MILLIS) / 1_000L;
boolean isWidthBigger = videoInfo.getSize().getWidth() >= videoInfo.getSize().getHeight();
int width = isWidthBigger ? MAX_SIDE_LENGTH : PRESERVE_ASPECT_RATIO;
int height = isWidthBigger ? PRESERVE_ASPECT_RATIO : MAX_SIDE_LENGTH;
return new ResultingVideoDetails(width, height, frameRate, String.valueOf(duration));
}
private record ResultingVideoDetails(int width, int height, float frameRate, String duration) {}
private MediaHelper() {
throw new UnsupportedOperationException();
}
}