private SampleDescriptionBox mergeStsds(SampleDescriptionBox stsd1, SampleDescriptionBox stsd2) throws IOException { ByteArrayOutputStream curBaos = new ByteArrayOutputStream(); ByteArrayOutputStream refBaos = new ByteArrayOutputStream(); try { stsd1.getBox(Channels.newChannel(curBaos)); stsd2.getBox(Channels.newChannel(refBaos)); } catch (IOException e) { LOG.logError(e.getMessage()); return null; } byte[] cur = curBaos.toByteArray(); byte[] ref = refBaos.toByteArray(); if (!Arrays.equals(ref, cur)) { SampleEntry se = mergeSampleEntry(stsd1.getBoxes(SampleEntry.class).get(0), stsd2.getBoxes(SampleEntry.class).get(0)); if (se != null) { stsd1.setBoxes(Collections.<Box>singletonList(se)); } else { throw new IOException("Cannot merge " + stsd1.getBoxes(SampleEntry.class).get(0) + " and " + stsd2.getBoxes(SampleEntry.class).get(0)); } } return stsd1; }
public Avc1ToAvc3TrackImpl(Track parent) throws IOException { super(parent); if (!"avc1".equals(parent.getSampleDescriptionBox().getSampleEntry().getType())) { throw new RuntimeException("Only avc1 tracks can be converted to avc3 tracks"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); parent.getSampleDescriptionBox().getBox(Channels.newChannel(baos)); IsoFile isoFile = new IsoFile(new MemoryDataSourceImpl(baos.toByteArray())); this.stsd = Path.getPath(isoFile, "stsd"); ((VisualSampleEntry) stsd.getSampleEntry()).setType("avc3"); avcC = Path.getPath(stsd, "avc./avcC"); samples = new ReplaceSyncSamplesList(parent.getSamples()); }
public AppendTrack(Track... tracks) throws IOException { this.tracks = tracks; for (Track track : tracks) { if (stsd == null) { stsd = track.getSampleDescriptionBox(); } else { ByteArrayOutputStream curBaos = new ByteArrayOutputStream(); ByteArrayOutputStream refBaos = new ByteArrayOutputStream(); track.getSampleDescriptionBox().getBox(Channels.newChannel(curBaos)); stsd.getBox(Channels.newChannel(refBaos)); byte[] cur = curBaos.toByteArray(); byte[] ref = refBaos.toByteArray(); if (!Arrays.equals(ref, cur)) { SampleDescriptionBox curStsd = track.getSampleDescriptionBox(); if (stsd.getBoxes().size() == 1 && curStsd.getBoxes().size() == 1) { if (stsd.getBoxes().get(0) instanceof AudioSampleEntry && curStsd.getBoxes().get(0) instanceof AudioSampleEntry) { AudioSampleEntry aseResult = mergeAudioSampleEntries((AudioSampleEntry) stsd.getBoxes().get(0), (AudioSampleEntry) curStsd.getBoxes().get(0)); if (aseResult != null) { stsd.setBoxes(Collections.<Box>singletonList(aseResult)); return; } } } throw new IOException("Cannot append " + track + " to " + tracks[0] + " since their Sample Description Boxes differ: \n" + track.getSampleDescriptionBox() + "\n vs. \n" + tracks[0].getSampleDescriptionBox()); } } } }
public SampleDescriptionBox getSampleDescriptionBox() { OriginalFormatBox frma = Path.getPath(original.getSampleDescriptionBox(), "enc./sinf/frma"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); SampleDescriptionBox stsd; try { original.getSampleDescriptionBox().getBox(Channels.newChannel(baos)); stsd = (SampleDescriptionBox) new IsoFile(new MemoryDataSourceImpl(baos.toByteArray())).getBoxes().get(0); } catch (IOException e) { throw new RuntimeException("Dumping stsd to memory failed"); } if (stsd.getSampleEntry() instanceof AudioSampleEntry) { ((AudioSampleEntry) stsd.getSampleEntry()).setType(frma.getDataFormat()); } else if (stsd.getSampleEntry() instanceof VisualSampleEntry) { ((VisualSampleEntry) stsd.getSampleEntry()).setType(frma.getDataFormat()); } else { throw new RuntimeException("I don't know " + stsd.getSampleEntry().getType()); } List<Box> nuBoxes = new LinkedList<Box>(); for (Box box : stsd.getSampleEntry().getBoxes()) { if (!box.getType().equals("sinf")) { nuBoxes.add(box); } } stsd.getSampleEntry().setBoxes(nuBoxes); return stsd; }
public AppendTrack(Track... tracks) throws IOException { this.tracks = tracks; for (Track track : tracks) { if (stsd == null) { stsd = track.getSampleDescriptionBox(); } else { ByteArrayOutputStream curBaos = new ByteArrayOutputStream(); ByteArrayOutputStream refBaos = new ByteArrayOutputStream(); track.getSampleDescriptionBox().getBox(Channels.newChannel(curBaos)); stsd.getBox(Channels.newChannel(refBaos)); byte[] cur = curBaos.toByteArray(); byte[] ref = refBaos.toByteArray(); if (!Arrays.equals(ref, cur)) { SampleDescriptionBox curStsd = track.getSampleDescriptionBox(); if (stsd.getBoxes().size() == 1 && curStsd.getBoxes().size() == 1) { if (stsd.getBoxes().get(0) instanceof AudioSampleEntry && curStsd.getBoxes().get(0) instanceof AudioSampleEntry) { AudioSampleEntry aseResult = mergeAudioSampleEntries((AudioSampleEntry) stsd.getBoxes().get(0), (AudioSampleEntry) curStsd.getBoxes().get(0)); if (aseResult != null) { stsd.setBoxes(Collections.<Box>singletonList(aseResult)); return; } } } throw new IOException("Cannot append " + track + " to " + tracks[0] + " since their Sample Description Boxes differ: \n" + track.getSampleDescriptionBox() + "\n vs. \n" + tracks[0].getSampleDescriptionBox()); } } } }
ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { source.getSampleDescriptionBox().getBox(Channels.newChannel(baos)); stsd = (SampleDescriptionBox) new IsoFile(new MemoryDataSourceImpl(baos.toByteArray())).getBoxes().get(0); } catch (IOException e) {