Search in sources :

Example 1 with FileChannelWrapper

use of org.jcodec.common.FileChannelWrapper in project OpenOLAT by OpenOLAT.

the class MovieServiceImpl method getSize.

@Override
public Size getSize(VFSLeaf media, String suffix) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return null;
    }
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            org.jcodec.common.model.Size size = demuxer1.getMovie().getDisplaySize();
            // Case 1: standard case, get dimension from movie
            int w = size.getWidth();
            int h = size.getHeight();
            // Case 2: landscape movie from iOS: width and height is negative, no dunny why
            if (w < 0 && h < 0) {
                w = 0 - w;
                h = 0 - h;
            }
            if (w == 0) {
                // something in the track box.
                try {
                    // This code is the way it is just because I don't know
                    // how to safely read the rotation/portrait/landscape
                    // flag of the movie. Those mp4 guys are really
                    // secretive folks, did not find any documentation about
                    // this. Best guess.
                    org.jcodec.common.model.Size size2 = demuxer1.getVideoTrack().getBox().getCodedSize();
                    w = size2.getHeight();
                    h = size2.getWidth();
                } catch (Exception e) {
                    log.debug("can not get size from box " + e.getMessage());
                }
            }
            return new Size(w, h, false);
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract size of: " + media, e);
        }
    } else if (suffix.equals("flv")) {
        try (InputStream stream = new FileInputStream(file)) {
            FLVParser infos = new FLVParser();
            infos.parse(stream);
            if (infos.getWidth() > 0 && infos.getHeight() > 0) {
                int w = infos.getWidth();
                int h = infos.getHeight();
                return new Size(w, h, false);
            }
        } catch (Exception e) {
            log.error("Cannot extract size of: " + media, e);
        }
    }
    return null;
}
Also used : MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) FileInputStream(java.io.FileInputStream) VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) FLVParser(org.olat.core.commons.services.video.spi.FLVParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with FileChannelWrapper

use of org.jcodec.common.FileChannelWrapper in project OpenOLAT by OpenOLAT.

the class MovieServiceImpl method isMP4.

@Override
public boolean isMP4(VFSLeaf media, String fileName) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return false;
    }
    String suffix = FileUtils.getFileSuffix(fileName);
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            String fourCC = demuxer1.getVideoTrack().getFourcc();
            if (fourCCs.contains(fourCC.toLowerCase())) {
                return true;
            }
            log.info("Movie file::" + fileName + " has correct suffix::" + suffix + " but fourCC::" + fourCC + " not in our list of supported codecs.");
        } catch (Exception | Error e) {
        // anticipated exception, is not an mp4 file
        }
    }
    return false;
}
Also used : VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)

Example 3 with FileChannelWrapper

use of org.jcodec.common.FileChannelWrapper 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 4 with FileChannelWrapper

use of org.jcodec.common.FileChannelWrapper in project openolat by klemens.

the class MovieServiceImpl method getDuration.

@Override
public long getDuration(VFSLeaf media, String suffix) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return -1;
    }
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            MovieBox movie = demuxer1.getMovie();
            long duration = movie.getDuration();
            int timescale = movie.getTimescale();
            if (timescale < 1) {
                timescale = 1;
            }
            // Simple calculation. Ignore NTSC and other issues for now
            return duration / timescale * 1000;
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract duration of: " + media, e);
        }
    }
    return -1;
}
Also used : MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) MovieBox(org.jcodec.containers.mp4.boxes.MovieBox) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with FileChannelWrapper

use of org.jcodec.common.FileChannelWrapper in project openolat by klemens.

the class MovieServiceImpl method isMP4.

@Override
public boolean isMP4(VFSLeaf media, String fileName) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return false;
    }
    String suffix = FileUtils.getFileSuffix(fileName);
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            String fourCC = demuxer1.getVideoTrack().getFourcc();
            if (fourCCs.contains(fourCC.toLowerCase())) {
                return true;
            }
            log.info("Movie file::" + fileName + " has correct suffix::" + suffix + " but fourCC::" + fourCC + " not in our list of supported codecs.");
        } catch (Exception | Error e) {
        // anticipated exception, is not an mp4 file
        }
    }
    return false;
}
Also used : VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)

Aggregations

File (java.io.File)12 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 FileChannelWrapper (org.jcodec.common.FileChannelWrapper)12 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)12 MP4Demuxer (org.jcodec.containers.mp4.demuxer.MP4Demuxer)8 CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)8 VFSCPNamedItem (org.olat.ims.cp.ui.VFSCPNamedItem)8 BufferedImage (java.awt.image.BufferedImage)4 BufferedOutputStream (java.io.BufferedOutputStream)4 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 ZipFile (java.util.zip.ZipFile)4 FrameGrab (org.jcodec.api.FrameGrab)4 SchedulerException (org.quartz.SchedulerException)4 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 MovieBox (org.jcodec.containers.mp4.boxes.MovieBox)2 Size (org.olat.core.commons.services.image.Size)2 FinalSize (org.olat.core.commons.services.thumbnail.FinalSize)2