简体   繁体   English

如何将两个 mp3 文件合并为一个(合并/加入)

[英]How to merge two mp3 files into one (combine/join)

Can any tell how to combine/merge two media files into one?谁能告诉如何将两个媒体文件合并/合并为一个?

i found a topics about audioInputStream but now it's not supported in android, and all code for java.我找到了一个关于audioInputStream的主题,但现在 android 和 java 的所有代码都不支持它。

And on StackOverflow i found this link here but there i can't find solution - these links only on streaming audio.在 StackOverflow 上,我在这里找到了这个链接,但在那里我找不到解决方案 - 这些链接仅在流式音频上。 Any one can tell me?有谁能告诉我?

PS and why i can't start bounty?:( PS,为什么我不能开始赏金?:(

import java.io.*;
public class TwoFiles
{
    public static void main(String args[]) throws IOException
    {
        FileInputStream fistream1 = new FileInputStream("C:\\Temp\\1.mp3");  // first source file
        FileInputStream fistream2 = new FileInputStream("C:\\Temp\\2.mp3");//second source file
        SequenceInputStream sistream = new SequenceInputStream(fistream1, fistream2);
        FileOutputStream fostream = new FileOutputStream("C:\\Temp\\final.mp3");//destinationfile

        int temp;

        while( ( temp = sistream.read() ) != -1)
        {
            // System.out.print( (char) temp ); // to print at DOS prompt
            fostream.write(temp);   // to write to file
        }
        fostream.close();
        sistream.close();
        fistream1.close();
        fistream2.close();
    }
}

Consider two cases for .mp3 files:考虑.mp3文件的两种情况:

  • Files with same sampling frequency and number of channels具有相同采样频率和通道数的文件

In this case, we can just append the second file to end of first file.在这种情况下,我们可以将第二个文件 append 放在第一个文件的末尾。 This can be achieved using File classes available on Android.这可以使用 Android 上可用的文件类来实现。

  • Files with different sampling frequency or number of channels.具有不同采样频率或通道数的文件。

In this case, one of the clips has to be re-encoded to ensure both files have same sampling frequency and number of channels.在这种情况下,必须对其中一个剪辑进行重新编码,以确保两个文件具有相同的采样频率和通道数。 To do this, we would need to decode MP3, get PCM samples,process it to change sampling frequency and then re-encode to MP3.为此,我们需要解码 MP3,获取 PCM 样本,对其进行处理以更改采样频率,然后重新编码为 MP3。 From what I know, android does not have transcode or reencode APIs.据我所知,android 没有转码或重新编码 API。 One option is to use external library like lame/FFMPEG via JNI for re-encode.一种选择是通过 JNI 使用像 lame/FFMPEG 这样的外部库进行重新编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM