使用mp4Parser合并两个MP4视频

时间:2016-07-28 06:12:47

标签: android mp4parser

我正在使用MP4Parser来合并两个MP4视频片段,但我在MovieCreator.build();中收到了一个例外。

public class MergeVideo extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        // do initialization of required objects objects here
    };

    @Override
    protected String doInBackground(String... params) {
        try {
            Movie[] inMovies = new Movie[path.size()];
            Log.d("MoviePaths",path.size()+"");
            for (int i = 0; i <path.size(); i++) {
                Log.d("FilePath",path.get(i)+"");
                inMovies[i] = MovieCreator.build(path.get(i));
            }

            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()])));
            }

            BasicContainer out = (BasicContainer) new DefaultMp4Builder()
                    .build(result);

            @SuppressWarnings("resource")
            FileChannel fc = new RandomAccessFile(String.format(Environment.getExternalStorageDirectory() + "/wishbyvideo.mp4"), "rw").getChannel();
            out.writeContainer(fc);
            fc.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 += "/wishbyvideo.mp4";

        return mFileName;
    }

    @Override
    protected void onPostExecute(String value) {
        super.onPostExecute(value);
        Log.d("File Name Final",value+"");
        progressDialog.dismiss();

    }
}

运行此代码时收到的异常如下:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)

Caused by: java.lang.NullPointerException
    at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:51)
    at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build(MovieCreator.java:38)
    at com.sourab.videorecorder.FFmpegRecorderActivity$MergeVideo.doInBackground(FFmpegRecorderActivity.java:1007)
    at com.sourab.videorecorder.FFmpegRecorderActivity$MergeVideo.doInBackground(FFmpegRecorderActivity.java:992)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
    at java.lang.Thread.run(Thread.java:856) 

我搜索了互联网并查看了不同的链接,提出了不同的解决方案,但似乎没有一个解决我的问题。任何帮助是极大的赞赏。提前谢谢。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 17
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.sourab.videorecorder"
        minSdkVersion 14
        targetSdkVersion 17
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile group: 'org.bytedeco', name: 'javacv', version: '1.1'
    compile 'com.googlecode.mp4parser:isoparser:1.0.5.4'
    compile group: 'org.bytedeco', name: 'javacv', version: '1.1'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0-1.1', classifier: 'android-arm'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0-1.1', classifier: 'android-x86'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.8.1-1.1', classifier: 'android-arm'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.8.1-1.1', classifier: 'android-x86'
}

1 个答案:

答案 0 :(得分:1)

这样做并确保视频和音轨具有一些值。它对我有用。

            final Container container = new DefaultMp4Builder().build(finalMovie);



        FileChannel fc = new RandomAccessFile(String.format(Environment.getExternalStorageDirectory().getAbsolutePath()+"/output.mp4"), "rw").getChannel();
        container.writeContainer(fc);
        fc.close();

依赖关系:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.googlecode.mp4parser:isoparser:1.1.18'

}

你可以克隆这个git repo用于工作示例

https://sandeep-agrawal@bitbucket.org/sandeep-agrawal/video-merger.git