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 java.io.IOException;
023
024import org.apache.commons.lang3.StringUtils;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
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;
034
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
037import org.nuxeo.ecm.platform.video.tools.VideoToolsService;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Operation for merging 2-n videos in one.
042 *
043 * @since 8.4
044 */
045@Operation(id = ConcatVideos.ID, category = Constants.CAT_BLOB, label = "Joins two or more videos sequentially.", description = "Merge 2-n videos in one.")
046public class ConcatVideos {
047
048    public static final String ID = "Video.Concat";
049
050    @Param(name = "xpath", required = false)
051    protected String xpath;
052
053    @OperationMethod
054    public Blob run(BlobList blobs) throws NuxeoException, IOException, CommandNotAvailable {
055        VideoToolsService service = Framework.getService(VideoToolsService.class);
056        return service.concat(blobs);
057    }
058
059    @OperationMethod
060    public Blob run(DocumentModelList docs) throws NuxeoException, IOException, CommandNotAvailable {
061        BlobList blobs = new BlobList();
062        for (DocumentModel doc : docs) {
063            if (StringUtils.isEmpty(xpath)) {
064                blobs.add(doc.getAdapter(BlobHolder.class).getBlob());
065            } else {
066                blobs.add((Blob) doc.getPropertyValue(xpath));
067            }
068        }
069        return run(blobs);
070    }
071}