@SuppressWarnings("unchecked") public <T extends Box> List<T> getBoxes(Class<T> clazz, boolean recursive) { List<T> boxesToBeReturned = new ArrayList<T>(2); List<Box> boxes = getBoxes(); for (int i = 0; i < boxes.size(); i++) { Box boxe = boxes.get(i); //clazz.isInstance(boxe) / clazz == boxe.getClass()? // I hereby finally decide to use isInstance if (clazz.isInstance(boxe)) { boxesToBeReturned.add((T) boxe); } if (recursive && boxe instanceof Container) { boxesToBeReturned.addAll(((Container) boxe).getBoxes(clazz, recursive)); } } return boxesToBeReturned; }
public static void main(String[] args) throws IOException { String f1 = AppendExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070268951.mp4"; Movie inMovie = MovieCreator.build(f1); inMovie.setMatrix(Matrix.ROTATE_90); new DefaultMp4Builder().build(inMovie).writeContainer(new FileOutputStream("output.mp4").getChannel()); } }
public static void check(Container root1, Container cb1, Container root2, Container cb2, String... ignores) throws IOException { Iterator<Box> it1 = cb1.getBoxes().iterator(); Iterator<Box> it2 = cb2.getBoxes().iterator(); while (it1.hasNext() && it2.hasNext()) { Box b1 = it1.next(); Box b2 = it2.next(); check(root1, b1, root2, b2, ignores); } if (it1.hasNext()) { Assert.fail("There is a box missing in the current output of the tool: " + it1.next()); } if (it2.hasNext()) { Assert.fail("There is a box too much in the current output of the tool: " + it2.next()); } }
public static void main(String[] args) throws IOException { //Movie video = mc.build(Channels.newChannel(ReadWriteExample.class.getResourceAsStream("/smoothstreaming/video-128h-75kbps.mp4"))); Movie video = MovieCreator.build("/home/sannies/scm/svn/mp4parser/Solekai022_854_29_640x75_MaxSdSubtitle.uvu"); //IsoFile out1 = new FragmentedMp4Builder().build(video); Container out2 = new DefaultMp4Builder().build(video); /* long starttime1 = System.currentTimeMillis(); FileChannel fc1 = new RandomAccessFile("video-128h-75kbps.fmp4", "rw").getChannel(); fc1.position(0); out1.getBox(fc1); long size1 = fc1.size(); fc1.truncate(fc1.position()); fc1.close(); System.err.println("Writing " + size1 / 1024 / 1024 + "MB took " + (System.currentTimeMillis() - starttime1));*/ long starttime2 = System.currentTimeMillis(); FileChannel fc2 = new RandomAccessFile("output_uvu.mp4", "rw").getChannel(); out2.writeContainer(fc2); long size2 = fc2.size(); fc2.truncate(fc2.position()); fc2.close(); System.err.println("Writing " + size2 / 1024 / 1024 + "MB took " + (System.currentTimeMillis() - starttime2)); }
public static List<Box> getBoxes(Container outer, String types[], List<Box> list) { for (Box box : outer.getBoxes()) { for (String type : types) { if (box.getType().equals(type)) { list.add(box); } } if (box instanceof Container) { getBoxes((Container) box, types, list); } } return list; }
public static void main(String[] args) throws IOException { Movie m = MovieCreator.build("c:\\content\\ffmpeg.mp4"); DefaultMp4Builder defaultMp4Builder = new DefaultMp4Builder(); Container c = defaultMp4Builder.build(m); FileOutputStream fos = new FileOutputStream("C:\\content\\out.mp4"); WritableByteChannel wbc = Channels.newChannel(fos); c.writeContainer(wbc); } }
FreeBox findFreeBox(Container c) { for (Box box : c.getBoxes()) { System.err.println(box.getType()); if (box instanceof FreeBox) { return (FreeBox) box; } if (box instanceof Container) { FreeBox freeBox = findFreeBox((Container) box); if (freeBox != null) { return freeBox; } } } return null; }
public static void main(String[] args) throws IOException { File dest = File.createTempFile("Unmux", "main"); FileOutputStream fos = new FileOutputStream(dest); IOUtils.copy(new URL("http://org.mp4parser.s3.amazonaws.com/examples/Cosmos%20Laundromat%20small.mp4").openStream(), fos); fos.close(); Movie m = MovieCreator.build(dest.getAbsolutePath()); DefaultMp4Builder builder = new DefaultMp4Builder(); for (Track track : m.getTracks()) { Movie singleTrackMovie = new Movie(Collections.singletonList(track)); builder.build(singleTrackMovie).writeContainer(new RandomAccessFile(track.getHandler() + "_" + track.getTrackMetaData().getTrackId() + ".mp4", "rw").getChannel()); } } }
public FindSaioSaizPair invoke() { List<SampleAuxiliaryInformationSizesBox> saizs = container.getBoxes(SampleAuxiliaryInformationSizesBox.class); List<SampleAuxiliaryInformationOffsetsBox> saios = container.getBoxes(SampleAuxiliaryInformationOffsetsBox.class); assert saizs.size() == saios.size(); saiz = null; saio = null; for (int i = 0; i < saizs.size(); i++) { if (saiz == null && (saizs.get(i).getAuxInfoType() == null) || "cenc".equals(saizs.get(i).getAuxInfoType())) { saiz = saizs.get(i); } else if (saiz != null && saiz.getAuxInfoType() == null && "cenc".equals(saizs.get(i).getAuxInfoType())) { saiz = saizs.get(i); } else { throw new RuntimeException("Are there two cenc labeled saiz?"); } if (saio == null && (saios.get(i).getAuxInfoType() == null) || "cenc".equals(saios.get(i).getAuxInfoType())) { saio = saios.get(i); } else if (saio != null && saio.getAuxInfoType() == null && "cenc".equals(saios.get(i).getAuxInfoType())) { saio = saios.get(i); } else { throw new RuntimeException("Are there two cenc labeled saio?"); } } return this; } }
public static void main(String[] args) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException, URISyntaxException { Movie m = new Movie(); /* m.addTrack( new SMPTETTTrackImpl( new File("C:\\dev\\mp4parser-github\\ttml-example\\subtitle_5_0.xml"), new File("C:\\dev\\mp4parser-github\\ttml-example\\subtitle_5_1.xml"), new File("C:\\dev\\mp4parser-github\\ttml-example\\subtitle_5_2.xml")));*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document d = dbf.newDocumentBuilder().parse("C:\\dev\\mp4parser-github\\ttml-example\\subtitle_3_0.xml"); m.addTrack( new TtmlTrackImpl("some subtile", Collections.singletonList(d))); DefaultMp4Builder builder = new DefaultMp4Builder(); builder.build(m).writeContainer(new FileOutputStream("output.mp4").getChannel()); } }
private static void dumpBoxes(Container container, int indent) { String meInd = getIndentation(indent); String subInd = getIndentation(indent + 2); System.out.println(meInd + container.getClass().getName()); for (Box box : container.getBoxes()) { if (box instanceof Container) { dumpBoxes((Container) box, indent + 2); } else { try { if (box instanceof UnknownBox) { System.out.println(subInd + box.getClass().getName() + "[" + box.getSize() + "/" + box.getType() + "]:" + box.toString()); } else if (box instanceof Utf8AppleDataBox) { System.out.println(subInd + box.getClass().getName() + ": " + box.getType() + ": " + box.toString() + ": " + ((Utf8AppleDataBox) box).getValue()); } else { System.out.println(subInd + box.getClass().getName() + ": " + box.getType() + "[" + box.getSize() + "]: " + box.toString()); } } catch (Exception e) { System.err.println("Error parsing " + box.getClass().getSimpleName() + " box: " + e); e.printStackTrace(System.err); } } } }
public static void main(String[] args) throws IOException { String videoFile = MuxVideoWithAmf0.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/example-sans-amf0.mp4"; Movie video = MovieCreator.build(videoFile); Properties props = new Properties(); props.load(MuxVideoWithAmf0.class.getResourceAsStream("/amf0track.properties")); HashMap<Long, byte[]> samples = new HashMap<Long, byte[]>(); for (String key : props.stringPropertyNames()) { samples.put(Long.parseLong(key), Base64.decodeBase64(props.getProperty(key))); } Track amf0Track = new Amf0Track(samples); amf0Track.getTrackMetaData(); video.addTrack(amf0Track); FragmentedMp4Builder fragmentedMp4Builder = new FragmentedMp4Builder(); fragmentedMp4Builder.setFragmenter(new DefaultFragmenterImpl(2)); Container out = fragmentedMp4Builder.build(video); FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4"))); FileChannel fc = fos.getChannel(); out.writeContainer(fc); fos.close(); } }
Iterator<Box> iterator = ((Container) thing).getBoxes().iterator(); while (iterator.hasNext()) { Box box1 = iterator.next();
public static void main(String[] args) throws URISyntaxException, ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse("C:\\dev\\mp4parser\\a.xml"); List<Document> split = TtmlSegmenter.split(doc, 60); /* for (Document document : split) { pretty(document, System.out, 4); }*/ Track t = new TtmlTrackImpl("a.xml", split); Movie m = new Movie(); m.addTrack(t); Container c = new DefaultMp4Builder().build(m); c.writeContainer(new FileOutputStream("output.mp4").getChannel()); }
private void initAllFragments() { List<TrackFragmentBox> trafs = new ArrayList<>(); for (MovieFragmentBox moof : isofile.getBoxes(MovieFragmentBox.class)) { for (TrackFragmentBox trackFragmentBox : moof.getBoxes(TrackFragmentBox.class)) { if (trackFragmentBox.getTrackFragmentHeaderBox().getTrackId() == trackBox.getTrackHeaderBox().getTrackId()) { trafs.add(trackFragmentBox); traf2moof.put(trackFragmentBox, moof); } } } allTrafs = trafs; int firstSample = 1; firstSamples = new int[allTrafs.size()]; for (int i = 0; i < allTrafs.size(); i++) { firstSamples[i] = firstSample; firstSample += getTrafSize(allTrafs.get(i)); } }
public static void main(String[] args) throws IOException { String audioEnglish = RemoveSomeSamplesExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-english-audio.mp4"; Movie originalMovie = MovieCreator.build(audioEnglish); Track audio = originalMovie.getTracks().get(0); Movie nuMovie = new Movie(); nuMovie.addTrack(new AppendTrack(new ClippedTrack(audio, 0, 10), new ClippedTrack(audio, 100, audio.getSamples().size()))); Container out = new DefaultMp4Builder().build(nuMovie); FileOutputStream fos = new FileOutputStream(new File("output.mp4")); out.writeContainer(fos.getChannel()); fos.close(); }
public static long find(Container container, ParsableBox target, long offset) { long nuOffset = offset; for (Box lightBox : container.getBoxes()) { if (lightBox == target) { return nuOffset; } if (lightBox instanceof Container) { long r = find((Container) lightBox, target, 0); if (r > 0) { return r + nuOffset; } else { nuOffset += lightBox.getSize(); } } else { nuOffset += lightBox.getSize(); } } return -1; } }
public static void main(String[] args) throws IOException { List<Movie> movies = new LinkedList<Movie>(); movies.add(MovieCreator.build(DavidAppend.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/davidappend/v1.mp4")); movies.add(MovieCreator.build(DavidAppend.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/davidappend/v2.mp4")); movies.add(MovieCreator.build(DavidAppend.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/davidappend/v2.mp4")); List<Track> videoTracks = new LinkedList<Track>(); List<Track> audioTracks = new LinkedList<Track>(); for (Movie m : movies) { for (Track track : m.getTracks()) { if (track.getHandler().equals("vide")) { videoTracks.add(track); } if (track.getHandler().equals("soun")) { audioTracks.add(track); } } } Movie concatMovie = new Movie(); concatMovie.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()]))); concatMovie.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()]))); Container out2 = new DefaultMp4Builder().build(concatMovie); FileChannel fc = new RandomAccessFile(String.format("output.mp4"), "rw").getChannel(); fc.position(0); out2.writeContainer(fc); fc.close(); }
for (MovieFragmentBox movieFragmentBox : isofile.getBoxes(MovieFragmentBox.class)) { List<TrackFragmentBox> trafs = movieFragmentBox.getBoxes(TrackFragmentBox.class); for (TrackFragmentBox traf : trafs) { baseOffset = traf.getTrackFragmentHeaderBox().getBaseDataOffset(); } else { Iterator<Box> it = isofile.getBoxes().iterator(); baseOffset = 0; for (Box b = it.next(); b != movieFragmentBox; b = it.next()) {
public static void main(String[] args) throws IOException { String basePath = GetDuration.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/dash/"; Movie m = new Movie(); IsoFile baseIsoFile = new IsoFile(basePath + "redbull_100kbit_dash.mp4"); List<IsoFile> fragments = new LinkedList<IsoFile>(); for (int i = 1; i < 9; i++) { fragments.add(new IsoFile(basePath + "redbull_10sec" + i + ".m4s")); } m.addTrack(new Mp4TrackImpl(1, new IsoFile("redbull_100kbit_dash.mp4"), new FileRandomAccessSourceImpl(new RandomAccessFile("redbull_100kbit_dash.mp4", "r")), "test")); DefaultMp4Builder builder = new DefaultMp4Builder(); Container stdMp4 = builder.build(m); FileOutputStream fos = new FileOutputStream("out.mp4"); stdMp4.writeContainer(fos.getChannel()); fos.close(); } }