【问题标题】:Combine two audio files in Python在 Python 中合并两个音频文件
【发布时间】:2021-06-29 09:24:58
【问题描述】:

我有两个音频文件:

'Users/x/Songs/1.mp4'
'Users/x/Songs/2.mp4'

我想将这些合并到一个音频文件中,两个文件在其中一个接一个地播放:

'Users/x/Songs/combined.mp4'

我在我的 Macbook 上使用 PyCharm 来实现这一点。有没有办法做到这一点? 我看到一个解决方案是使用这段代码:

from glob import iglob
import shutil
import os

PATH = r'/Users/x/Songs'

destination = open('everything.mp4', 'wb')
for filename in iglob(os.path.join(PATH, '*.mp4')):
    shutil.copyfileobj(open(filename, 'rb'), destination)
destination.close()

但每当我这样做时,我只会得到名为“everything.mp4”的文件中的最后一个音频剪辑。

不确定上面的代码做了什么,但发现它是对另一个问题的答案,比如我的问题。

【问题讨论】:

    标签: python audio pycharm append


    【解决方案1】:

    我有点困惑为什么您使用 mp4(视频)文件作为音频。

    stack overflow post about joining wav files 可能与python AudioConverter module 结合使用

    它可能看起来像这样:

    import audioconverter
    import audiolab, scipy
    audioconvert convert wavfiledir/ outputdir/ --output-format .wav
    a, fs, enc = audiolab.wavread('file1.wav')
    b, fs, enc = audiolab.wavread('file2.wav')
    c = scipy.vstack((a,b))
    audiolab.wavwrite(c, 'file3.wav', fs, enc)
    

    请记住,您将需要 ffmpeg,使用此解决方案您将失去 任何视频(不是音频)(如果有的话)。

    其他几个可能的解决方案 https://www.codegrepper.com/code-examples/python/merge+all+mp4+video+files+into+one+file+python

    https://pythonprogramming.altervista.org/join-all-mp4-with-python-and-ffmpeg/

    https://gist.github.com/rohitrangan/3841212

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 2015-06-22
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多