Skip to content

Commit

Permalink
merge #master
Browse files Browse the repository at this point in the history
  • Loading branch information
sannies committed Oct 7, 2015
2 parents be490de + 32cd9f4 commit aa632e7
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 78 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -4,4 +4,10 @@
*.jar
*.war
*.ear
.idea
*.iml

examples/target
isoparser/target
isoparser/aac-sample.mp4
isoparser/ac3-sample.mp4
103 changes: 95 additions & 8 deletions examples/src/main/java/com/googlecode/mp4parser/AppendExample.java
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -20,14 +21,100 @@ public class AppendExample {
public static void main(String[] args) throws IOException {


String f1 = "C:\\Users\\sannies\\Downloads\\merge_73677.mp4";
String f2 = "C:\\Users\\sannies\\Downloads\\rsmedia-test(1).mp4";
//String f2 = AppendExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070285923.mp4";
//String f3 = AppendExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070453555.mp4";

Movie[] inMovies = new Movie[]{
MovieCreator.build(f1),
MovieCreator.build(f2)};
String[] videoUris = new String[]{

"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20150930_161515.mp4",
"c:\\content\\20150930_161525.mp4",
"c:\\content\\20150930_161529.mp4",
"c:\\content\\20150930_161534.mp4",
"c:\\content\\20150930_161543.mp4",
"c:\\content\\20151001_135436.mp4",
"c:\\content\\20151001_135446.mp4",
"c:\\content\\20151001_135540.mp4"

};

List<Movie> inMovies = new ArrayList<Movie>();
for (String videoUri : videoUris) {
inMovies.add(MovieCreator.build(videoUri));
}

List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
Expand Down
@@ -1,5 +1,7 @@
package org.mp4parser.muxer;

import com.googlecode.mp4parser.util.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -9,6 +11,7 @@
import java.nio.channels.WritableByteChannel;

public class FileDataSourceImpl implements DataSource {
private static Logger LOG = Logger.getLogger(FileDataSourceImpl.class);
FileChannel fc;
String filename;

Expand Down Expand Up @@ -56,6 +59,7 @@ public synchronized long transferTo(long startPosition, long count, WritableByte
}

public synchronized ByteBuffer map(long startPosition, long size) throws IOException {
LOG.logDebug(startPosition + " " + size);
return fc.map(FileChannel.MapMode.READ_ONLY, startPosition, size);
}

Expand Down
@@ -0,0 +1,79 @@
package org.mp4parser.muxer;

import com.googlecode.mp4parser.util.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

import static com.googlecode.mp4parser.util.CastUtils.l2i;


public class FileDataSourceViaHeapImpl implements DataSource {
private static Logger LOG = Logger.getLogger(FileDataSourceViaHeapImpl.class);
FileChannel fc;
String filename;


public FileDataSourceViaHeapImpl(File f) throws FileNotFoundException {
this.fc = new FileInputStream(f).getChannel();
this.filename = f.getName();
}

public FileDataSourceViaHeapImpl(String f) throws FileNotFoundException {
File file = new File(f);
this.fc = new FileInputStream(file).getChannel();
this.filename = file.getName();
}


public FileDataSourceViaHeapImpl(FileChannel fc) {
this.fc = fc;
this.filename = "unknown";
}

public FileDataSourceViaHeapImpl(FileChannel fc, String filename) {
this.fc = fc;
this.filename = filename;
}

public synchronized int read(ByteBuffer byteBuffer) throws IOException {
return fc.read(byteBuffer);
}

public synchronized long size() throws IOException {
return fc.size();
}

public synchronized long position() throws IOException {
return fc.position();
}

public synchronized void position(long nuPos) throws IOException {
fc.position(nuPos);
}

public synchronized long transferTo(long startPosition, long count, WritableByteChannel sink) throws IOException {
return fc.transferTo(startPosition, count, sink);
}

public synchronized ByteBuffer map(long startPosition, long size) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(l2i(size));
fc.read(bb, startPosition);
return (ByteBuffer) bb.rewind();
}

public void close() throws IOException {
fc.close();
}

@Override
public String toString() {
return filename;
}

}
Expand Up @@ -27,17 +27,17 @@
import org.mp4parser.muxer.Sample;
import org.mp4parser.muxer.Track;
import org.mp4parser.muxer.tracks.CencEncryptedTrack;
import org.mp4parser.support.Logger;
import org.mp4parser.tools.IsoTypeWriter;
import org.mp4parser.tools.Mp4Arrays;
import org.mp4parser.tools.Offsets;
import org.mp4parser.tools.Path;


import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.mp4parser.tools.CastUtils.l2i;
import static org.mp4parser.tools.Mp4Math.lcm;
Expand All @@ -47,7 +47,7 @@
*/
public class DefaultMp4Builder implements Mp4Builder {

private static Logger LOG = Logger.getLogger(DefaultMp4Builder.class.getName());
private static Logger LOG = Logger.getLogger(DefaultMp4Builder.class);
Map<Track, StaticChunkOffsetBox> chunkOffsetBoxes = new HashMap<Track, StaticChunkOffsetBox>();
Set<SampleAuxiliaryInformationOffsetsBox> sampleAuxiliaryInformationOffsetsBoxes = new HashSet<SampleAuxiliaryInformationOffsetsBox>();
HashMap<Track, List<Sample>> track2Sample = new HashMap<Track, List<Sample>>();
Expand Down Expand Up @@ -83,7 +83,7 @@ public Container build(Movie movie) {
if (fragmenter == null) {
fragmenter = new TimeBasedFragmenter(2);
}
LOG.fine("Creating movie " + movie);
LOG.logDebug("Creating movie " + movie);
for (Track track : movie.getTracks()) {
// getting the samples may be a time consuming activity
List<Sample> samples = track.getSamples();
Expand Down Expand Up @@ -114,14 +114,15 @@ public Container build(Movie movie) {
contentSize += sum(stsz.getSampleSizes());

}

LOG.logDebug("About to create mdat");
InterleaveChunkMdat mdat = new InterleaveChunkMdat(movie, chunks, contentSize);

long dataOffset = 16;
for (Box lightBox : isoFile.getBoxes()) {
dataOffset += lightBox.getSize();
}
isoFile.addBox(mdat);
LOG.logDebug("mdat crated");

/*
dataOffset is where the first sample starts. In this special mdat the samples always start
Expand Down Expand Up @@ -306,7 +307,7 @@ protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> ch
ParsableBox stbl = createStbl(track, movie, chunks);
minf.addBox(stbl);
mdia.addBox(minf);

LOG.logDebug("done with trak for track_" + track.getTrackMetaData().getTrackId());
return trackBox;
}

Expand Down Expand Up @@ -344,6 +345,7 @@ protected ParsableBox createStbl(Track track, Movie movie, Map<Track, int[]> chu
createStsz(track, stbl);
createStco(track, movie, chunks, stbl);


Map<String, List<GroupEntry>> groupEntryFamilies = new HashMap<String, List<GroupEntry>>();
for (Map.Entry<GroupEntry, long[]> sg : track.getSampleGroups().entrySet()) {
String type = sg.getKey().getType();
Expand Down Expand Up @@ -385,7 +387,7 @@ protected ParsableBox createStbl(Track track, Movie movie, Map<Track, int[]> chu
createCencBoxes((CencEncryptedTrack) track, stbl, chunks.get(track));
}
createSubs(track, stbl);

LOG.logDebug("done with stbl for track_" + track.getTrackMetaData().getTrackId());
return stbl;
}

Expand Down Expand Up @@ -452,9 +454,7 @@ protected void createStco(Track targetTrack, Movie movie, Map<Track, int[]> chun

long offset = 0;
// all tracks have the same number of chunks
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Calculating chunk offsets for track_" + targetTrack.getTrackMetaData().getTrackId());
}
LOG.logDebug("Calculating chunk offsets for track_" + targetTrack.getTrackMetaData().getTrackId());

List<Track> tracks = new ArrayList<Track>(chunks.keySet());
Collections.sort(tracks, new Comparator<Track>() {
Expand Down Expand Up @@ -496,9 +496,10 @@ public int compare(Track o1, Track o2) {
int startSample = trackToSample.get(nextChunksTrack);
double time = trackToTime.get(nextChunksTrack);

long[] durs = nextChunksTrack.getSampleDurations();
for (int j = startSample; j < startSample + numberOfSampleInNextChunk; j++) {
offset += track2SampleSizes.get(nextChunksTrack)[j];
time += (double) nextChunksTrack.getSampleDurations()[j] / nextChunksTrack.getTrackMetaData().getTimescale();
time += (double) durs[j] / nextChunksTrack.getTrackMetaData().getTimescale();
}
trackToChunk.put(nextChunksTrack, nextChunksIndex + 1);
trackToSample.put(nextChunksTrack, startSample + numberOfSampleInNextChunk);
Expand Down Expand Up @@ -705,9 +706,19 @@ public void getBox(WritableByteChannel writableByteChannel) throws IOException {
}
bb.rewind();
writableByteChannel.write(bb);
long writtenBytes = 0;
long writtenMegaBytes = 0;

LOG.logDebug("About to write " + contentSize);
for (List<Sample> samples : chunkList) {
for (Sample sample : samples) {
sample.writeTo(writableByteChannel);
writtenBytes += sample.getSize();
if (writtenBytes > 1024 * 1024) {
writtenBytes -= 1024 * 1024;
writtenMegaBytes++;
LOG.logDebug("Written " + writtenMegaBytes + "MB");
}
}
}

Expand Down

0 comments on commit aa632e7

Please sign in to comment.