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 * Ricardo Dias 018 */ 019package org.nuxeo.ecm.platform.video.tools; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.Arrays; 024import java.util.List; 025import java.util.Map; 026 027import org.apache.commons.io.FilenameUtils; 028import org.apache.commons.lang3.StringUtils; 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * The {@link VideoTool} that extracts closed captions. 036 * 037 * @since 8.4 038 */ 039public class VideoClosedCaptionsExtractor extends VideoTool { 040 041 public final static String NAME = "ccExtractorTool"; 042 043 public final static String START_AT_PARAM = "startAt"; 044 045 public final static String END_AT_PARAM = "endAt"; 046 047 public final static String OUTPUT_FORMAT_PARAM = "outFormat"; 048 049 protected static final String COMMANDLINE_FULL_VIDEO = "videoClosedCaptionsExtractor"; 050 051 protected static final String COMMANDLINE_SLICED_VIDEO = "videoPartClosedCaptionsExtractor"; 052 053 protected static final String DEFAULT_OUTFORMAT = "ttxt"; 054 055 protected static final List<String> TEXT_OUTFORMATS = Arrays.asList("srt", "txt", "ttxt"); 056 057 public VideoClosedCaptionsExtractor() { 058 super(NAME, COMMANDLINE_FULL_VIDEO); 059 } 060 061 public List<String> getSupportedFormats() { 062 return TEXT_OUTFORMATS; 063 } 064 065 public boolean isFormatSupported(String format) { 066 return TEXT_OUTFORMATS.contains(format); 067 } 068 069 @Override 070 public Map<String, String> setupParameters(BlobHolder blobHolder, Map<String, Object> parameters) { 071 Map<String, String> cmdParameters = super.setupParameters(blobHolder, parameters); 072 073 Blob video = blobHolder.getBlob(); 074 String outputFormat = (String) parameters.get(OUTPUT_FORMAT_PARAM); 075 String startAt = (String) parameters.get(START_AT_PARAM); 076 String endAt = (String) parameters.get(END_AT_PARAM); 077 078 if (StringUtils.isEmpty(outputFormat)) { 079 outputFormat = DEFAULT_OUTFORMAT; 080 } 081 cmdParameters.put(OUTPUT_FORMAT_PARAM, outputFormat); 082 cmdParameters.put(OUTPUT_MIMETYPE_PARAM, outputFormat.equals(DEFAULT_OUTFORMAT) ? "text/xml" : "text/plain"); 083 084 if (!StringUtils.isBlank(startAt) && !StringUtils.isBlank(endAt)) { 085 cmdParameters.put(START_AT_PARAM, startAt); 086 cmdParameters.put(END_AT_PARAM, endAt); 087 commandLineName = COMMANDLINE_SLICED_VIDEO; 088 } 089 090 try { 091 File outputFile = Framework.createTempFile(FilenameUtils.removeExtension(video.getFilename()), 092 "-CC." + outputFormat); 093 094 cmdParameters.put(OUTPUT_FILE_PATH_PARAM, outputFile.getAbsolutePath()); 095 } catch (IOException e) { 096 throw new NuxeoException("Cannot setup parameters for VideoClosedCaptionsExtractor", e); 097 } 098 return cmdParameters; 099 } 100}