package io.lumify.storm.audio;
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.ProcessRunner;
import org.securegraph.Element;
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;
import java.util.HashMap;
import java.util.Map;
public class AudioMp4EncodingWorker extends GraphPropertyWorker {
private static final String PROPERTY_KEY = AudioMp4EncodingWorker.class.getName();
private ProcessRunner processRunner;
@Override
public void execute(InputStream in, GraphPropertyWorkData data) throws Exception {
File mp4File = File.createTempFile("encode_mp4_", ".mp4");
try {
processRunner.execute(
"ffmpeg",
new String[]{
"-y", // overwrite output files
"-i", data.getLocalFile().getAbsolutePath(),
"-acodec", "libfdk_aac",
mp4File.getAbsolutePath()
},
null,
data.getLocalFile().getAbsolutePath() + ": "
);
ExistingElementMutation<Vertex> m = data.getElement().prepareMutation();
InputStream mp4FileIn = new FileInputStream(mp4File);
try {
StreamingPropertyValue spv = new StreamingPropertyValue(mp4FileIn, byte[].class);
spv.searchIndex(false);
Map<String, Object> metadata = new HashMap<String, Object>();
metadata.put(LumifyProperties.MIME_TYPE.getPropertyName(), MediaLumifyProperties.MIME_TYPE_AUDIO_MP4);
MediaLumifyProperties.AUDIO_MP4.addPropertyValue(m, PROPERTY_KEY, spv, metadata, data.getProperty().getVisibility());
m.save(getAuthorizations());
} finally {
mp4FileIn.close();
}
} finally {
mp4File.delete();
}
}
@Override
public boolean isHandled(Element element, Property property) {
if (property == null) {
return false;
}
if (!property.getName().equals(LumifyProperties.RAW.getPropertyName())) {
return false;
}
String mimeType = (String) property.getMetadata().get(LumifyProperties.MIME_TYPE.getPropertyName());
if (mimeType == null || !mimeType.startsWith("audio")) {
return false;
}
if (MediaLumifyProperties.AUDIO_MP4.hasProperty(element, PROPERTY_KEY)) {
return false;
}
return true;
}
@Override
public boolean isLocalFileRequired() {
return true;
}
@Inject
public void setProcessRunner(ProcessRunner ffmpeg) {
this.processRunner = ffmpeg;
}
}