I'm trying to concatenate a series of mp3 files into one mp3 file and failing miserably.
我正在尝试将一系列mp3文件连接成一个mp3文件,但失败得很惨。
As far as I can tell, the code I'm using is correct but it's creating a file that contains only the string 'Done'.
据我所知,我使用的代码是正确的,但它创建的文件只包含字符串‘Done’。
const dhh = fs.createWriteStream("./dhh-interview.mp3");
clips.forEach((clip) => {
currentfile = `${dirPath}\\${clip}`;
stream = fs.createReadStream(currentfile);
stream.pipe(dhh, { end: false });
stream.on("end", function () {
console.log(currentfile + " appended");
});
});
clips is an array containing the filenames of the files I want concatenating.
I've done a console.log on currentfile so I know it's pointing to where it should.
Clip是一个数组,其中包含我想要连接的文件的文件名。我已经在当前文件上做了一次sole.log,所以我知道它指向它应该指向的地方。
更多回答
You can't concatenate mp3 files as raw bitstreams. You need to read in the file, decode the audio, concatenate the audio streams (it may be stereo for example), re-encode as mp3 (which may result in additional lossy compression) and write the file back out, setting the appropriate meta data, etc. Another option is to write out a file that has chapters, where the two original audio streams become separate chapters, and possibly skip the decode re-encode steps. That still leaves with with not being able to treat it as raw bitstreams.
您不能将mp3文件连接为原始比特流。您需要读入文件、解码音频、连接音频流(例如,它可以是立体声)、重新编码为MP3(这可能导致额外的有损压缩)并将文件写回、设置适当的元数据等。另一种选择是写出具有章节的文件,其中两个原始音频流成为单独的章节,并且可能跳过解码重新编码步骤。这仍然留下了无法将其视为原始比特流的问题。
It looks like the issue you're facing is related to how you're using the stream.pipe method and the way you're handling the end event for each stream. The code you provided is trying to concatenate multiple MP3 files, but it's not handling the MP3 file format correctly, which is why you're getting "Done" in the output file.
看起来您面临的问题与您如何使用Stream.Tube方法以及处理每个流的End事件的方式有关。您提供的代码试图连接多个MP3文件,但它没有正确处理MP3文件格式,这就是为什么您在输出文件中完成了该操作。
To concatenate MP3 files properly, you should use a library that understands the MP3 format and can concatenate the audio streams correctly. One such library is ffmpeg, which is a powerful multimedia framework. You can use the ffmpeg command-line tool or a Node.js wrapper like fluent-ffmpeg to achieve this.
要正确连接MP3文件,您应该使用理解MP3格式并且可以正确连接音频流的库。Ffmpeg就是这样一个库,它是一个强大的多媒体框架。您可以使用ffmpeg命令行工具或像fluent-ffmpeg这样的Node.js包装器来实现这一点。
Here's how you can concatenate MP3 files using fluent-ffmpeg:
以下是使用fluent-ffmpeg连接MP3文件的方法:
First, install the fluent-ffmpeg library if you haven't already:
首先,安装fluent-ffmpeg库(如果尚未安装):
npm install fluent-ffmpeg
Then, use the following code to concatenate your MP3 files:
然后,使用以下代码连接您的MP3文件:
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const clips = ['file1.mp3', 'file2.mp3', 'file3.mp3']; // Replace with your file names
const outputFilePath = './concatenated.mp3'; // Replace with your desired output file path
const concat = ffmpeg();
clips.forEach((clip) => {
concat.input(clip);
});
concat
.on('end', function () {
console.log('Concatenation finished.');
})
.on('error', function (err) {
console.error('Error:', err);
})
.mergeToFile(outputFilePath, './tmp')
.run();
Make sure to replace 'file1.mp3', 'file2.mp3', etc., with the actual file paths of your MP3 files and './concatenated.mp3' with your desired output file path.
请确保将‘file1.mp3’、‘file2.mp3’等替换为MP3文件的实际文件路径,并将‘./linatenated.mp3’替换为所需的输出文件路径。
This code uses fluent-ffmpeg to concatenate the MP3 files correctly, and it should produce the expected output without the "Done" issue you mentioned.
这段代码使用fluent-ffmpeg来正确地连接MP3文件,它应该会产生预期的输出,而不会出现您提到的“Done”问题。
更多回答
I think this is nearly there but I'm getting the following error message: 'Error: No output specified'.
我认为这差不多是正确的,但我收到以下错误消息:‘Error:No Output Specify’(错误:未指定输出)。
It seems like you haven't specified the output file for the merged MP3. You should provide the output file path as an argument to the mergeToFile function.
您似乎还没有为合并后的MP3指定输出文件。您应该将输出文件路径作为参数提供给mergeToFile函数。
I've used: const outputFilePath = './concatenated.mp3'; & ``` concat .on('end', function () { console.log('Concatenation finished.'); }) .on('error', function (err) { console.error('Error:', err); }) .mergeToFile(outputFilePath, './tmp') .run(); }; ```
我用过:const outputFilePath=‘./linatenated.mp3’;&`conat.on(‘end’,函数(){console.log(‘拼接已完成.’);}).on(‘error’,函数(Err){console.Error(‘error:’,err);}).mergeToFile(outputFilePath,‘./tMP’).run();};`
To clarify my previous comment: I've set outputFilePath to './concatenated.mp3' & the concat part is exactly as specified above.
为了澄清我前面的评论:我已经将outputFilePath设置为‘./linatenated.mp3’&连接部分与上面指定的完全相同。
Maybe 2 possibilities : 1 ) Ensure that the ./tmp directory exists or choose a different temporary directory for processing 2) Ensure also that the "clips" array is correctly set.
可能有两种可能性:1)确保./tmp目录存在,或者选择不同的临时目录进行处理2)还要确保正确设置了“Clip”数组。
我是一名优秀的程序员,十分优秀!