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 *     Thibaud Arguillere
018 *     Ricardo Dias
019 */
020package org.nuxeo.ecm.platform.video.tools.operations;
021
022import org.apache.commons.lang.StringUtils;
023import org.nuxeo.ecm.automation.OperationException;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
029import org.nuxeo.ecm.automation.core.util.BlobList;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.DocumentModelList;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
035import org.nuxeo.ecm.platform.video.tools.VideoToolsService;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Watermark a Video with the given Picture, at the given position (from top-left).
040 *
041 * @since 8.4
042 */
043@Operation(id = AddWatermarkToVideo.ID, category = Constants.CAT_BLOB, label = "Watermarks a Video with a Picture", description = "Watermark the video with the picture stored in file:content of watermark, at the position(x, y) from the left-top corner of the picture.")
044public class AddWatermarkToVideo {
045
046    public static final String ID = "Video.AddWatermark";
047
048    @Param(name = "watermark")
049    protected DocumentModel watermark;
050
051    @Param(name = "x", required = false, values = { "0" })
052    protected String x;
053
054    @Param(name = "y", required = false, values = { "0" })
055    protected String y;
056
057    @Param(name = "xpath", required = false)
058    protected String xpath;
059
060    @OperationMethod
061    public Blob run(DocumentModel input) throws OperationException {
062        if (StringUtils.isEmpty(xpath)) {
063            return run(input.getAdapter(BlobHolder.class).getBlob());
064        } else {
065            return run((Blob) input.getPropertyValue(xpath));
066        }
067    }
068
069    @OperationMethod
070    public BlobList run(DocumentModelList input) throws OperationException {
071        BlobList blobList = new BlobList();
072        String blobPath = StringUtils.isEmpty(xpath)? "file:content" : xpath;
073        for (DocumentModel doc : input) {
074            blobList.add(run((Blob) doc.getPropertyValue(blobPath)));
075        }
076        return blobList;
077    }
078
079    @OperationMethod(collector = BlobCollector.class)
080    public Blob run(Blob input) throws OperationException {
081        Blob watermarkBlob = (Blob) watermark.getPropertyValue("file:content");
082        try {
083            VideoToolsService service = Framework.getService(VideoToolsService.class);
084            return service.watermark(input, watermarkBlob, x, y);
085        } catch (NuxeoException e) {
086            throw new OperationException("Cannot add the watermark to the video.", e);
087        }
088    }
089}