android与MP4Parser问题合并

时间:2016-02-02 21:31:38

标签: android video mp4parser

我遇到了两个视频合并的问题。当我尝试合并来自不同横向模式(左/右)的两个视频时,其中一个视频正在颠倒显示。我正在使用MP4Parser作为库来管理修剪和合并

   protected void combineClips() throws IOException {

    MediaMetadataRetriever m = new MediaMetadataRetriever();
    for (int i = 0; i < paths.size(); i++) {
        m.setDataSource(paths.get(i));
        if (Build.VERSION.SDK_INT >= 17) {
            String s = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
            if (Integer.valueOf(s) == 0) {
                isLeftLandscape = true;
            }
            if (Integer.valueOf(s) == 180) {
                isRightLandscape = true;
            }
        }
    }


    for (int i = 0; i < paths.size(); i++) {
        Movie tm = MovieCreator.build(paths.get(i));
        Log.d(TAG, files.size() + "files");
        files.add(tm);
    }

    if (isLeftLandscape && isRightLandscape) {
        for (int i = 0; i < paths.size(); i++) {
            m.setDataSource(paths.get(i));
            if (Build.VERSION.SDK_INT >= 17) {
                String s = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
                if (Integer.valueOf(s) == 0)
                {
              Log.d(TAG, "the rotation is " + s);
                    rotate(paths.get(i), i);
            }}
        }
    }

    List<Track> videoTracks = new ArrayList<Track>();
    List<Track> audioTracks = new ArrayList<Track>();
    for (Movie movie : files) {
        for (Track t : movie.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) {
        Log.d(TAG, String.valueOf(audioTracks.size()) + "audi");
        result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    }
    if (videoTracks.size() > 0) {
        Log.d(TAG, String.valueOf(audioTracks.size()) + "vide");
        result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
    }


    Container out = new DefaultMp4Builder().build(result);
    String folder_main = "VidApp";
    File f = new File(Environment.getExternalStorageDirectory(), folder_main);
    if (!f.exists()) {
        f.mkdirs();
    }
    FileChannel fc = new RandomAccessFile(String.format(System.getenv("EXTERNAL_STORAGE") + "/VidApp" + "/output.mp4"), "rw").getChannel();
    out.writeContainer(fc);

    fc.close();
    while (!audioTracks.isEmpty()) {
        audioTracks.remove(0);
    }
    while (!videoTracks.isEmpty()) {
        videoTracks.remove(0);
    }
    while (!files.isEmpty()) {
        files.remove(0);
    }
}




public void rotate(String path, int position) throws IOException {
    IsoFile isoFile = new IsoFile(path);
    Movie m = new Movie();

    List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(
            TrackBox.class);

    for (TrackBox trackBox : trackBoxes) {

        trackBox.getTrackHeaderBox().setMatrix(Matrix.ROTATE_180);
        m.addTrack(new Mp4TrackImpl(trackBox));
    }
    Log.d(TAG, position + " of " + files.size());
    files.set(position, m);
}

0 个答案:

没有答案