Refine search
@Override public String toString() { String s = "Movie{ "; for (Track track : tracks) { s += "track_" + track.getTrackMetaData().getTrackId() + " (" + track.getHandler() + ") "; } s += '}'; return s; }
public long[] getSampleDurations() { long[] scaled = new long[source.getSampleDurations().length]; for (int i = 0; i < source.getSampleDurations().length; i++) { scaled[i] = source.getSampleDurations()[i] / timeScaleDivisor; } return scaled; }
public List<Sample> getSamples() { return source.getSamples(); }
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String start = request.getParameter("start"); List<Track> tracks = movie.getTracks(); movie.setTracks(new LinkedList<Track>()); double endTime = (double) tracks.get(0).getDuration() / tracks.get(0).getTrackMetaData().getTimescale(); if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) { if (timeCorrected) { long endSample = -1; for (int i = 0; i < track.getSampleDurations().length; i++) { long delta = track.getSampleDurations()[i]; currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale(); currentSample++; movie.addTrack(new ClippedTrack(track, startSample, endSample)); Container out = new DefaultMp4Builder().build(movie); response.setHeader("content-type", "video/mp4"); WritableByteChannel reponse = Channels.newChannel(response.getOutputStream());
public static void main(String[] args) throws IOException { Movie m = MovieCreator.build("C:\\dev\\mp4parser\\schaf-schleppt-knüppel.mp4"); Track videoTrack = null; for (Track track : m.getTracks()) { if ("vide".equals(track.getHandler())) { videoTrack = track; int refNumSamples = videoTrack.getSamples().size(); long[] refSampleDuration = videoTrack.getSampleDurations(); long[] syncSamples = videoTrack.getSyncSamples(); double[] syncSampleTimes = new double[0]; int refIndex = 0; for (long syncSample : syncSamples) { while (refIndex < syncSample - 1 && refIndex < refNumSamples) { refTime += (double) refSampleDuration[refIndex] / videoTrack.getTrackMetaData().getTimescale(); refIndex++; int numSamples = track.getSamples().size(); long[] durations = track.getSampleDurations(); double time = 0; int timeIndex = 0; if (timeIndex >= syncSampleTimes.length) { chops.add(new ClippedTrack(track, lastStart, numSamples)); System.err.println("Added partial track for " + track.getTrackMetaData().getTrackId() + " from sample " + lastStart + " to " + numSamples); break; if (lastStart != index) { chops.add(new ClippedTrack(track, lastStart, index));
public static void main(String[] args) throws IOException { Movie mWithVideo = MovieCreator.build("C:\\dev\\mp4parser\\examples\\src\\main\\resources\\davidappend\\v1.mp4"); Movie mWOutVideo = new Movie(); for (Track track : mWithVideo.getTracks()) { if (track.getHandler().equals("soun")) { mWOutVideo.addTrack(track); } } DefaultMp4Builder b = new DefaultMp4Builder(); Container c = b.build(mWOutVideo); c.writeContainer(new FileOutputStream("output.mp4").getChannel()); } }
for (Track track : m.getTracks()) { if (track.getHandler().endsWith("vide")) { Movie vide = new Movie(Collections.singletonList(track)); DefaultMp4Builder builder = new DefaultMp4Builder(); builder.build(vide).writeContainer(new RandomAccessFile("vide_" + track.getTrackMetaData().getTrackId() + ".mp4", "rw").getChannel()); if (track.getHandler().endsWith("soun")) { Movie vide = new Movie(Collections.singletonList(track)); DefaultMp4Builder builder = new DefaultMp4Builder(); builder.build(vide).writeContainer(new RandomAccessFile("soun_" + track.getTrackMetaData().getTrackId() + ".mp4", "rw").getChannel()); if (track.getHandler().endsWith("subt")) { for (int i = 0; i < track.getSamples().size(); i++) { File f = new File("subtitle_" + track.getTrackMetaData().getTrackId() + "_" + i + ".xml"); f.delete(); RandomAccessFile raf = new RandomAccessFile(f, "rw"); SubSampleInformationBox subs = track.getSubsampleInformationBox(); int j = 0; ByteBuffer xmlSamplePart = null; if (subsampleIter.hasNext()) { SubSampleInformationBox.SubSampleEntry.SubsampleEntry xmlSubSampleEntry = subsampleIter.next(); ByteBuffer sample = track.getSamples().get(i).asByteBuffer(); xmlSamplePart = (ByteBuffer) sample.slice().limit(l2i(xmlSubSampleEntry.getSubsampleSize())); raf.getChannel().write(xmlSamplePart); sample = ((ByteBuffer) sample.position(l2i(picSubSampleEntry.getSubsampleSize()))).slice(); FileOutputStream fosPic = new FileOutputStream( "subtitle_" + track.getTrackMetaData().getTrackId() + "_" + i + "_" + imageNames.get(p++).replace(":", "_"));
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 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(); }
mvhd.setMatrix(movie.getMatrix()); long movieTimeScale = getTimescale(movie); long duration = 0; for (Track track : movie.getTracks()) { long tracksDuration; if (track.getEdits() == null || track.getEdits().isEmpty()) { tracksDuration = (track.getDuration() * movieTimeScale / track.getTrackMetaData().getTimescale()); } else { double d = 0; for (Edit edit : track.getEdits()) { d += (long) edit.getSegmentDuration(); for (Track track : movie.getTracks()) { nextTrackId = nextTrackId < track.getTrackMetaData().getTrackId() ? track.getTrackMetaData().getTrackId() : nextTrackId; for (Track track : movie.getTracks()) { movieBox.addBox(createTrackBox(track, movie, chunks)); ParsableBox udta = createUdta(movie); if (udta != null) { movieBox.addBox(udta);
public static void main(String[] args) throws IOException { String audioDeutsch = MuxMp4SourcesExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-deutsch-audio.mp4"; String audioEnglish = MuxMp4SourcesExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-english-audio.mp4"; String video = MuxMp4SourcesExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/count-video.mp4"; Movie countVideo = MovieCreator.build(video); Movie countAudioDeutsch = MovieCreator.build(audioDeutsch); Movie countAudioEnglish = MovieCreator.build(audioEnglish); Track audioTrackDeutsch = countAudioDeutsch.getTracks().get(0); audioTrackDeutsch.getTrackMetaData().setLanguage("deu"); Track audioTrackEnglish = countAudioEnglish.getTracks().get(0); audioTrackEnglish.getTrackMetaData().setLanguage("eng"); countVideo.addTrack(audioTrackDeutsch); countVideo.addTrack(audioTrackEnglish); { Container out = new DefaultMp4Builder().build(countVideo); FileOutputStream fos = new FileOutputStream(new File("output.mp4")); out.writeContainer(fos.getChannel()); fos.close(); } { FragmentedMp4Builder fragmentedMp4Builder = new FragmentedMp4Builder(); fragmentedMp4Builder.setFragmenter(new SyncSampleIntersectFinderImpl(countVideo, null, -1)); Container out = fragmentedMp4Builder.build(countVideo); FileOutputStream fos = new FileOutputStream(new File("output-frag.mp4")); out.writeContainer(fos.getChannel()); fos.close(); } }
tkhd.setMatrix(track.getTrackMetaData().getMatrix()); tkhd.setAlternateGroup(track.getTrackMetaData().getGroup()); tkhd.setCreationTime(track.getTrackMetaData().getCreationTime()); if (track.getEdits() == null || track.getEdits().isEmpty()) { tkhd.setDuration(track.getDuration() * getTimescale(movie) / track.getTrackMetaData().getTimescale()); } else { long d = 0; for (Edit edit : track.getEdits()) { d += (long) edit.getSegmentDuration(); tkhd.setDuration(d * track.getTrackMetaData().getTimescale()); tkhd.setHeight(track.getTrackMetaData().getHeight()); tkhd.setWidth(track.getTrackMetaData().getWidth()); tkhd.setLayer(track.getTrackMetaData().getLayer()); tkhd.setModificationTime(new Date()); tkhd.setTrackId(track.getTrackMetaData().getTrackId()); tkhd.setVolume(track.getTrackMetaData().getVolume()); mdhd.setCreationTime(track.getTrackMetaData().getCreationTime()); mdhd.setDuration(track.getDuration()); mdhd.setTimescale(track.getTrackMetaData().getTimescale()); mdhd.setLanguage(track.getTrackMetaData().getLanguage()); mdia.addBox(mdhd); HandlerBox hdlr = new HandlerBox(); mdia.addBox(hdlr);
public static void main(String[] args) throws IOException { Movie m = MovieCreator.build("C:\\dev\\mp4parser\\examples\\src\\main\\resources\\1365070453555.mp4"); for (Track track : m.getTracks()) { track.getEdits().clear();; track.getEdits().add(new Edit(0, 1, 1, 2.0)); } new DefaultMp4Builder().build(m).writeContainer(new FileOutputStream("output.mp4").getChannel()); } }
private static long findNextSyncSample(Track track, double cutHere) { long currentSample = 0; double currentTime = 0; long[] durations = track.getSampleDurations(); long[] syncSamples = track.getSyncSamples(); for (int i = 0; i < durations.length; i++) { long delta = durations[i]; if ((syncSamples == null || syncSamples.length > 0 || Arrays.binarySearch(syncSamples, currentSample + 1) >= 0) && currentTime > cutHere) { return i; } currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale(); currentSample++; } return currentSample; }
public static void main(String[] args) throws IOException { Movie m = MovieCreator.build("C:\\Users\\sannies\\Downloads\\3ae39746-7e83-4653-860b-78a59e6ef474 (3).mp4"); for (Track track : m.getTracks()) { System.err.print(track.getHandler()); System.err.print(track.getSamples().size()); } } }
private static long calculateTracktimesScalingFactor(Movie m, Track track) { long timeScale = 1; for (Track track1 : m.getTracks()) { if (track1.getHandler().equals(track.getHandler())) { if (track1.getTrackMetaData().getTimescale() != track.getTrackMetaData().getTimescale()) { timeScale = lcm(timeScale, track1.getTrackMetaData().getTimescale()); } } } return timeScale; }
/** * Calculates the timestamp of all tracks' sync samples. * * @param movie <code>track</code> is located in this movie * @param track get this track's samples timestamps * @return a list of timestamps */ public static List<long[]> getSyncSamplesTimestamps(Movie movie, Track track) { List<long[]> times = new LinkedList<long[]>(); for (Track currentTrack : movie.getTracks()) { if (currentTrack.getHandler().equals(track.getHandler())) { long[] currentTrackSyncSamples = currentTrack.getSyncSamples(); if (currentTrackSyncSamples != null && currentTrackSyncSamples.length > 0) { final long[] currentTrackTimes = getTimes(currentTrack, movie); times.add(currentTrackTimes); } } } return times; }
public static void main(String[] args) throws IOException { Movie m = MovieCreator.build("c:\\content\\big_buck_bunny_1080p_h264-2min-handbraked.mp4"); double movieDuration = 0; for (Track track : m.getTracks()) { movieDuration = Math.max((double) track.getDuration() / track.getTrackMetaData().getTimescale(), movieDuration); } // We got the full duration in seconds System.err.println("Bitrate in bit/s: " + (new File("c:\\content\\big_buck_bunny_1080p_h264-2min-handbraked.mp4").length() * 8 /movieDuration)); } }
protected void createTfhd(long startSample, long endSample, Track track, int sequenceNumber, TrackFragmentBox parent) { TrackFragmentHeaderBox tfhd = new TrackFragmentHeaderBox(); SampleFlags sf = new SampleFlags(); tfhd.setDefaultSampleFlags(sf); tfhd.setBaseDataOffset(-1); tfhd.setSampleDescriptionIndex(track.getSampleEntries().indexOf(track.getSamples().get(l2i(startSample)).getSampleEntry()) + 1); tfhd.setTrackId(track.getTrackMetaData().getTrackId()); tfhd.setDefaultBaseIsMoof(true); parent.addBox(tfhd); }
/** * Changes the time scale of the source track to the target time scale and makes sure * that any rounding errors that may have summed are corrected exactly before the syncSamples. * * @param source the source track * @param targetTimeScale the resulting time scale of this track. * @param syncSamples at these sync points where rounding error are corrected. */ public ChangeTimeScaleTrack(Track source, long targetTimeScale, long[] syncSamples) { this.source = source; this.timeScale = targetTimeScale; double timeScaleFactor = (double) targetTimeScale / source.getTrackMetaData().getTimescale(); ctts = adjustCtts(source.getCompositionTimeEntries(), timeScaleFactor); decodingTimes = adjustTts(source.getSampleDurations(), timeScaleFactor, syncSamples, getTimes(source, syncSamples, targetTimeScale)); }