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.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
036/**
037 * Operation for slicing a video in parts with approximately equal duration.
038 *
039 * @since 8.4
040 */
041@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 = {
042        "Video.SliceInParts" })
043public class SliceVideoInParts {
044
045    public static final String ID = "Video.SliceInParts";
046
047    @Param(name = "duration", required = false)
048    protected String duration;
049
050    @Param(name = "xpath", required = false)
051    protected String xpath;
052
053    @OperationMethod
054    public BlobList run(DocumentModel input) throws OperationException {
055        if (StringUtils.isEmpty(xpath)) {
056            return run(input.getAdapter(BlobHolder.class).getBlob());
057        } else {
058            return run((Blob) input.getPropertyValue(xpath));
059        }
060    }
061
062    @OperationMethod
063    public BlobList run(Blob input) throws OperationException {
064        try {
065            VideoToolsService service = Framework.getService(VideoToolsService.class);
066            return new BlobList(service.slice(input, "", duration, false));
067        } catch (NuxeoException e) {
068            throw new OperationException(e);
069        }
070    }
071}