FFMpegStoryBoardProcess.java
7.5 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.metadata.MetaDataMap
* javax.jcr.RepositoryException
* org.apache.commons.io.FileUtils
* org.apache.commons.lang.BooleanUtils
* 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
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.video;
import com.day.cq.dam.api.Asset;
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.dam.video.StoryBoard;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.metadata.MetaDataMap;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.BooleanUtils;
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;
import org.slf4j.LoggerFactory;
@Component(label="Day CQ DAM FFmpeg Storyboard Process", description="Workflow process that creates storyboards from video files")
@Service
@Properties(value={@Property(name="process.label", value={"Create Video Storyboard"}, propertyPrivate=1)})
public class FFMpegStoryBoardProcess
extends AbstractFFMpegProcess {
private static final Logger log = LoggerFactory.getLogger(FFMpegStoryBoardProcess.class);
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
protected void processVideo(MetaDataMap metaData, Asset asset, File tmpFile, WorkflowSession wfSession) throws IOException, RepositoryException {
String[] args = this.buildArguments(metaData);
FFMpegWrapper wrapper = null;
File tmpWorkingDir = null;
try {
tmpWorkingDir = this.createTempDir(this.getWorkingDir());
wrapper = new FFMpegWrapper(tmpFile, tmpWorkingDir);
wrapper.setExecutableLocator(this.locator);
StoryBoard board = new StoryBoard(wrapper, asset);
for (String arg : args) {
String frameConfig;
String value = this.getValue(arg);
if (arg.startsWith("frames:")) {
board.setFrames(NumberUtils.toInt((String)value, (int)10));
continue;
}
if (arg.startsWith("start:")) {
board.setStart(NumberUtils.toInt((String)value, (int)0));
continue;
}
if (arg.startsWith("maxWidth:")) {
board.setMaxWidth(NumberUtils.toInt((String)value, (int)0));
continue;
}
if (arg.startsWith("maxHeight:")) {
board.setMaxHeight(NumberUtils.toInt((String)value, (int)0));
continue;
}
if (arg.startsWith("upScale:")) {
if (null == value) continue;
board.setUpscale(BooleanUtils.toBoolean((String)value));
continue;
}
if (!arg.startsWith("[") || !arg.endsWith("]") || !StringUtils.isNotBlank((String)(frameConfig = StringUtils.replaceEach((String)arg, (String[])new String[]{"[", "]"}, (String[])new String[]{"", ""})))) continue;
board.addFrame(frameConfig);
}
board.create();
log.info("created storyboard for video [{}]", (Object)asset.getPath());
}
finally {
try {
if (tmpWorkingDir != null) {
FileUtils.deleteDirectory((File)tmpWorkingDir);
}
}
catch (IOException e) {
log.warn("Could not delete ffmpeg's temporary working directory: {}", (Object)tmpWorkingDir.getPath());
}
}
}
private String getValue(String arg) {
String[] strings = StringUtils.split((String)arg, (String)":");
return strings.length == 2 ? strings[1] : null;
}
@Override
public String[] buildArguments(MetaDataMap metaData) {
String[] frames;
Integer maxHeight;
Integer start;
Boolean upScale;
Integer maxWidth;
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>();
Integer frameCount = (Integer)metaData.get(Arguments.FRAME_COUNT.name(), Integer.class);
if (frameCount != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.FRAME_COUNT.getArgumentPrefix()).append(frameCount);
arguments.add(builder.toString());
}
if ((start = (Integer)metaData.get(Arguments.START.name(), Integer.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.START.getArgumentPrefix()).append(start);
arguments.add(builder.toString());
}
if ((maxWidth = (Integer)metaData.get(Arguments.MAX_WIDTH.name(), Integer.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.MAX_WIDTH.getArgumentPrefix()).append(maxWidth);
arguments.add(builder.toString());
}
if ((maxHeight = (Integer)metaData.get(Arguments.MAX_HEIGHT.name(), Integer.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.MAX_HEIGHT.getArgumentPrefix()).append(maxHeight);
arguments.add(builder.toString());
}
if ((upScale = (Boolean)metaData.get(Arguments.UPSCALE.name(), Boolean.class)) != null) {
StringBuilder builder = new StringBuilder();
builder.append(Arguments.UPSCALE.getArgumentPrefix()).append(upScale);
arguments.add(builder.toString());
}
if ((frames = (String[])metaData.get(Arguments.FRAMES.name(), String[].class)) != null) {
for (String frame : frames) {
if (!frame.startsWith("[")) {
frame = "[" + frame;
}
if (!frame.endsWith("]")) {
frame = frame + "]";
}
arguments.add(frame);
}
}
return arguments.toArray(new String[arguments.size()]);
}
public static enum Arguments {
PROCESS_ARGS("PROCESS_ARGS"),
FRAME_COUNT("frames"),
START("start"),
MAX_WIDTH("maxWidth"),
MAX_HEIGHT("maxHeight"),
UPSCALE("upScale"),
FRAMES("");
private String argumentName;
private Arguments(String argumentName) {
this.argumentName = argumentName;
}
public String getArgumentName() {
return this.argumentName;
}
public String getArgumentPrefix() {
return this.argumentName + ":";
}
}
}