www in docker support

This commit is contained in:
MaddoScientisto 2026-04-22 18:41:37 +02:00
commit c227fce036
2145 changed files with 399596 additions and 58 deletions

View file

@ -0,0 +1,129 @@
package it.acxent.videoj;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.jcodec.codecs.png.PNGDecoder;
import org.jcodec.codecs.png.PNGEncoder;
import org.jcodec.common.Preconditions;
import org.jcodec.common.VideoCodecMeta;
import org.jcodec.common.io.NIOUtils;
import org.jcodec.common.model.ColorSpace;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.ColorUtil;
import org.jcodec.scale.RgbToBgr;
import org.jcodec.scale.Transform;
public class AWTUtil {
private static final int alphaR = 255;
private static final int alphaG = 255;
private static final int alphaB = 255;
public static void toBufferedImage2(Picture src, BufferedImage dst) {
byte[] data = ((DataBufferByte)dst.getRaster().getDataBuffer()).getData();
byte[] srcData = src.getPlaneData(0);
for (int i = 0; i < data.length; i++)
data[i] = (byte)(srcData[i] + 128);
}
public static BufferedImage toBufferedImage(Picture src) {
if (src.getColor() != ColorSpace.BGR) {
Picture bgr = Picture.createCropped(src.getWidth(), src.getHeight(), ColorSpace.BGR, src.getCrop());
if (src.getColor() == ColorSpace.RGB) {
new RgbToBgr().transform(src, bgr);
} else {
Transform transform = ColorUtil.getTransform(src.getColor(), ColorSpace.RGB);
transform.transform(src, bgr);
new RgbToBgr().transform(bgr, bgr);
}
src = bgr;
}
BufferedImage dst = new BufferedImage(src.getCroppedWidth(), src.getCroppedHeight(), 5);
if (src.getCrop() == null) {
toBufferedImage2(src, dst);
} else {
toBufferedImageCropped(src, dst);
}
return dst;
}
private static void toBufferedImageCropped(Picture src, BufferedImage dst) {
byte[] data = ((DataBufferByte)dst.getRaster().getDataBuffer()).getData();
byte[] srcData = src.getPlaneData(0);
int dstStride = dst.getWidth() * 3;
int srcStride = src.getWidth() * 3;
for (int line = 0, srcOff = 0, dstOff = 0; line < dst.getHeight(); line++) {
for (int id = dstOff, is = srcOff; id < dstOff + dstStride; id += 3, is += 3) {
data[id] = (byte)(srcData[is] + 128);
data[id + 1] = (byte)(srcData[is + 1] + 128);
data[id + 2] = (byte)(srcData[is + 2] + 128);
}
srcOff += srcStride;
dstOff += dstStride;
}
}
public static void writePNG(Picture picture, File pngFile) throws IOException {
Picture rgb = picture.getColor().equals(ColorSpace.RGB) ? picture : convertColorSpace(picture, ColorSpace.RGB);
PNGEncoder encoder = new PNGEncoder();
ByteBuffer tmpBuf = ByteBuffer.allocate(encoder.estimateBufferSize(rgb));
ByteBuffer encoded = encoder.encodeFrame(rgb, tmpBuf).getData();
NIOUtils.writeTo(encoded, pngFile);
}
public static Picture decodePNG(File f, ColorSpace tgtColor) throws IOException {
Picture picture = decodePNG0(f);
Preconditions.checkNotNull(picture, "cant decode " + f.getPath());
return convertColorSpace(picture, tgtColor);
}
public static Picture decodePNG0(File f) throws IOException {
PNGDecoder pngDec = new PNGDecoder();
ByteBuffer buf = NIOUtils.fetchFromFile(f);
VideoCodecMeta codecMeta = pngDec.getCodecMeta(buf);
Picture pic = Picture.create(codecMeta.getSize().getWidth(), codecMeta.getSize().getHeight(), ColorSpace.RGB);
return pngDec.decodeFrame(buf, pic.getData());
}
public static Picture convertColorSpace(Picture pic, ColorSpace tgtColor) {
Transform tr = ColorUtil.getTransform(pic.getColor(), tgtColor);
Picture res = Picture.create(pic.getWidth(), pic.getHeight(), tgtColor);
tr.transform(pic, res);
return res;
}
public static Picture fromBufferedImage(BufferedImage src, ColorSpace tgtColor) {
return convertColorSpace(fromBufferedImageRGB(src), tgtColor);
}
public static Picture fromBufferedImageRGB(BufferedImage src) {
Picture dst = Picture.create(src.getWidth(), src.getHeight(), ColorSpace.RGB);
bufImgToPicture(src, dst);
return dst;
}
public static void bufImgToPicture(BufferedImage src, Picture dst) {
byte[] dstData = dst.getPlaneData(0);
int off = 0;
for (int i = 0; i < src.getHeight(); i++) {
for (int j = 0; j < src.getWidth(); j++) {
int rgb1 = src.getRGB(j, i);
int alpha = rgb1 >> 24 & 0xFF;
if (alpha == 255) {
dstData[off++] = (byte)((rgb1 >> 16 & 0xFF) - 128);
dstData[off++] = (byte)((rgb1 >> 8 & 0xFF) - 128);
dstData[off++] = (byte)((rgb1 & 0xFF) - 128);
} else {
int nalpha = 255 - alpha;
dstData[off++] = (byte)(((rgb1 >> 16 & 0xFF) * alpha + 255 * nalpha >> 8) - 128);
dstData[off++] = (byte)(((rgb1 >> 8 & 0xFF) * alpha + 255 * nalpha >> 8) - 128);
dstData[off++] = (byte)(((rgb1 & 0xFF) * alpha + 255 * nalpha >> 8) - 128);
}
}
}
}
}

View file

@ -0,0 +1,52 @@
package it.acxent.videoj;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
import org.jcodec.api.FrameGrab;
import org.jcodec.common.io.NIOUtils;
import org.jcodec.common.model.Picture;
public class ManageVideoJ {
public static final LinkedHashSet<String> extractNFrames(String videoFileName, String targetDir, String targetName, int nFrames) throws Exception {
Timer timer = new Timer();
timer.start();
FrameGrab g = FrameGrab.createFrameGrab(NIOUtils.readableChannel(new File(videoFileName)));
LinkedHashSet<String> frames = new LinkedHashSet<>();
BufferedImage bufferedImage = null;
Picture picture = null;
try {
File targetDirFile = new File(targetDir);
if (!targetDirFile.exists())
targetDirFile.mkdirs();
int movieLen = g.getVideoTrack().getMeta().getTotalFrames();
int step = movieLen / (nFrames + 1);
System.out.println("len:" + movieLen + " step: " + step);
for (int i = 0; i < nFrames; i++) {
g.seekToFrameSloppy(i * step);
picture = g.getNativeFrame();
bufferedImage = AWTUtil.toBufferedImage(picture);
String currentFile = targetDir + targetDir + "_" + targetName + ".jpg";
frames.add(currentFile);
File frameFile = new File(currentFile);
ImageIO.write(bufferedImage, "jpg", frameFile);
bufferedImage.flush();
bufferedImage = null;
picture = null;
}
} finally {
if (bufferedImage != null) {
bufferedImage.flush();
bufferedImage = null;
}
if (g != null) {
g.getDecoder().shutdownThreadPool();
g = null;
}
}
timer.stop();
System.out.println("extractNFrames. DURATA: " + timer.getDurataHourMin());
return frames;
}
}

View file

@ -0,0 +1,17 @@
package it.acxent.videoj;
public class TestVideo {
public static void main(String[] args) {
try {
String video = args[0];
String targetDir = args[1];
String targetName = args[2];
ManageVideoJ.extractNFrames(video, targetDir, targetName, 4);
} catch (Exception e) {
e.printStackTrace();
System.out.println("usage: TestVideo video targetdir targetname");
}
}
public static void testLogin() {}
}

View file

@ -0,0 +1,92 @@
package it.acxent.videoj;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
public class Timer {
private Timestamp tStart = null;
private Timestamp tStop = null;
public void start() {
this.tStart = new Timestamp(System.currentTimeMillis());
this.tStop = null;
}
public void stop() {
this.tStop = new Timestamp(System.currentTimeMillis());
}
public String getDurataSec() {
return String.valueOf(getDurataSecLong());
}
public long getDurataSecLong() {
long secs = 0L;
if (this.tStop == null) {
secs = getTimeDiff(new Time(getTStart().getTime()), new Time(System.currentTimeMillis()));
} else {
secs = getTimeDiff(new Time(getTStart().getTime()), new Time(this.tStop.getTime()));
}
return secs;
}
public String getDurataHourMin() {
String temp = secToTempoHourMin(getDurataSecLong());
return temp;
}
public static String secToTempoHourMin(long sec) {
long h = sec / 3600L;
long min = (sec - h * 3600L) / 60L;
long secF = sec - h * 3600L - min * 60L;
return "" + h + " h " + h + " min " + min + " sec";
}
public Timestamp getTStart() {
if (this.tStart == null)
this.tStart = new Timestamp(System.currentTimeMillis());
return this.tStart;
}
public Timestamp getTStop() {
return this.tStop;
}
public String getDurata() {
String temp = "\n\n--------------------------------------------------\nPartenza: " + String.valueOf(getTStart()) + "\nFine: " + String.valueOf(this.tStop) + "\nDurata: " +
secToTempoHourMin(getDurataSecLong());
return temp;
}
public long getDurataMilliSec() {
long msecs = 0L;
if (this.tStop == null) {
msecs = System.currentTimeMillis() - getTStart().getTime();
} else {
msecs = this.tStop.getTime() - getTStart().getTime();
}
return msecs;
}
public static long getTimeDiff(Time time1, Time time2) {
long c1, c2;
if (time1 == null) {
c1 = 0L;
} else {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time1.getTime());
c1 = (long)(cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13));
}
if (time2 == null) {
c2 = 0L;
} else {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time2.getTime());
c2 = (long)(cal.get(11) * 3600 + cal.get(12) * 60 + cal.get(13));
}
long difInSecs = c2 - c1;
return difInSecs;
}
}

View file

@ -0,0 +1 @@
package it.acxent.videoj;