001/*
002 * (C) Copyright 2016-2018 Nuxeo (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.lang3.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 * Slices the video to obtain a part of it.
040 *
041 * @since 8.4
042 */
043@Operation(id = SliceVideo.ID, category = Constants.CAT_CONVERSION, label = "SliceVideo the video for a given duration and startAt time.", description = "SliceVideo the input blob starting at startAt, for a certain duration. The duration and startAt arguments are optional, but not simultaneously. A specific converter can be used.")
044public class SliceVideo {
045
046    public static final String ID = "Video.Slice";
047
048    @Param(name = "startAt", required = false)
049    protected String startAt;
050
051    @Param(name = "duration", required = false)
052    protected String duration;
053
054    @Param(name = "encode", required = false, values = "true")
055    protected boolean encode = true;
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        for (DocumentModel doc : input) {
073            blobList.add(run(doc));
074        }
075        return blobList;
076    }
077
078    @OperationMethod(collector = BlobCollector.class)
079    public Blob run(Blob input) throws OperationException {
080        try {
081            VideoToolsService videoService = Framework.getService(VideoToolsService.class);
082            return videoService.slice(input, StringUtils.isEmpty(startAt) ? "00:00" : startAt, duration, encode).get(0);
083        } catch (NuxeoException e) {
084            throw new OperationException(e);
085        }
086    }
087
088}