如何将不同格式的音频文件合并成一个MP3文件,使用FFmpeg或PHP?

huangapple go评论71阅读模式
英文:

how to concatenate different audio files in different format into one mp3 file in ffpmeg or php?

问题

我允许用户上传具有扩展名mp3、wav、flac的音频文件。使用以下命令,我可以仅在音频文件为mp3格式时连接音频文件。

ffmpeg -i "concat:sample1.mp3|sample2.mp3" -c:a libmp3lame -write_xing 0 merged.mp3

如果音频文件以不同的格式和编码存在,我该如何使用ffmpeg或php进行合并?
在ffmpeg文档中,提供了一个示例,用于连接不同编码的文件,但示例包括像mp4、webm、mov之类的视频文件,而我需要的是仅限音频的示例。
FFmpeg连接的文档

-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mkv```

<details>
<summary>英文:</summary>

I am allowing users to upload audio files with file extensions mp3, wav, flac. With the following command, I can concatenate audio files if it is in mp3 only.

```ffmpeg -i &quot;concat:sample1.mp3|sample2.mp3&quot; -c:a libmp3lame -write_xing 0 merged.mp3```

How can i merge if the audio files are in different formats and encoding using ffmpeg or php?
In the ffmpeg documentation, it provides an example for concatenating files with different codes but the example includes video files like mp4,webm,mov but i need example for audio only.
[FFmpeg documentation of concatenate](https://trac.ffmpeg.org/wiki/Concatenate)

```ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \
-filter_complex &quot;[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]&quot; \
-map &quot;[outv]&quot; -map &quot;[outa]&quot; output.mkv```



</details>


# 答案1
**得分**: 2

Sure, here are the translated parts:

**-i sample1.mp3**  **-i sample2.wav**  **-i sample3.flac** : 指定了音频文件。

**-filter_complex &quot;[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa]** : filter_complex 连接了三个输入文件的音频流。

**[0:a][1:a][2:a]** 表示三个音频流,**concat=n=3:v=0:a=1[outa]** 将它们连接成一个单一的音频流 **[outa]**。

**-map [outa]** : [outa] 指定了输出应该包括音频。

**-c:a libmp3lame** : 用于将合并后的音频编码为MP3文件。

**merged.mp3** : 指定了输出文件的名称。

<details>
<summary>英文:</summary>

    ffmpeg -i sample1.mp3 -i sample2.wav -i sample3.flac -filter_complex &quot;[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa]&quot; -map &quot;[outa]&quot; -c:a libmp3lame merged.mp3

**-i sample1.mp3**  **-i sample2.wav**  **-i sample3.flac** : Audio files are specified.

**-filter_complex &quot;[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa]** : The filter_complex concatenates the audio streams from the three input files.

**[0:a][1:a][2:a]** Three audio streams are represented, and **concat=n=3:v=0:a=1[outa]** concatenates them into a single audio stream **[outa]**.
 
The **v=0** Parameter indicates no video streams.

**-map [outa]** : [outa] specifies the output should include audio.

**-c:a libmp3lame** : Is used to encode the merged audio as an MP3 file.

**merged.mp3** : Specifies the name of the output file.

</details>



huangapple
  • 本文由 发表于 2023年5月25日 12:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定