diff --git a/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java b/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java deleted file mode 100644 index efac8ea..0000000 --- a/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.forkalsrud.album.video; - -public interface EncodingProcessListener { - - void chunkAvailable(int chunkNo); - - void codingFinished(int lastChunkNo); - -} \ No newline at end of file diff --git a/src/main/java/org/forkalsrud/album/video/MovieCoder.java b/src/main/java/org/forkalsrud/album/video/MovieCoder.java index 890b1b5..5b065e4 100644 --- a/src/main/java/org/forkalsrud/album/video/MovieCoder.java +++ b/src/main/java/org/forkalsrud/album/video/MovieCoder.java @@ -24,7 +24,7 @@ public class MovieCoder { private String ffmpegExecutable; private String mplayerExecutable; private PictureScaler pictureScaler; - private HashMap currentEncodings = new HashMap(); + private HashMap currentEncodings = new HashMap<>(); public MovieCoder(PictureScaler pictureScaler) { this.pictureScaler = pictureScaler; @@ -52,7 +52,7 @@ public class MovieCoder { return temp; } - public void deleteTempDirectory(File dir) { + private void deleteTempDirectory(File dir) { for (File sub : dir.listFiles()) { if (sub.isDirectory()) { deleteTempDirectory(sub); @@ -74,8 +74,7 @@ public class MovieCoder { p.getOutputStream().close(); log.debug(IOUtils.toString(p.getInputStream())); p.waitFor(); - CachedImage ci = pictureScaler.scalePicture(frame, thumbnail, size); - return ci; + return pictureScaler.scalePicture(frame, thumbnail, size); } finally { deleteTempDirectory(tmpDir); } @@ -107,7 +106,6 @@ public class MovieCoder { private final int chunkSize = 4 * 65536; private File file; private Dimension targetSize; - private ArrayList listeners = new ArrayList(); private Chunk currentChunk = null; private int chunkPos; private int remainingCapacity; @@ -138,7 +136,7 @@ public class MovieCoder { * http://rob.opendot.cl/index.php/useful-stuff/ffmpeg-x264-encoding-guide/ * http://rob.opendot.cl/index.php/useful-stuff/encoding-a-flv-video-for-embedded-web-playback/ * - * Assuming video in is 1280x720 pixels resolution and we want to show 640x360 + * Assuming video in is 1280x720 pixels resolution, and we want to show 640x360, * and we want an output bitrate about 1 Mbit/sec * we can do a one pass encoding like this: * @@ -174,25 +172,7 @@ public class MovieCoder { try { - ArrayList command = new ArrayList(); - command.add(ffmpegExecutable); - command.add("-i"); - command.add(file.getAbsolutePath()); - command.add("-s"); - command.add(targetSize.getWidth() + "x" + targetSize.getHeight()); - command.add("-crf"); - command.add("30"); - command.add("-acodec"); command.add("libmp3lame"); - command.add("-ar"); command.add("22050"); - command.add("-vcodec"); command.add("libx264"); - command.add("-g"); command.add("150"); - if (vf != null) { - command.add("-vf"); - command.add(vf); - } - command.add("-f"); command.add("flv"); - command.add("-"); - ProcessBuilder pb = new ProcessBuilder(command); + ProcessBuilder pb = createTranscoderProcess(vf); log.info(pb.command().toString()); pb.redirectErrorStream(false); @@ -228,7 +208,35 @@ public class MovieCoder { notifyListeners(chunkAvailable); } } - + + private ProcessBuilder createTranscoderProcess(String vf) { + ArrayList command = new ArrayList<>(); + command.add(ffmpegExecutable); + command.add("-i"); + command.add(file.getAbsolutePath()); + command.add("-s"); + command.add(targetSize.getWidth() + "x" + targetSize.getHeight()); + command.add("-crf"); + command.add("30"); + command.add("-acodec"); + command.add("libmp3lame"); + command.add("-ar"); + command.add("22050"); + command.add("-vcodec"); + command.add("libx264"); + command.add("-g"); + command.add("150"); + if (vf != null) { + command.add("-vf"); + command.add(vf); + } + command.add("-f"); + command.add("flv"); + command.add("-"); + ProcessBuilder pb = new ProcessBuilder(command); + return pb; + } + /** * @return number of chunks available. * -Integer.MAX_VALUE if something went very wrong @@ -257,7 +265,7 @@ public class MovieCoder { int len = data.length; Chunk chunk0 = new Chunk(fileTimestamp, len, 0); chunk0.bits = data; - log.info("Writing " + dbKey + " seq 0 (" + chunkInProgress + ") " + data.length); + log.info("Writing {} seq 0 ({}) {}", dbKey, chunkInProgress, data.length); movieDb.store(dbKey, 0, chunk0); notifyListeners(chunkInProgress); } @@ -297,9 +305,9 @@ public class MovieCoder { private void endChunk() { - log.info("store chunk " + chunkInProgress); + log.info("store chunk {}", chunkInProgress); movieDb.store(dbKey, chunkInProgress, currentChunk); - log.info("Writing " + dbKey + " seq " + chunkInProgress + " (" + chunkInProgress + ") " + currentChunk.bits.length); + log.info("Writing {} seq {} ({}) {}", dbKey, chunkInProgress, chunkInProgress, currentChunk.bits.length); currentChunk = null; notifyListeners(chunkInProgress); } @@ -313,17 +321,9 @@ public class MovieCoder { Chunk last = new Chunk(fileTimestamp, chunkPos, 0); System.arraycopy(currentChunk.bits, 0, last.bits, 0, chunkPos); movieDb.store(dbKey, chunkInProgress, last); - log.info("Writing " + dbKey + " seq " + chunkInProgress + " (" + chunkInProgress + ") " + last.bits.length); + log.info("Writing {} seq {} ({}) {}", dbKey, chunkInProgress, chunkInProgress, last.bits.length); currentChunk = null; } - - public synchronized void addListener(EncodingProcessListener videoStreamer) { - listeners.add(videoStreamer); - } - - public synchronized void removeListener(VideoStreamer videoStreamer) { - listeners.remove(videoStreamer); - } } class ErrorStreamPumper implements Runnable { @@ -382,7 +382,7 @@ public class MovieCoder { // If the file has been modified since our last encoding job finished if (chunk != null && ep == null) { if (chunk.timestamp != file.lastModified()) { - log.info(" " + key + " has changed so cache entry wil be refreshed"); + log.info(" {} has changed so cache entry wil be refreshed", key); movieDb.delete(key); chunk = null; } @@ -424,11 +424,11 @@ public class MovieCoder { while (!done) { if (chunk == null) { - log.info("Looking for " + key + " - " + chunkNo); + log.info("Looking for {} - {}", key, chunkNo); chunk = movieDb.load(key, chunkNo); } if (chunk != null) { - log.info("Sending " + chunkNo + ", " + chunk.bits.length + " bytes"); + log.info("Sending {}, {} bytes", chunkNo, chunk.bits.length); out.write(chunk.bits); chunk = null; chunkNo++;