package io.lumify.gpw.video; import com.google.inject.Inject; import io.lumify.core.ingest.graphProperty.GraphPropertyWorkData; import io.lumify.core.ingest.graphProperty.GraphPropertyWorker; import io.lumify.core.model.properties.LumifyProperties; import io.lumify.core.model.properties.MediaLumifyProperties; import io.lumify.core.util.LumifyLogger; import io.lumify.core.util.LumifyLoggerFactory; import io.lumify.core.util.ProcessRunner; import org.securegraph.Element; import org.securegraph.Metadata; import org.securegraph.Property; import org.securegraph.Vertex; import org.securegraph.mutation.ExistingElementMutation; import org.securegraph.property.StreamingPropertyValue; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class VideoAudioExtractWorker extends GraphPropertyWorker { private static final LumifyLogger LOGGER = LumifyLoggerFactory.getLogger(VideoAudioExtractWorker.class); private static final String PROPERTY_KEY = VideoAudioExtractWorker.class.getName(); private ProcessRunner processRunner; @Override public void execute(InputStream in, GraphPropertyWorkData data) throws Exception { File mp3File = File.createTempFile("audio_extract_", ".mp3"); try { processRunner.execute( "ffmpeg", new String[]{ "-i", data.getLocalFile().getAbsolutePath(), "-vn", "-ar", "44100", "-ab", "320k", "-f", "mp3", "-y", mp3File.getAbsolutePath() }, null, data.getLocalFile().getAbsolutePath() + ": " ); ExistingElementMutation<Vertex> m = data.getElement().prepareMutation(); try (InputStream mp3FileIn = new FileInputStream(mp3File)) { StreamingPropertyValue spv = new StreamingPropertyValue(mp3FileIn, byte[].class); spv.searchIndex(false); Metadata metadata = new Metadata(); metadata.add(LumifyProperties.MIME_TYPE.getPropertyName(), MediaLumifyProperties.MIME_TYPE_AUDIO_MP3, getVisibilityTranslator().getDefaultVisibility()); MediaLumifyProperties.AUDIO_MP3.addPropertyValue(m, PROPERTY_KEY, spv, metadata, data.getProperty().getVisibility()); m.save(getAuthorizations()); getGraph().flush(); getWorkQueueRepository().pushGraphPropertyQueue(data.getElement(), PROPERTY_KEY, MediaLumifyProperties.AUDIO_MP3.getPropertyName()); } } finally { if (!mp3File.delete()) { LOGGER.warn("Could not delete %s", mp3File.getAbsolutePath()); } } } @Override public boolean isHandled(Element element, Property property) { if (property == null) { return false; } if (!property.getName().equals(LumifyProperties.RAW.getPropertyName())) { return false; } String mimeType = LumifyProperties.MIME_TYPE.getMetadataValue(property.getMetadata(), null); if (mimeType == null || !mimeType.startsWith("video")) { return false; } if (MediaLumifyProperties.AUDIO_MP3.hasProperty(element, PROPERTY_KEY)) { return false; } return true; } @Override public boolean isLocalFileRequired() { return true; } @Inject public void setProcessRunner(ProcessRunner ffmpeg) { this.processRunner = ffmpeg; } }