Search in sources :

Example 1 with IsoFile

use of org.mp4parser.IsoFile in project VideoOptimzer by attdevsupport.

the class VideoStreamConstructor method processSidxBox.

private void processSidxBox(byte[] content, ChildManifest childManifest) {
    LOG.info("Processing sidx information for URI: " + childManifest.getUriName());
    File tempFile = null;
    IsoFile isoFile = null;
    try {
        // Write content data (mp4 file) to a temporary file
        tempFile = File.createTempFile("temp" + RANDOM.nextInt(), ".mp4");
        FileUtils.writeByteArrayToFile(tempFile, content);
        // Parse mp4 file and process Segment Box
        isoFile = new IsoFile(tempFile);
        for (Box box : isoFile.getBoxes()) {
            if (box instanceof SegmentIndexBox) {
                SegmentIndexBox sidxBox = (SegmentIndexBox) box;
                int lastSegmentIndex = -1;
                // Update last segment index to last entry in the segment list for the corresponding child manifest's segments
                if (!childManifest.getSegmentInfoTrie().isEmpty()) {
                    lastSegmentIndex = Integer.valueOf(childManifest.getSegmentInfoTrie().lastKey().split("-")[1]);
                }
                double timePos = 0;
                int idx = childManifest.getSegmentInfoTrie().size();
                Map<String, Double> segmentInformationMap = segmentInformationByFile.get(childManifest);
                // Iterate through Segment Index Box entries
                for (SegmentIndexBox.Entry entry : sidxBox.getEntries()) {
                    String key = String.valueOf((lastSegmentIndex + 1)) + "-" + String.valueOf((lastSegmentIndex + entry.getReferencedSize()));
                    segmentInformationMap.put(key, (double) entry.getSubsegmentDuration() / sidxBox.getTimeScale());
                    // Add SegmentInfo to Child Manifest
                    SegmentInfo segmentInfo = new SegmentInfo();
                    segmentInfo.setDuration((double) entry.getSubsegmentDuration() / sidxBox.getTimeScale());
                    segmentInfo.setStartTime(timePos);
                    segmentInfo.setSegmentID(idx++);
                    segmentInfo.setContentType(childManifest.getContentType());
                    segmentInfo.setVideo(childManifest.isVideo());
                    segmentInfo.setSize(entry.getReferencedSize());
                    segmentInfo.setQuality(String.valueOf(childManifest.getQuality()));
                    childManifest.addSegment(key, segmentInfo);
                    timePos += segmentInfo.getDuration();
                    lastSegmentIndex += entry.getReferencedSize();
                }
                // Not expecting and will not process another Segment Index Box
                break;
            }
        }
    } catch (Exception e) {
        LOG.error("Something went wrong while reading sidx box", e);
    } finally {
        if (tempFile != null && !tempFile.delete()) {
            tempFile.deleteOnExit();
        }
        if (isoFile != null) {
            try {
                isoFile.close();
            } catch (IOException e) {
                LOG.warn("Unable to close iso file resource", e);
            }
        }
    }
}
Also used : IsoFile(org.mp4parser.IsoFile) Box(org.mp4parser.Box) SegmentIndexBox(org.mp4parser.boxes.iso14496.part12.SegmentIndexBox) IOException(java.io.IOException) IOException(java.io.IOException) SegmentIndexBox(org.mp4parser.boxes.iso14496.part12.SegmentIndexBox) SegmentInfo(com.att.aro.core.videoanalysis.impl.SegmentInfo) IsoFile(org.mp4parser.IsoFile) File(java.io.File)

Aggregations

SegmentInfo (com.att.aro.core.videoanalysis.impl.SegmentInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 Box (org.mp4parser.Box)1 IsoFile (org.mp4parser.IsoFile)1 SegmentIndexBox (org.mp4parser.boxes.iso14496.part12.SegmentIndexBox)1