001/*
002 * (C) Copyright 2010-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.video.convert;
020
021import static org.nuxeo.ecm.platform.video.convert.Constants.POSITION_PARAMETER;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Map;
026
027import org.apache.commons.io.FilenameUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.CloseableFile;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.convert.api.ConversionException;
035import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
036import org.nuxeo.ecm.core.convert.extension.Converter;
037import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
038import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
039import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
040import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
041import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
042import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
043import org.nuxeo.runtime.api.Framework;
044
045/**
046 * Extract a JPEG screenshot of the video at a given time offset (position).
047 *
048 * @author ogrisel
049 */
050public class ScreenshotConverter implements Converter {
051
052    public static final Log log = LogFactory.getLog(ScreenshotConverter.class);
053
054    public static final String FFMPEG_SCREENSHOT_COMMAND = "ffmpeg-screenshot";
055
056    @Override
057    public void init(ConverterDescriptor descriptor) {
058    }
059
060    @Override
061    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
062        Blob blob = blobHolder.getBlob();
063        if (blob == null) {
064            throw new ConversionException("conversion failed (null blob)");
065        }
066        try (CloseableFile source = blob.getCloseableFile("." + FilenameUtils.getExtension(blob.getFilename()))) {
067            Blob outBlob = Blobs.createBlobWithExtension(".jpeg");
068
069            CommandLineExecutorService cles = Framework.getService(CommandLineExecutorService.class);
070            CmdParameters params = cles.getDefaultCmdParameters();
071            params.addNamedParameter("inFilePath", source.getFile().getAbsolutePath());
072            params.addNamedParameter("outFilePath", outBlob.getFile().getAbsolutePath());
073            Double position = 0.0;
074            if (parameters != null) {
075                position = (Double) parameters.get(POSITION_PARAMETER);
076                if (position == null) {
077                    position = 0.0;
078                }
079            }
080            long positionParam = Math.round(position);
081            params.addNamedParameter(POSITION_PARAMETER, String.valueOf(positionParam));
082            ExecResult res = cles.execCommand(FFMPEG_SCREENSHOT_COMMAND, params);
083            if (!res.isSuccessful()) {
084                throw res.getError();
085            }
086
087            outBlob.setMimeType("image/jpeg");
088            outBlob.setFilename(String.format("video-screenshot-%05d.000.jpeg", positionParam));
089            return new SimpleCachableBlobHolder(outBlob);
090        } catch (CommandNotAvailable | IOException | CommandException e) {
091            throw new ConversionException("error extracting screenshot from '" + blob.getFilename() + "'", e);
092        }
093    }
094
095}