如何在核心Java代码中结合两首歌曲



我想以MP4,MP3,AVI和WMV等格式将歌曲组合到一个单个文件中,借助Java代码,就像我的服务器中一样,我只有JDK环境,到目前为止,我正在使用核心Java文件操作代码。如下。

    String str="E:\Movies\Dark Skies (2013)\Dark.mp4";
    String ftr="F:\CRS\stv.mp4";
    File file=new File(str);
    File Kile=new File(ftr);
    FileInputStream fis=new FileInputStream(file);
    FileOutputStream fos=new FileOutputStream(Kile);
    int luffersize=102400000;
    byte[] luffer=new byte[luffersize];
    int lenght=(int) fis.available();

    if ((fis.read(luffer)) != 0) {
            try {
                fos.write(luffer, 0, lenght);
                System.out.println("working");
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                fis.close();
                fos.close();
            }
        }
}

下面是我搅动的一些代码,以演示如何连接(顺序将)两个文件组合在一起。

这没有考虑MP4文件格式。我不熟悉MP4,但是MP3只是一系列具有任意ID3标头的512个字节块。如果您可以在第二个文件上脱下标题,则两个音乐文件的一般串联(即" Cat Song1.mp Song2.mp3> newsong.mp3")确实可靠地工作。但是我对MP4不熟悉,而且我很确定它可以支持各种编解码器。因此,使用此解决方案。否则,使用编解码器库进行正式解析和流式传输。

无论如何,这里有一些示例代码将两个文件结合在一起:

public void CombineFiles(String filename1, String filename2, String filenameOuput) throws IOException
{
    FileInputStream stream1 = new FileInputStream(filename1);
    FileInputStream stream2 = new FileInputStream(filename2);
    FileOutputStream streamOut = new FileOutputStream(filenameOuput);
    FileInputStream stream = stream1;
    byte [] buffer = new byte[8192]; // 8K temp buffer should suffice
    while (true)
    {
        int bytesread = stream.read(buffer);
        if (bytesread != -1)
        {
            streamOut.write(buffer, 0, bytesread);
        }
        else
        {
            // handle end of file and moving to the next file
            if (stream == stream2)
                break;
            else
                stream = stream2;
        }
    }
    streamOut.close();
    stream1.close();
    stream2.close();
}

最新更新