001/*
002 * (C) Copyright 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 *     Ricardo Dias
018 */
019package org.nuxeo.ecm.platform.video.tools;
020
021import org.apache.commons.io.FilenameUtils;
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
025import org.nuxeo.runtime.api.Framework;
026
027import java.io.File;
028import java.io.IOException;
029import java.util.Map;
030
031/**
032 * The {@link VideoTool} for adding a watermark to a video blob.
033 * @since 8.4
034 */
035public class VideoWatermarker extends VideoTool {
036
037    public final static String NAME = "watermarkerTool";
038
039    public final static String WATERMARK_X_POSITION_PARAM = "x";
040
041    public final static String WATERMARK_Y_POSITION_PARAM = "y";
042
043    public final static String WATERMARK_PARAM = "pictureFilePath";
044
045    protected final static String FILTER_COMPLEX_PARAM = "filterComplex";
046
047    protected final static String VIDEO_WATERMARKER_COMMANDLINE = "videoWatermarkWithPicture";
048
049    public VideoWatermarker() {
050        super(NAME, VIDEO_WATERMARKER_COMMANDLINE);
051    }
052
053    @Override
054    public Map<String, String> setupParameters(BlobHolder input, Map<String, Object> parameters) {
055        Map<String, String> cmdParameters = super.setupParameters(input,parameters);
056
057        Blob video = input.getBlob();
058        String x = (String) parameters.get(WATERMARK_X_POSITION_PARAM);
059        String y = (String) parameters.get(WATERMARK_Y_POSITION_PARAM);
060        Blob watermark = (Blob) parameters.get(WATERMARK_PARAM);
061
062        // Prepare parameters
063        try {
064            String overlay = "overlay=" + x + ":" + y;
065            File outputFile = Framework.createTempFile(FilenameUtils.removeExtension(video.getFilename()),
066                    "-WM." + FilenameUtils.getExtension(video.getFilename()));
067
068            cmdParameters.put(WATERMARK_PARAM, watermark.getFile().getAbsolutePath());
069            cmdParameters.put(FILTER_COMPLEX_PARAM, overlay);
070            cmdParameters.put(OUTPUT_FILE_PATH_PARAM, outputFile.getAbsolutePath());
071        } catch (IOException e) {
072            throw new NuxeoException("Cannot setup parameters for VideoWatermarker.", e);
073        }
074        return cmdParameters;
075    }
076}