Some more work on movies.

This commit is contained in:
Knut Forkalsrud 2011-06-22 15:10:12 -07:00
parent 1b642ddab9
commit 74f08f44b0
3 changed files with 45 additions and 12 deletions

View file

@ -16,8 +16,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.forkalsrud.album.db.MovieDatabase;
import org.forkalsrud.album.db.Chunk; import org.forkalsrud.album.db.Chunk;
import org.forkalsrud.album.db.MovieDatabase;
import org.forkalsrud.album.exif.Dimension; import org.forkalsrud.album.exif.Dimension;
import org.forkalsrud.album.exif.Thumbnail; import org.forkalsrud.album.exif.Thumbnail;
import org.forkalsrud.album.web.CachedImage; import org.forkalsrud.album.web.CachedImage;
@ -64,7 +64,7 @@ public class MovieCoder {
stdin.close(); stdin.close();
InputStream stdout = p.getInputStream(); InputStream stdout = p.getInputStream();
String searchPath = IOUtils.toString(stdout); String searchPath = IOUtils.toString(stdout);
int returnStatus = p.waitFor(); p.waitFor();
String separator = System.getProperty("path.separator"); String separator = System.getProperty("path.separator");
if (searchPath != null && separator != null && !"".equals(separator)) { if (searchPath != null && separator != null && !"".equals(separator)) {
@ -100,7 +100,7 @@ public class MovieCoder {
Process p = pb.start(); Process p = pb.start();
p.getOutputStream().close(); p.getOutputStream().close();
List<String> lines = IOUtils.readLines(p.getInputStream()); List<String> lines = IOUtils.readLines(p.getInputStream());
int returnStatus = p.waitFor(); p.waitFor();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String width = "", height = ""; String width = "", height = "";
for (String line : lines) { for (String line : lines) {
@ -153,9 +153,7 @@ public class MovieCoder {
Process p = pb.start(); Process p = pb.start();
p.getOutputStream().close(); p.getOutputStream().close();
log.debug(IOUtils.toString(p.getInputStream())); log.debug(IOUtils.toString(p.getInputStream()));
int returnStatus = p.waitFor(); p.waitFor();
String key = file.getPath() + ":" + secondNo + ":" + size;
CachedImage ci = pictureScaler.scalePicture(frame, thumbnail, size); CachedImage ci = pictureScaler.scalePicture(frame, thumbnail, size);
return ci; return ci;
} finally { } finally {
@ -265,6 +263,7 @@ public class MovieCoder {
* <outfile>.mp4 * <outfile>.mp4
*/ */
@Override
public void run() { public void run() {
try { try {
@ -353,7 +352,8 @@ public class MovieCoder {
public void stream(File file, Thumbnail thumbnail, String size, OutputStream out) throws IOException, InterruptedException { public void stream(File file, Thumbnail thumbnail, String size, OutputStream out) throws IOException, InterruptedException {
Dimension targetSize = thumbnail.getSize().scale(size); Dimension targetSize = thumbnail.getSize().scale(size);
// new EncodingProcess(file, thumbnail, targetSize).streamTo(out); new EncodingProcess(file, thumbnail, targetSize).streamTo(out);
/*
String key = file.getPath() + ":" + targetSize.getWidth(); String key = file.getPath() + ":" + targetSize.getWidth();
int chunkNo = 0; int chunkNo = 0;
boolean done = false; boolean done = false;
@ -364,6 +364,7 @@ public class MovieCoder {
} }
out.write(chunk.bits); out.write(chunk.bits);
} }
*/
} }
} }

View file

@ -296,11 +296,41 @@ public class AlbumServlet
} }
void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) {
try {
File file = entry.getPath();
if (notModified(req, file)) {
res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days
log.info(file.getName() + " not modified (based on date)");
return;
}
int secondNo = 3;
String size = req.getParameter("size"); String size = req.getParameter("size");
CachedImage cimg = movieCoder.extractFrame(entry.getPath(), 3, entry.getThumbnail(), size != null ? size : "250"); if (size == null) {
size = "250";
}
String key = file.getPath() + ":" + secondNo + ":" + size;
CachedImage cimg = thumbDb.load(key);
if (cimg != null) {
if (cimg.lastModified == file.lastModified()) {
log.info("cache hit on " + key);
} else {
log.info(" " + key + " has changed so cache entry wil be refreshed");
cimg = null;
}
}
if (cimg == null) {
try {
cimg = movieCoder.extractFrame(file, secondNo, entry.getThumbnail(), size);
thumbDb.store(key, cimg);
log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries");
} catch (Exception e) {
throw new RuntimeException("sadness", e);
}
}
try {
res.setStatus(HttpServletResponse.SC_OK); res.setStatus(HttpServletResponse.SC_OK);
res.setDateHeader("Last-Modified", entry.getPath().lastModified()); res.setDateHeader("Last-Modified", file.lastModified());
res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days
res.setContentType(cimg.mimeType); res.setContentType(cimg.mimeType);
res.setContentLength(cimg.bits.length); res.setContentLength(cimg.bits.length);

View file

@ -86,6 +86,7 @@ public class PictureScaler {
} }
@Override
public CachedImage call() throws Exception { public CachedImage call() throws Exception {
return scalePictureReally(file, thumbnail, size); return scalePictureReally(file, thumbnail, size);
} }
@ -108,6 +109,7 @@ public class PictureScaler {
return new Comparator<PictureRequest>() { return new Comparator<PictureRequest>() {
@Override
public int compare(PictureRequest o1, PictureRequest o2) { public int compare(PictureRequest o1, PictureRequest o2) {
return Long.signum(o1.priority - o2.priority); return Long.signum(o1.priority - o2.priority);
} }