com.googlecode.mp4parser.authoring.tracks.CroppedTrack Java Examples

The following examples show how to use com.googlecode.mp4parser.authoring.tracks.CroppedTrack. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProcessVideoUtils.java    From patrol-android with GNU General Public License v3.0 4 votes vote down vote up
public static void startTrim(File src, File dst, double startTime, double endTime) throws IOException {
    FileDataSourceImpl file = new FileDataSourceImpl(src);
    Movie movie = MovieCreator.build(file);
    List<Track> tracks = movie.getTracks();
    movie.setTracks(new LinkedList<Track>());

    Log.d(TAG, "startTrim: " + startTime + " " + endTime);
    for (Track track : tracks) {
        long currentSample = 0;
        double currentTime = 0;
        long startSample = -1;
        long endSample = -1;
        for (int i = 0; i < track.getSampleDurations().length; i++) {
            if (currentTime <= startTime) {

                // current sample is still before the new starttime
                startSample = currentSample;
            }
            if (currentTime <= endTime) {
                // current sample is after the new start time and still before the new endtime
                endSample = currentSample;
            } else {
                // current sample is after the end of the cropped video
                break;
            }
            currentTime += (double) track.getSampleDurations()[i] / (double) track.getTrackMetaData().getTimescale();
            currentSample++;
        }
        movie.addTrack(new CroppedTrack(track, startSample, endSample));
    }
    Container out = new DefaultMp4Builder().build(movie);
    MovieHeaderBox mvhd = Path.getPath(out, "moov/mvhd");
    mvhd.setMatrix(Matrix.ROTATE_180);
    if (!dst.exists()) {
        dst.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(dst);
    WritableByteChannel fc = fos.getChannel();
    try {
        out.writeContainer(fc);
    }catch (Exception e){
        e.printStackTrace();
    } finally {
        fc.close();
        fos.close();
        file.close();
    }
}