使用python中的ffmpeg合并音频和视频

发布于 2025-02-06 14:11:47 字数 952 浏览 3 评论 0原文

我正在尝试将两个文件(.mp3& mp4)合并到一个.mp4。

视频和音频位于与我的main..py的同一文件夹中,并且在这里没有损坏,

这似乎是创建的代码错误:

    if (haveAudio):
        infile1 = ffmpeg.input(title+"_video.mp4")
        infile2 = ffmpeg.input(title+"_audio.mp3")

        merged  = ffmpeg.concat(infile1, infile2, v=1, a=1)
        output  = ffmpeg.output(merged[0], merged[1], title+".mp4")

我在最后一行中遇到了一个错误:

Traceback (most recent call last):
  File "C:\Users\...\projectName\main.py", line 67, in <module>
    output  = ffmpeg.output(merged[0], merged[1], title+".mp4")
  File "C:\Users\...\Python\Python39\lib\site-packages\ffmpeg\nodes.py", line 70, in __getitem__
    raise TypeError("Expected string index (e.g. 'a'); got {!r}".format(index))
TypeError: Expected string index (e.g. 'a'); got 0

我的猜测是调用ffmpeg.output()时缺少一个参数,但基于文档似乎是正确的。

I am trying to merge two files (.mp3 & .mp4) to a single .mp4

The videos and audio are located in the same folder as my main.py, and aren't corrupted

Here is the code that seems to create the error:

    if (haveAudio):
        infile1 = ffmpeg.input(title+"_video.mp4")
        infile2 = ffmpeg.input(title+"_audio.mp3")

        merged  = ffmpeg.concat(infile1, infile2, v=1, a=1)
        output  = ffmpeg.output(merged[0], merged[1], title+".mp4")

I am getting an error on the last line:

Traceback (most recent call last):
  File "C:\Users\...\projectName\main.py", line 67, in <module>
    output  = ffmpeg.output(merged[0], merged[1], title+".mp4")
  File "C:\Users\...\Python\Python39\lib\site-packages\ffmpeg\nodes.py", line 70, in __getitem__
    raise TypeError("Expected string index (e.g. 'a'); got {!r}".format(index))
TypeError: Expected string index (e.g. 'a'); got 0

My guess would be that an argument is missing when calling ffmpeg.output() but based on the documentation it seems correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2025-02-13 14:11:47

我从本教程中学到了: https://github.com/github.com/jnyh/pytube/ blob/master/pytube_sample_code.ipynb

我使用了以下2种方法,它们都为我工作:

audio = ffmpeg.input('audio.mp3')
video = ffmpeg.input('video.mp4')

ffmpeg.output(audio, video, 'my_file.mp4').run()

ffmpeg.concat(video, audio, v=1, a=1).output('my_file.mp4').run(overwrite_output=True)

I learnt from this tutorial: https://github.com/JNYH/pytube/blob/master/pytube_sample_code.ipynb

I used below 2 ways and they both worked for me:

audio = ffmpeg.input('audio.mp3')
video = ffmpeg.input('video.mp4')

ffmpeg.output(audio, video, 'my_file.mp4').run()

or

ffmpeg.concat(video, audio, v=1, a=1).output('my_file.mp4').run(overwrite_output=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文