Search in sources :

Example 1 with FrameGrab

use of org.jcodec.api.FrameGrab in project OpenOLAT by OpenOLAT.

the class VideoManagerImpl method getFrameWithFilter.

@Override
public boolean getFrameWithFilter(OLATResource videoResource, int frameNumber, long duration, VFSLeaf frame) {
    File videoFile = ((LocalFileImpl) getMasterVideoFile(videoResource)).getBasefile();
    BufferedImage bufImg = null;
    boolean imgBlack = true;
    int countBlack = 0;
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(videoFile, "r")) {
        OutputStream frameOutputStream = frame.getOutputStream(false);
        FileChannel ch = randomAccessFile.getChannel();
        FileChannelWrapper in = new FileChannelWrapper(ch);
        FrameGrab frameGrab = new FrameGrab(in).seekToFrameSloppy(frameNumber);
        bufImg = frameGrab.getFrame();
        int xmin = bufImg.getMinX();
        int ymin = bufImg.getMinY();
        int xmax = xmin + bufImg.getWidth();
        int ymax = ymin + bufImg.getHeight();
        int pixelCount = bufImg.getWidth() * bufImg.getHeight();
        for (int x = xmin; x < xmax; x++) {
            for (int y = ymin; y < ymax; y++) {
                int rgb = bufImg.getRGB(x, y);
                // int alpha = (0xff000000 & rgb) >>> 24;
                int r = (0x00ff0000 & rgb) >> 16;
                int g = (0x0000ff00 & rgb) >> 8;
                int b = (0x000000ff & rgb);
                if (r < 30 && g < 30 && b < 30) {
                    countBlack++;
                }
            }
        }
        if (countBlack > (int) (0.7F * pixelCount)) {
            imgBlack = true;
        } else {
            imgBlack = false;
            ImageIO.write(bufImg, "JPG", frameOutputStream);
        }
        // avoid endless loop
        if (frameNumber > duration) {
            imgBlack = false;
        }
        // close everything to prevent resource leaks
        frameOutputStream.close();
        in.close();
        ch.close();
        return imgBlack;
    } catch (Exception | AssertionError e) {
        log.error("Could not get frame::" + frameNumber + " for video::" + videoFile.getAbsolutePath(), e);
        return false;
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) BufferedImage(java.awt.image.BufferedImage) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) FrameGrab(org.jcodec.api.FrameGrab) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with FrameGrab

use of org.jcodec.api.FrameGrab in project BoofCV by lessthanoptimal.

the class JCodecSimplified method reset.

@Override
public void reset() {
    try {
        grabber = new FrameGrab(NIOUtils.readableFileChannel(new File(filename)));
    } catch (IOException | JCodecException e) {
        throw new RuntimeException(e);
    }
    try {
        frameCurrent = null;
        frameNext = grabber.getNativeFrame();
    } catch (IOException e) {
        frameNext = null;
    }
}
Also used : FrameGrab(org.jcodec.api.FrameGrab) JCodecException(org.jcodec.api.JCodecException) IOException(java.io.IOException) File(java.io.File)

Example 3 with FrameGrab

use of org.jcodec.api.FrameGrab in project OpenOLAT by OpenOLAT.

the class VideoManagerImpl method getFrame.

/**
 * write the the given frame at frameNumber in the frame leaf
 * @param videoResource videoresource
 * @param frameNumber the frameNumber at which the frame should be taken from
 * @param frame the VFSLeaf to write the picked image to
 */
@Override
public boolean getFrame(OLATResource videoResource, int frameNumber, VFSLeaf frame) {
    File videoFile = ((LocalFileImpl) getMasterVideoFile(videoResource)).getBasefile();
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(videoFile, "r")) {
        FileChannel ch = randomAccessFile.getChannel();
        FileChannelWrapper in = new FileChannelWrapper(ch);
        FrameGrab frameGrab = new FrameGrab(in).seekToFrameSloppy(frameNumber);
        OutputStream frameOutputStream = frame.getOutputStream(false);
        BufferedImage bufImg = frameGrab.getFrame();
        ImageIO.write(bufImg, "JPG", frameOutputStream);
        // close everything to prevent resource leaks
        frameOutputStream.close();
        in.close();
        ch.close();
        return true;
    } catch (Exception | AssertionError e) {
        log.error("Could not get frame::" + frameNumber + " for video::" + videoFile.getAbsolutePath(), e);
        return false;
    }
}
Also used : FrameGrab(org.jcodec.api.FrameGrab) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException)

Example 4 with FrameGrab

use of org.jcodec.api.FrameGrab in project openolat by klemens.

the class VideoManagerImpl method getFrameWithFilter.

@Override
public boolean getFrameWithFilter(OLATResource videoResource, int frameNumber, long duration, VFSLeaf frame) {
    File videoFile = ((LocalFileImpl) getMasterVideoFile(videoResource)).getBasefile();
    BufferedImage bufImg = null;
    boolean imgBlack = true;
    int countBlack = 0;
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(videoFile, "r")) {
        OutputStream frameOutputStream = frame.getOutputStream(false);
        FileChannel ch = randomAccessFile.getChannel();
        FileChannelWrapper in = new FileChannelWrapper(ch);
        FrameGrab frameGrab = new FrameGrab(in).seekToFrameSloppy(frameNumber);
        bufImg = frameGrab.getFrame();
        int xmin = bufImg.getMinX();
        int ymin = bufImg.getMinY();
        int xmax = xmin + bufImg.getWidth();
        int ymax = ymin + bufImg.getHeight();
        int pixelCount = bufImg.getWidth() * bufImg.getHeight();
        for (int x = xmin; x < xmax; x++) {
            for (int y = ymin; y < ymax; y++) {
                int rgb = bufImg.getRGB(x, y);
                // int alpha = (0xff000000 & rgb) >>> 24;
                int r = (0x00ff0000 & rgb) >> 16;
                int g = (0x0000ff00 & rgb) >> 8;
                int b = (0x000000ff & rgb);
                if (r < 30 && g < 30 && b < 30) {
                    countBlack++;
                }
            }
        }
        if (countBlack > (int) (0.7F * pixelCount)) {
            imgBlack = true;
        } else {
            imgBlack = false;
            ImageIO.write(bufImg, "JPG", frameOutputStream);
        }
        // avoid endless loop
        if (frameNumber > duration) {
            imgBlack = false;
        }
        // close everything to prevent resource leaks
        frameOutputStream.close();
        in.close();
        ch.close();
        return imgBlack;
    } catch (Exception | AssertionError e) {
        log.error("Could not get frame::" + frameNumber + " for video::" + videoFile.getAbsolutePath(), e);
        return false;
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) BufferedImage(java.awt.image.BufferedImage) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) FrameGrab(org.jcodec.api.FrameGrab) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 5 with FrameGrab

use of org.jcodec.api.FrameGrab in project openolat by klemens.

the class VideoManagerImpl method getFrame.

/**
 * write the the given frame at frameNumber in the frame leaf
 * @param videoResource videoresource
 * @param frameNumber the frameNumber at which the frame should be taken from
 * @param frame the VFSLeaf to write the picked image to
 */
@Override
public boolean getFrame(OLATResource videoResource, int frameNumber, VFSLeaf frame) {
    File videoFile = ((LocalFileImpl) getMasterVideoFile(videoResource)).getBasefile();
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(videoFile, "r")) {
        FileChannel ch = randomAccessFile.getChannel();
        FileChannelWrapper in = new FileChannelWrapper(ch);
        FrameGrab frameGrab = new FrameGrab(in).seekToFrameSloppy(frameNumber);
        OutputStream frameOutputStream = frame.getOutputStream(false);
        BufferedImage bufImg = frameGrab.getFrame();
        ImageIO.write(bufImg, "JPG", frameOutputStream);
        // close everything to prevent resource leaks
        frameOutputStream.close();
        in.close();
        ch.close();
        return true;
    } catch (Exception | AssertionError e) {
        log.error("Could not get frame::" + frameNumber + " for video::" + videoFile.getAbsolutePath(), e);
        return false;
    }
}
Also used : FrameGrab(org.jcodec.api.FrameGrab) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException)

Aggregations

File (java.io.File)5 IOException (java.io.IOException)5 FrameGrab (org.jcodec.api.FrameGrab)5 BufferedImage (java.awt.image.BufferedImage)4 BufferedOutputStream (java.io.BufferedOutputStream)4 OutputStream (java.io.OutputStream)4 RandomAccessFile (java.io.RandomAccessFile)4 FileChannel (java.nio.channels.FileChannel)4 ZipFile (java.util.zip.ZipFile)4 FileChannelWrapper (org.jcodec.common.FileChannelWrapper)4 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)4 SchedulerException (org.quartz.SchedulerException)4 JCodecException (org.jcodec.api.JCodecException)1