【问题标题】:How to combine The video and audio files in ffmpeg-python如何在 ffmpeg-python 中合并视频和音频文件
【发布时间】:2019-11-20 05:29:13
【问题描述】:

我正在尝试将视频(没有声音)与其单独的音频文件结合起来

我试过 ffmpeg ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4 而且效果很好。

我正在尝试从 ffmpeg-python 获得相同的输出,但没有运气。有关如何执行此操作的任何帮助?

【问题讨论】:

    标签: python ffmpeg


    【解决方案1】:

    我遇到了同样的问题。

    这是在您的环境中拥有pip install ffmpeg-python 后的python 代码:

    import ffmpeg
    
    input_video = ffmpeg.input('./test/test_video.webm')
    
    input_audio = ffmpeg.input('./test/test_audio.webm')
    
    ffmpeg.concat(input_video, input_audio, v=1, a=1).output('./processed_folder/finished_video.mp4').run()
    
    

    v=1: 设置输出视频流的个数,也就是每个段的视频流个数。默认为 1。

    a=1:设置输出音频流的个数,也就是每个段的音频流个数。默认为 0。

    ffmpeg.concat的详细信息,请查看:https://ffmpeg.org/ffmpeg-filters.html#concat

    您可以在此处查看更多示例:https://github.com/kkroening/ffmpeg-python/issues/281

    希望这会有所帮助!

    附言。 如果您使用的是 MacOS 并出现错误: FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg' 运行代码时,只需在终端中使用 brew install ffmpeg

    【讨论】:

      【解决方案2】:

      你可以使用subprocess:

      import subprocess    
      subprocess.run("ffmpeg -i video.mp4 -i audio.mp4 -c copy output.mp4")
      

      您也可以使用fstrings 来使用变量名作为输入:

      videofile = "video.mp4"
      audiofile = "audio.mp4"
      outputfile = "output.mp4"
      codec = "copy"
      subprocess.run(f"ffmpeg -i {videofile} -i {audiofile} -c {codec} {outputfile}")
      

      【讨论】:

      • 欢迎来到 Stack Overflow!这不是这个人所要求的。他知道如何使用“ffmpeg”将音频与视频结合起来,但他想使用“ffmpeg-python”包将音频与视频结合起来。
      【解决方案3】:
      import ffmpeg
      input_video = ffmpeg.input("../resources/video_with_audio.mp4")
      added_audio = ffmpeg.input("../resources/dance_beat.ogg").audio.filter('adelay', "1500|1500")
      merged_audio = ffmpeg.filter([input_video.audio, added_audio], 'amix')
      (ffmpeg
      .concat(input_video, merged_audio, v=1, a=1)
      .output("mix_delayed_audio.mp4")
      .run(overwrite_output=True))
      

      您可以查看此链接https://github.com/kkroening/ffmpeg-python/issues/281#issuecomment-546724993

      【讨论】:

      • 我收到此错误:ValueError: Encountered atempo(2) <721e83834c8e> with multiple outgoing edges with same upstream label None; a split` 可能需要过滤器`
      【解决方案4】:

      添加了以下代码: https://github.com/russellstrei/combineViaFFMPEG

      它遍历目录,找到“.mp4”文件,将其添加到ffmpeg要使用的文件中,然后执行命令。

      for name in glob.glob(directory +"\\"+ '*.mp4'):
          print(name)
          file1 = open(processList, "a")  # append mode
          file1.write("file '" + name + "'\n")
          file1.close()
          execute()
      
      def execute():
          cmd = "ffmpeg -f concat -safe 0 -i " + processList + " -c copy "+ dir + "output.mp4"
          os.system(cmd)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 2019-05-04
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多