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 * Slices the video to obtain a part of it.
040 * @since 8.4
041 */
042@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.")
043public class SliceVideo {
044
045    public static final String ID = "Video.Slice";
046
047    @Param(name = "startAt", required = false)
048    protected String startAt;
049
050    @Param(name = "duration", required = false)
051    protected String duration;
052
053    @Param(name = "encode", required = false)
054    protected boolean encode = true;
055
056    @Param(name = "xpath", required = false)
057    protected String xpath;
058
059    @OperationMethod
060    public Blob run(DocumentModel input) throws OperationException {
061        if (StringUtils.isEmpty(xpath)) {
062            return run(input.getAdapter(BlobHolder.class).getBlob());
063        } else {
064            return run((Blob) input.getPropertyValue(xpath));
065        }
066    }
067
068    @OperationMethod
069    public BlobList run(DocumentModelList input) throws OperationException {
070        BlobList blobList = new BlobList();
071        for (DocumentModel doc : input) {
072            blobList.add(run(doc));
073        }
074        return blobList;
075    }
076
077    @OperationMethod(collector = BlobCollector.class)
078    public Blob run(Blob input) throws OperationException {
079        try {
080            VideoToolsService videoService = Framework.getService(VideoToolsService.class);
081            return videoService.slice(input,
082                    StringUtils.isEmpty(startAt) ? "00:00" : startAt,
083                    duration, encode).get(0);
084        } catch(NuxeoException e) {
085            throw new OperationException(e);
086        }
087    }
088
089}