【问题标题】:How to concat mp4 files?如何连接 mp4 文件?
【发布时间】:2014-06-01 12:19:29
【问题描述】:

我想连接 2 个 mp4 音频文件。我厌倦了使用https://code.google.com/p/mp4parser/
我想要实现的目标:我想连接 f1 和 f2 并将其保存到 f2。如果 f2 不存在 - f2 = f1。基本上将 f1 附加到 f2。
但是,当我使用 mergeSongs() 时,生成的音频文件会重复第一个音频文件(f1)
常量

private static String mFileNameFromRec = null;
    private static String mFileNameToUse = null;
    mFileNameFromRec = context.getCacheDir().getAbsolutePath();
        mFileNameFromRec += "/audiorecordtest.mp4";
        mFileNameToUse = context.getCacheDir().getAbsolutePath();
        mFileNameToUse += "/audioToUse.mp4";

我的方法如下:

 private void mergeSongs() throws Exception {
            String f1 = mFileNameFromRec;
            String f2 = mFileNameToUse;
            File merge = new File(mFileNameToUse);
            if (!merge.exists()) {
                InputStream in = new FileInputStream(new File(f1));
                OutputStream out = new FileOutputStream(new File(f2));

                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
                Log.d("audio concatenation","was copied");

            } else {
                Movie[] inMovies = null;

                inMovies = new Movie[] { MovieCreator.build(f1),
                        MovieCreator.build(f2) };

                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")) {
                            Log.d("audio concatenation","add audio track: "+ t.toString());
                            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);
                File newAudio = new File(mFileNameToUse);
                FileOutputStream fOut = new FileOutputStream(newAudio);
                FileChannel fc = fOut.getChannel();
                // FileChannel fc = new RandomAccessFile(mFileNameToUse, "rw")
                // .getChannel();

                out.writeContainer(fc);

                fc.close();
                fOut.flush();
                fOut.close();
                Log.d("audio concatenation","was concated");
            }
        }

【问题讨论】:

  • 在带有命令行的 linux/windows 中使用本机工具,并从 java 调用它,这不是最好的方法,但我怀疑你会找到所有编码的纯 java impl
  • 抱歉忘记添加android标签了。

标签: java android


【解决方案1】:

并且有有效的解决方案:
使用库https://code.google.com/p/mp4parser/

下载罐子

isoparser-1.0.1.jar
aspectjrt-1.7.4.jar

常量

mFileNameFromRec = context.getCacheDir().getAbsolutePath();
        mFileNameFromRec += "/audiorecordtest.mp4";
        mFileNameToUse = context.getCacheDir().getAbsolutePath();
        mFileNameToUse += "/audioToUse.mp4";

代码

private void mergeSongs() throws Exception {
        File merge = new File(mFileNameToUse);
        String f1 = mFileNameFromRec;
        String f2 = mFileNameToUse;
        if (!merge.exists()) {

            InputStream in = new FileInputStream(new File(f1));
            OutputStream out = new FileOutputStream(new File(f2));

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            Log.d("audio concatenation", "was copied");

        } else {

            Movie[] inMovies;

            inMovies = new Movie[] { MovieCreator.build(f2),
                    MovieCreator.build(f1), };

            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 (videoTracks.size() > 0) {
                result.addTrack(new AppendTrack(videoTracks
                        .toArray(new Track[videoTracks.size()])));
            }

            if (audioTracks.size() > 0) {
                result.addTrack(new AppendTrack(audioTracks
                        .toArray(new Track[audioTracks.size()])));
            }

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

            RandomAccessFile ram = new RandomAccessFile(String.format(context
                    .getCacheDir() + "/output.mp4"), "rw");
            FileChannel fc = ram.getChannel();
            out.writeContainer(fc);
            ram.close();
            fc.close();
            // IsoFile out = (IsoFile) new DefaultMp4Builder().build(result);
            // FileOutputStream fos = new FileOutputStream(new File(
            // String.format(context.getCacheDir() + "/output.mp4")));
            // out.getBox(fos.getChannel());
            // fos.close();
            String mergedFilepath = String.format(context.getCacheDir()
                    + "/output.mp4");

            copy(new File(mergedFilepath), new File(mFileNameToUse));
            Toast.makeText(getApplicationContext(), "success",
                    Toast.LENGTH_SHORT).show();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2011-11-12
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多