Clean up various IntelliJ warnings

This commit is contained in:
Erik Forkalsrud 2025-02-01 18:47:18 -08:00
parent 05859a2e58
commit 7df5a06cd1
2 changed files with 41 additions and 50 deletions

View file

@ -1,9 +0,0 @@
package org.forkalsrud.album.video;
public interface EncodingProcessListener {
void chunkAvailable(int chunkNo);
void codingFinished(int lastChunkNo);
}

View file

@ -24,7 +24,7 @@ public class MovieCoder {
private String ffmpegExecutable; private String ffmpegExecutable;
private String mplayerExecutable; private String mplayerExecutable;
private PictureScaler pictureScaler; private PictureScaler pictureScaler;
private HashMap<String, EncodingProcess> currentEncodings = new HashMap<String, EncodingProcess>(); private HashMap<String, EncodingProcess> currentEncodings = new HashMap<>();
public MovieCoder(PictureScaler pictureScaler) { public MovieCoder(PictureScaler pictureScaler) {
this.pictureScaler = pictureScaler; this.pictureScaler = pictureScaler;
@ -52,7 +52,7 @@ public class MovieCoder {
return temp; return temp;
} }
public void deleteTempDirectory(File dir) { private void deleteTempDirectory(File dir) {
for (File sub : dir.listFiles()) { for (File sub : dir.listFiles()) {
if (sub.isDirectory()) { if (sub.isDirectory()) {
deleteTempDirectory(sub); deleteTempDirectory(sub);
@ -74,8 +74,7 @@ public class MovieCoder {
p.getOutputStream().close(); p.getOutputStream().close();
log.debug(IOUtils.toString(p.getInputStream())); log.debug(IOUtils.toString(p.getInputStream()));
p.waitFor(); p.waitFor();
CachedImage ci = pictureScaler.scalePicture(frame, thumbnail, size); return pictureScaler.scalePicture(frame, thumbnail, size);
return ci;
} finally { } finally {
deleteTempDirectory(tmpDir); deleteTempDirectory(tmpDir);
} }
@ -107,7 +106,6 @@ public class MovieCoder {
private final int chunkSize = 4 * 65536; private final int chunkSize = 4 * 65536;
private File file; private File file;
private Dimension targetSize; private Dimension targetSize;
private ArrayList<EncodingProcessListener> listeners = new ArrayList<EncodingProcessListener>();
private Chunk currentChunk = null; private Chunk currentChunk = null;
private int chunkPos; private int chunkPos;
private int remainingCapacity; 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/ffmpeg-x264-encoding-guide/
* http://rob.opendot.cl/index.php/useful-stuff/encoding-a-flv-video-for-embedded-web-playback/ * 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 * and we want an output bitrate about 1 Mbit/sec
* we can do a one pass encoding like this: * we can do a one pass encoding like this:
* *
@ -174,25 +172,7 @@ public class MovieCoder {
try { try {
ArrayList<String> command = new ArrayList<String>(); ProcessBuilder pb = createTranscoderProcess(vf);
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);
log.info(pb.command().toString()); log.info(pb.command().toString());
pb.redirectErrorStream(false); pb.redirectErrorStream(false);
@ -228,7 +208,35 @@ public class MovieCoder {
notifyListeners(chunkAvailable); notifyListeners(chunkAvailable);
} }
} }
private ProcessBuilder createTranscoderProcess(String vf) {
ArrayList<String> 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. * @return number of chunks available.
* -Integer.MAX_VALUE if something went very wrong * -Integer.MAX_VALUE if something went very wrong
@ -257,7 +265,7 @@ public class MovieCoder {
int len = data.length; int len = data.length;
Chunk chunk0 = new Chunk(fileTimestamp, len, 0); Chunk chunk0 = new Chunk(fileTimestamp, len, 0);
chunk0.bits = data; 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); movieDb.store(dbKey, 0, chunk0);
notifyListeners(chunkInProgress); notifyListeners(chunkInProgress);
} }
@ -297,9 +305,9 @@ public class MovieCoder {
private void endChunk() { private void endChunk() {
log.info("store chunk " + chunkInProgress); log.info("store chunk {}", chunkInProgress);
movieDb.store(dbKey, chunkInProgress, currentChunk); 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; currentChunk = null;
notifyListeners(chunkInProgress); notifyListeners(chunkInProgress);
} }
@ -313,17 +321,9 @@ public class MovieCoder {
Chunk last = new Chunk(fileTimestamp, chunkPos, 0); Chunk last = new Chunk(fileTimestamp, chunkPos, 0);
System.arraycopy(currentChunk.bits, 0, last.bits, 0, chunkPos); System.arraycopy(currentChunk.bits, 0, last.bits, 0, chunkPos);
movieDb.store(dbKey, chunkInProgress, last); 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; currentChunk = null;
} }
public synchronized void addListener(EncodingProcessListener videoStreamer) {
listeners.add(videoStreamer);
}
public synchronized void removeListener(VideoStreamer videoStreamer) {
listeners.remove(videoStreamer);
}
} }
class ErrorStreamPumper implements Runnable { class ErrorStreamPumper implements Runnable {
@ -382,7 +382,7 @@ public class MovieCoder {
// If the file has been modified since our last encoding job finished // If the file has been modified since our last encoding job finished
if (chunk != null && ep == null) { if (chunk != null && ep == null) {
if (chunk.timestamp != file.lastModified()) { 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); movieDb.delete(key);
chunk = null; chunk = null;
} }
@ -424,11 +424,11 @@ public class MovieCoder {
while (!done) { while (!done) {
if (chunk == null) { if (chunk == null) {
log.info("Looking for " + key + " - " + chunkNo); log.info("Looking for {} - {}", key, chunkNo);
chunk = movieDb.load(key, chunkNo); chunk = movieDb.load(key, chunkNo);
} }
if (chunk != null) { if (chunk != null) {
log.info("Sending " + chunkNo + ", " + chunk.bits.length + " bytes"); log.info("Sending {}, {} bytes", chunkNo, chunk.bits.length);
out.write(chunk.bits); out.write(chunk.bits);
chunk = null; chunk = null;
chunkNo++; chunkNo++;