FFMpegThumbnailProcess.java
7.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.commons.thumbnail.ThumbnailGenerator
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.RepositoryException
* org.apache.commons.io.FileUtils
* org.apache.commons.lang.ArrayUtils
* org.apache.commons.lang.StringUtils
* org.apache.commons.lang.math.NumberUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.slf4j.Logger
*/
package com.day.cq.dam.video;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.thumbnail.ThumbnailGenerator;
import com.day.cq.dam.handler.ffmpeg.ExecutableLocator;
import com.day.cq.dam.handler.ffmpeg.FFMpegWrapper;
import com.day.cq.dam.video.AbstractFFMpegProcess;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
@Component(label="Day CQ DAM FFmpeg Thumbnail Process", description="Workflow process that creates thumbnails from video files")
@Service
@Properties(value={@Property(name="process.label", value={"Create Video Thumbnails"}, propertyPrivate=1)})
public class FFMpegThumbnailProcess
extends AbstractFFMpegProcess {
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
protected void processVideo(MetaDataMap metaData, Asset asset, File tmpFile, WorkflowSession wfSession) throws IOException, RepositoryException {
block20 : {
Object[] args = this.buildArguments(metaData);
int start = 0;
int count = 1;
int index = 0;
int i = 0;
while (i < args.length) {
String arg = args[i];
if (arg.startsWith("start:") || arg.startsWith("count:") || arg.startsWith("index:")) {
String[] split = StringUtils.split((String)arg, (String)":");
if (split.length == 2) {
if (arg.startsWith("start:")) {
start = NumberUtils.toInt((String)split[1], (int)start);
} else if (arg.startsWith("count:")) {
if ((count = NumberUtils.toInt((String)split[1], (int)count)) <= 0) {
count = 1;
}
} else if (arg.startsWith("index:")) {
index = NumberUtils.toInt((String)split[1], (int)index);
}
}
args = (String[])ArrayUtils.remove((Object[])args, (int)i);
continue;
}
++i;
}
FFMpegWrapper wrapper = null;
File tmpWorkingDir = null;
try {
tmpWorkingDir = this.createTempDir(this.getWorkingDir());
wrapper = new FFMpegWrapper(tmpFile, tmpWorkingDir);
wrapper.setExecutableLocator(this.locator);
BufferedImage[] thumbnails = wrapper.getThumbnails(count, start);
if (thumbnails != null && thumbnails.length > 0) {
BufferedImage thumbnail;
if (index >= thumbnails.length) {
index = thumbnails.length - 1;
}
if ((thumbnail = thumbnails[index]) != null) {
BufferedImage rgbaThumbnail = new BufferedImage(thumbnail.getWidth(), thumbnail.getHeight(), 2);
rgbaThumbnail.getGraphics().drawImage(thumbnail, 0, 0, null);
ThumbnailGenerator generator = new ThumbnailGenerator(asset, rgbaThumbnail);
generator.generate(ThumbnailGenerator.parseConfig((String[])args));
}
break block20;
}
this.log.warn("Could not create thumbnails for video asset {}, maybe ffmpeg is not installed.", (Object)asset.getPath());
}
finally {
try {
if (tmpWorkingDir != null) {
FileUtils.deleteDirectory((File)tmpWorkingDir);
}
}
catch (IOException e) {
this.log.warn("Could not delete ffmpeg's temporary working directory: {}", (Object)tmpWorkingDir.getPath());
}
}
}
}
@Override
public String[] buildArguments(MetaDataMap metaData) {
String count;
String[] configs;
String index;
String processArgs = (String)metaData.get(Arguments.PROCESS_ARGS.name(), String.class);
if (processArgs != null && !processArgs.equals("")) {
return processArgs.split(",");
}
ArrayList<String> arguments = new ArrayList<String>();
String start = (String)metaData.get(Arguments.START.name(), String.class);
if (start != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.START.getArgumentPrefix()).append(start);
arguments.add(builder.toString());
}
if ((count = (String)metaData.get(Arguments.COUNT.name(), String.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.COUNT.getArgumentPrefix()).append(count);
arguments.add(builder.toString());
}
if ((index = (String)metaData.get(Arguments.INDEX.name(), String.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.INDEX.getArgumentPrefix()).append(index);
arguments.add(builder.toString());
}
if ((configs = (String[])metaData.get(Arguments.CONFIGS.name(), String[].class)) != null) {
for (String config : configs) {
arguments.add(config);
}
}
return arguments.toArray(new String[arguments.size()]);
}
public static enum Arguments {
PROCESS_ARGS("PROCESS_ARGS"),
START("start"),
COUNT("count"),
INDEX("index"),
CONFIGS("CONFIGS");
private String argumentName;
private Arguments(String argumentName) {
this.argumentName = argumentName;
}
public String getArgumentName() {
return this.argumentName;
}
public String getArgumentPrefix() {
return this.argumentName + ":";
}
}
}