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.util.BlobList;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.platform.video.tools.VideoToolsService;
034import org.nuxeo.runtime.api.Framework;
035
036import java.util.List;
037
038/**
039 * Operation for slicing a video in parts with approximately equal duration.
040 *
041 * @since 8.4
042 */
043@Operation(id = SliceVideoInParts.ID, category = Constants.CAT_CONVERSION, label = "SliceVideo a Video in Parts with equal duration.", description = "Slices the video in n parts of approximately the same duration each.", aliases = {
044        "Video.SliceInParts" })
045public class SliceVideoInParts {
046
047    public static final String ID = "Video.SliceInParts";
048
049    @Param(name = "duration", required = false)
050    protected String duration;
051
052    @Param(name = "xpath", required = false)
053    protected String xpath;
054
055    @OperationMethod
056    public List<Blob> run(DocumentModel input) throws OperationException {
057        if (StringUtils.isEmpty(xpath)) {
058            return run(input.getAdapter(BlobHolder.class).getBlob());
059        } else {
060            return run((Blob) input.getPropertyValue(xpath));
061        }
062    }
063
064    @OperationMethod
065    public List<Blob> run(Blob input) throws OperationException {
066        try {
067            VideoToolsService service = Framework.getService(VideoToolsService.class);
068            return service.slice(input, "", duration, false);
069        } catch(NuxeoException e){
070            throw new OperationException(e);
071        }
072    }
073}