Search in sources :

Example 11 with FileChannelWrapper

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

Example 12 with FileChannelWrapper

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

the class MovieServiceImpl method getFrameCount.

@Override
public long getFrameCount(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);
            return demuxer1.getVideoTrack().getFrameCount();
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract num. of frames of: " + media, e);
        }
    }
    return -1;
}
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