Android上的MP4Parser库问题

时间:2014-09-02 06:14:19

标签: android video mp4parser

我正在使用 isoparser-1.0-RC-2.jar aspectjrt-1.7.3.jar 在Android上合并 mp4 视频。 它在 Nexus4 上工作正常但不能正常使用所有 android 设备。如果我使用更高版本的isoparser,则它无法在所有设备上运行。

private class MergeVideos extends AsyncTask<String, Integer, String> {

    //The working path where the video files are located
    private String workingPath; 
    //The file names to merge
    private ArrayList<String> videosToMerge;
    //Dialog to show to the user
    private ProgressDialog progressDialog;

    private MergeVideos(String workingPath, ArrayList<String> videosToMerge) {
        this.workingPath = workingPath;
        this.videosToMerge = videosToMerge;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context,
                "Merging videos", "Please wait...", true);
    };

    @Override
    protected String doInBackground(String... params) {
        int count = videosToMerge.size();
        try {
            Movie[] inMovies = new Movie[count];
            for (int i = 0; i < count; i++) {
                File file = new File(workingPath, videosToMerge.get(i));
                if(file.exists()) {
                    FileInputStream fis = new FileInputStream(file);
                    FileChannel fc = fis.getChannel();
                    inMovies[i] = MovieCreator.build(fc);
                    fis.close();
                    fc.close();
                }
            }
            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);
                    }
                    if (t.getHandler().equals("")) {

                    }
                }
            }

            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()])));
            }
            IsoFile out = new DefaultMp4Builder()
            .build(result);

            //rotate video

            out.getMovieBox().getMovieHeaderBox().setMatrix(ROTATE_270);

            long timestamp=new Date().getTime();
            String timestampS="" + timestamp;

            File storagePath = new File(workingPath);             
            storagePath.mkdirs();  

            File myMovie = new File(storagePath, String.format("output-%s.mp4", timestampS)); 

            FileOutputStream fos = new FileOutputStream(myMovie);
            FileChannel fco = fos.getChannel();
            fco.position(0);
            out.getBox(fco);
            fco.close();
            fos.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String mFileName = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        mFileName += "/output.mp4";
        return mFileName;
    }

    @Override
    protected void onPostExecute(String value) {
        super.onPostExecute(value);
        progressDialog.dismiss();
    }

}

0 个答案:

没有答案