001/*
002 * (C) Copyright 2010-2015 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 *
019 */
020package org.nuxeo.ecm.platform.video.convert;
021
022import static org.nuxeo.ecm.platform.video.convert.Constants.POSITION_PARAMETER;
023
024import java.io.IOException;
025import java.io.Serializable;
026import java.util.Map;
027
028import org.apache.commons.io.FilenameUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.CloseableFile;
034import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
035import org.nuxeo.ecm.core.convert.api.ConversionException;
036import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
037import org.nuxeo.ecm.core.convert.extension.Converter;
038import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
039import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
040import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
041import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
042import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
043import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
044import org.nuxeo.runtime.api.Framework;
045
046/**
047 * Extract a JPEG screenshot of the video at a given time offset (position).
048 *
049 * @author ogrisel
050 */
051public class ScreenshotConverter extends BaseVideoConverter implements Converter {
052
053    public static final Log log = LogFactory.getLog(ScreenshotConverter.class);
054
055    public static final String FFMPEG_SCREENSHOT_COMMAND = "ffmpeg-screenshot";
056
057    @Override
058    public void init(ConverterDescriptor descriptor) {
059    }
060
061    @Override
062    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
063        Blob blob = blobHolder.getBlob();
064        if (blob == null) {
065            throw new ConversionException("conversion failed (null blob)");
066        }
067        try (CloseableFile source = blob.getCloseableFile("." + FilenameUtils.getExtension(blob.getFilename()))) {
068            Blob outBlob = Blobs.createBlobWithExtension(".jpeg");
069
070            CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
071            CmdParameters params = cles.getDefaultCmdParameters();
072            params.addNamedParameter("inFilePath", source.getFile().getAbsolutePath());
073            params.addNamedParameter("outFilePath", outBlob.getFile().getAbsolutePath());
074            Double position = 0.0;
075            if (parameters != null) {
076                position = (Double) parameters.get(POSITION_PARAMETER);
077                if (position == null) {
078                    position = 0.0;
079                }
080            }
081            long positionParam = Math.round(position);
082            params.addNamedParameter(POSITION_PARAMETER, String.valueOf(positionParam));
083            ExecResult res = cles.execCommand(FFMPEG_SCREENSHOT_COMMAND, params);
084            if (!res.isSuccessful()) {
085                throw res.getError();
086            }
087
088            outBlob.setMimeType("image/jpeg");
089            outBlob.setFilename(String.format("video-screenshot-%05d.000.jpeg", positionParam));
090            return new SimpleCachableBlobHolder(outBlob);
091        } catch (CommandNotAvailable | IOException | CommandException e) {
092            throw new ConversionException("error extracting screenshot from '" + blob.getFilename() + "'", e);
093        }
094    }
095
096}