【问题标题】:Joining two mp3 files into one将两个 mp3 文件合并为一个
【发布时间】:2014-06-13 19:10:56
【问题描述】:

我有这个代码来读取字节到另一个文件。 但我无法将两个 mp3 文件合并为一个。 我错过了什么吗?

public static void main(String[] args) {
  String strFileName = ("D:/Music/Assb/Love.mp3");
                BufferedOutputStream bos = null;

                try
                {
                        //create an object of FileOutputStream
                        FileOutputStream fos = new FileOutputStream(new File(strFileName));

                        //create an object of BufferedOutputStream
                        bos = new BufferedOutputStream(fos);

                        String str = "D:/Music/Assembled/Heart001.mp3" 
                            + "D:/Music/Assembled/Heart002.mp3";

                        /*
                         * To write byte array to file use,
                         * public void write(byte[] b) method of BufferedOutputStream
                         * class.
                         */
                         System.out.println("Writing byte array to file");

                         bos.write(str.getBytes());

                        System.out.println("File written");

【问题讨论】:

  • 您只是想将文件连接成一个大文件,还是想“混合”它们,让一个文件播放 2 首歌曲?
  • 我想拼接成一个大文件。有可能吗?
  • 您的str 不包含合法文件名。
  • 抱歉,您所说的合法​​是什么意思?
  • mp3 文件不同于简单的文本文件。它是压缩的,由帧组成,帧本身有标题。为了合并两个 mp3 文件,您可能需要先解压缩,合并,然后再压缩,因为压缩不相等或帧包含彼此不兼容的数据,请参阅mp3 file specification。跨度>

标签: java mp3


【解决方案1】:

真是糟透了。 Mp3 文件以标题开头。为了正确合并,您必须跳过前 32 个字节。试试这个。

 try {
            FileInputStream fistream1 = new FileInputStream(_file_name);
            File f = new File(new File(_file_name).getParent()+"/final.mp3");
            if(!f.exists())
            {
                f.createNewFile();
            }
            FileOutputStream sistream = new FileOutputStream((new File(_file_name)).getParent()+"/final.mp3");
            int temp;
            int size = 0;
            temp = fistream1.read();
            while( temp != -1)
            {
                sistream.write(temp);
                temp = fistream1.read();
            };
            fistream1.close();
            FileInputStream fistream2 = new FileInputStream(temp_file);
            fistream2.read(new byte[32],0,32);
            temp = fistream2.read();
            while( temp != -1)
            {
                sistream.write(temp);
                temp = fistream2.read();
            };
            fistream2.close();
            sistream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

【讨论】:

    【解决方案2】:

    您需要分两步完成此操作

    String str = "D:/Music/Assembled/Heart001.mp3";
    
    >>> ADD code to open the file given by str <<<<
    bos.write(strFile.getBytes());
    >>> Add code to close the file
    
    
    str = "D:/Music/Assembled/Heart002.mp3";
    >>> ADD code to open the file given by str <<<<
    bos.write(strFile.getBytes());
    >>> Add code to close the file
    

    如您所见,您需要代码来打开 mp3 文件来阅读它

    【讨论】:

    • 那么我做完上面的代码就可以合并成一个文件了吗?
    • 我想是的。但是 mp3 可能没用,无法播放
    • hmm.. 我需要它来玩.. java 中有什么可以用来做这个的吗?
    • 看看this SO thread,它的功能和你需要的一样
    【解决方案3】:

    您在尝试什么...实际上..如果您想读取 2 个文件以字节流,请不要 String str = "D:/Music/Assembled/Heart001.mp3" + "D:/Music/Assembled/Heart002.mp3"; 制作 str1=D:/Music/Assembled/Heart001.mp3str2=D:/Music/Assembled/Heart002.mp3 并通过 bufferedoutputsream 分别读取 str1,str2

    【讨论】:

    • 结果相同。我需要它来合并两个 mp3 文件的数据,并在合并后能够播放
    【解决方案4】:

    此代码将运行良好,并在几秒钟内合并类似类型的音频...

    try {
    
    
                       InputStream in = new FileInputStream("C:\\a.mp3");//firstmp3
                        byte[] buffer = new byte[1 << 20];  // loads 1 MB of the file
                        OutputStream os = new FileOutputStream(new File("C:\\output.mp3", true);//output mp3
                        int count;
                        while ((count = in.read(buffer)) != -1) {
                            os.write(buffer, 0, count);
                            os.flush();
                        }
                        in.close();
                        in = new FileInputStream("C:\\b.mp3");//second mp3
                        while ((count = in.read(buffer)) != -1) {
                            os.write(buffer, 0, count);
                            os.flush();
                        }
                        in.close();
                        os.close();
    
    
    
    
                   } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    

    【讨论】:

    • 不起作用...它只是连接两个文件并忽略标题等