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 * Extracts Closed Captions from a Video.
040 *
041 * @since 8.4
042 */
043@Operation(id = ExtractClosedCaptionsFromVideo.ID, category = Constants.CAT_CONVERSION, label = "Extracts closed captions from the video.", description = "Extracts the closed captions from the whole video or from a part of it when startAt and end time is provided. The output format references how the output is generated, and xpath can be used to indicate the video blob when using documents.")
044public class ExtractClosedCaptionsFromVideo {
045
046    public static final String ID = "Video.ExtractClosedCaptions";
047
048    @Param(name = "outFormat", required = false, values = { "ttxt", "srt", "txt" })
049    protected String outFormat;
050
051    @Param(name = "startAt", required = false)
052    protected String startAt;
053
054    @Param(name = "endAt", required = false)
055    protected String endAt;
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 service = Framework.getService(VideoToolsService.class);
082            return service.extractClosedCaptions(input, outFormat, startAt, endAt);
083        } catch (NuxeoException e) {
084            throw new OperationException(e);
085        }
086    }
087
088}