Open XinYiWorld opened 6 years ago
would you create a small example for me to check? That cuts down on the time I need to reproduce and I can only invest a small amount of time into support of the project. Thank you.
I'm sorry to response you too late,Here is my source code 1.AppendVideo
public class AppendVideo {
public static void test() throws IOException {
String dstVideoPath = TFileHelper.getInstance().getRoot() + "/record/temp/test/";
String[] srcVideoPath = new String[]{
dstVideoPath + "v1.mp4",
dstVideoPath + "v2.mp4",
};
videoMerge(srcVideoPath, dstVideoPath);
}
public static void videoMerge(String[] srcVideoPath, String dstVideoPath) throws IOException {
List<Movie> inMovies = new ArrayList<Movie>();
for (String videoUri : srcVideoPath) {
inMovies.add(MovieCreator.build(videoUri));
}
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
FileChannel fc = new RandomAccessFile(String.format(dstVideoPath+"mergeOutput.mp4"), "rw").getChannel();
out.writeContainer(fc);
fc.close();
}
}
2.ShortenVideo
public class ShortenVideo {
private static final String TAG = "ShortenVideo";
public static void test() throws IOException {
String dstVideoPath = TFileHelper.getInstance().getRoot() + "/record/temp/test/";
String srcVideoPath = dstVideoPath + "v2.mp4";
String outVideoPath = dstVideoPath + "cutOutput.mp4";
cropMp4(srcVideoPath, 0, 2, outVideoPath);
}
/**
* 将 MP4 切割
*
* @param mp4Path .mp4
* @param fromSample 起始位置
* @param toSample 结束位置
* @param outPath .mp4
*/
public static void cropMp4(String mp4Path, long fromSample, long toSample, String outPath) throws IOException {
Movie movie = MovieCreator.build(mp4Path);
List<Track> tracks = movie.getTracks();
movie.setTracks(new LinkedList<Track>());
// remove all tracks we will create new tracks from the old
double startTime1 = fromSample;
double endTime1 = toSample;
boolean timeCorrected = false;
// Here we try to find a track that has sync samples. Since we can only start decoding
// at such a sample we SHOULD make sure that the start of the new fragment is exactly
// such a frame
for (Track track : tracks) {
if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
if (timeCorrected) {
// This exception here could be a false positive in case we have multiple tracks
// with sync samples at exactly the same positions. E.g. a single movie containing
// multiple qualities of the same video (Microsoft Smooth Streaming file)
throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
}
startTime1 = correctTimeToSyncSample(track, startTime1, false);
endTime1 = correctTimeToSyncSample(track, endTime1, true);
timeCorrected = true;
}
}
for (Track track : tracks) {
long currentSample = 0;
double currentTime = 0;
double lastTime = -1;
long startSample1 = -1;
long endSample1 = -1;
for (int i = 0; i < track.getSampleDurations().length; i++) {
long delta = track.getSampleDurations()[i];
if (currentTime > lastTime && currentTime <= startTime1) {
// current sample is still before the new starttime
startSample1 = currentSample;
}
if (currentTime > lastTime && currentTime <= endTime1) {
// current sample is after the new start time and still before the new endtime
endSample1 = currentSample;
}
lastTime = currentTime;
currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
currentSample++;
}
movie.addTrack(new AppendTrack(new CroppedTrack(track, startSample1, endSample1)));
}
long start1 = System.currentTimeMillis();
Container out = new DefaultMp4Builder().build(movie);
long start2 = System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream(outPath);
FileChannel fc = fos.getChannel();
out.writeContainer(fc);
fc.close();
fos.close();
long start3 = System.currentTimeMillis();
System.err.println("Building IsoFile took : " + (start2 - start1) + "ms");
System.err.println("Writing IsoFile took : " + (start3 - start2) + "ms");
//System.err.println("Writing IsoFile speed : " + (new File(String.format("output-%f-%f--%f-%f.mp4", startTime1, endTime1, startTime2, endTime2)).length() / (start3 - start2) / 1000) + "MB/s");
}
private static double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
long currentSample = 0;
double currentTime = 0;
for (int i = 0; i < track.getSampleDurations().length; i++) {
long delta = track.getSampleDurations()[i];
if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
// samples always start with 1 but we start with zero therefore +1
timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
}
currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
currentSample++;
}
double previous = 0;
for (double timeOfSyncSample : timeOfSyncSamples) {
if (timeOfSyncSample > cutHere) {
if (next) {
return timeOfSyncSample;
} else {
return previous;
}
}
previous = timeOfSyncSample;
}
return timeOfSyncSamples[timeOfSyncSamples.length - 1];
}
}
The problem happend when I use mp4parser make a new video(no mattar cut or merge), if I use the new video to merge or cut with any other video,the problem happens.
1.when i merge a new video created by mp4Parser(I cut from another video)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at com.googlecode.mp4parser.authoring.samples.DefaultMp4SampleList.<init>(DefaultMp4SampleList.java:85) at com.coremedia.iso.boxes.mdat.SampleList.<init>(SampleList.java:33) at com.googlecode.mp4parser.authoring.Mp4TrackImpl.<init>(Mp4TrackImpl.java:64) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:57) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:38) at com.xinyi.czsuperrecorder.encode.video.merge_cut.AppendVideo.videoMerge(AppendVideo.java:37) at com.xinyi.czsuperrecorder.encode.video.merge_cut.AppendVideo.test(AppendVideo.java:29) at com.xinyi.czsuperrecorder.ExampleInstrumentedTest.testAppendVideo(ExampleInstrumentedTest.java:60)
2.when i cut a new video created by mp4Parser(I cut from another video)
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 at com.googlecode.mp4parser.authoring.samples.DefaultMp4SampleList.<init>(DefaultMp4SampleList.java:85) at com.coremedia.iso.boxes.mdat.SampleList.<init>(SampleList.java:33) at com.googlecode.mp4parser.authoring.Mp4TrackImpl.<init>(Mp4TrackImpl.java:64) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:57) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:38)