VideoClipServlet.java
7.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* javax.servlet.ServletException
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.osgi.framework.BundleContext
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.video.servlet;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.handler.ffmpeg.ExecutableLocator;
import com.day.cq.dam.handler.ffmpeg.FFMpegWrapper;
import com.day.cq.dam.handler.ffmpeg.FfmpegNotFoundException;
import com.day.cq.dam.video.servlet.VideoProfileListServlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Dictionary;
import javax.servlet.ServletException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
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.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.selectors", value={"videoclip"}), @Property(name="sling.servlet.methods", value={"POST"}), @Property(name="service.description", value={"Servlet to clip video."})})
public class VideoClipServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -780464494271946961L;
private final Logger log = LoggerFactory.getLogger(VideoProfileListServlet.class);
private File workingDir;
@Reference(policy=ReferencePolicy.STATIC)
protected ExecutableLocator locator;
@Property(value={"./logs/ffmpeg"})
public static final String PROP_WORKING_DIR = "ffmpeg.workingdir";
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Resource resource = request.getResource();
Asset asset = null;
if (null != resource) {
asset = (Asset)resource.adaptTo(Asset.class);
}
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
FFMpegWrapper wrapper = null;
File tmpWorkingDir = null;
File tmpDir = null;
FileOutputStream fos = null;
InputStream is = null;
try {
tmpDir = File.createTempFile("cqdam", null);
tmpDir.delete();
tmpDir.mkdir();
tmpWorkingDir = this.createTempDir(this.getWorkingDir());
File tmpFile = new File(tmpDir, asset.getName().replace(' ', '_'));
fos = new FileOutputStream(tmpFile);
is = asset.getOriginal().getStream();
IOUtils.copy((InputStream)is, (OutputStream)fos);
wrapper = new FFMpegWrapper(tmpFile, tmpWorkingDir);
wrapper.setExecutableLocator(this.locator);
File clippedFile = wrapper.getClip(Double.parseDouble(startTime), Double.parseDouble(endTime), asset.getMimeType());
FileInputStream fis = new FileInputStream(clippedFile);
String roundStartTime = startTime.substring(0, startTime.indexOf("."));
String roundEndTime = endTime.substring(0, endTime.indexOf("."));
String mimeType = asset.getMimeType();
String outputFormat = mimeType.substring(mimeType.indexOf("/") + 1);
asset.addRendition("cq5dam.clipped." + roundStartTime + "." + roundEndTime + "." + outputFormat, (InputStream)fis, asset.getMimeType());
}
catch (IOException e) {}
catch (FfmpegNotFoundException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
finally {
IOUtils.closeQuietly((InputStream)is);
IOUtils.closeQuietly((OutputStream)fos);
try {
if (tmpDir != null) {
FileUtils.deleteDirectory((File)tmpDir);
}
}
catch (IOException e) {
this.log.error("Could not delete temp directory: {}", (Object)tmpDir.getPath());
}
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());
}
}
}
public File getWorkingDir() {
this.workingDir.mkdir();
return this.workingDir;
}
private File resolveWorkingDir(String slingHome, String path) {
File workingDir;
if (path == null) {
path = "";
}
if (!(workingDir = new File(path = path.replace('/', File.separatorChar))).isAbsolute()) {
File baseDir = new File(slingHome == null ? "" : slingHome).getAbsoluteFile();
workingDir = new File(baseDir, path).getAbsoluteFile();
}
try {
this.log.info("ffmpeg working directory: {}", (Object)workingDir.getCanonicalPath());
}
catch (IOException e) {
this.log.info("ffmpeg working directory: {}", (Object)workingDir.getAbsolutePath());
}
return workingDir;
}
protected void activate(ComponentContext ctx) {
String slingHome = ctx.getBundleContext().getProperty("sling.home");
this.workingDir = this.resolveWorkingDir(slingHome, (String)ctx.getProperties().get("ffmpeg.workingdir"));
}
protected File createTempDir(File parentDir) {
File tempDir = null;
try {
tempDir = File.createTempFile("cqdam", null, parentDir);
tempDir.delete();
tempDir.mkdir();
}
catch (IOException e) {
this.log.warn("could not create temp directory in the [{}] with the exception", (Object)parentDir, (Object)e);
}
return tempDir;
}
protected void bindLocator(ExecutableLocator executableLocator) {
this.locator = executableLocator;
}
protected void unbindLocator(ExecutableLocator executableLocator) {
if (this.locator == executableLocator) {
this.locator = null;
}
}
}