You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ffmpeg-python开发tkinter工具时遇AttributeError错误求助

问题解决方法

1. 修复ffmpeg-python调用错误

FilterableStream对象确实没有input方法,因为input()是ffmpeg-python模块的顶层函数,用于创建新的输入流,而非流对象的方法。正确的图片音频合成逻辑应该是分别创建图片和音频的输入流,再合并输出:

import ffmpeg

def create_video(image_path, audio_path, output_path):
    # 创建图片输入流,loop=1表示循环播放图片直到音频结束
    img_stream = ffmpeg.input(image_path, loop=1)
    # 创建音频输入流
    audio_stream = ffmpeg.input(audio_path)
    # 合并流并输出,shortest参数确保视频时长与音频一致
    ffmpeg.output(img_stream, audio_stream, output_path, 
                  vcodec='libx264', acodec='aac', shortest=True).run()

2. 修复tkinter按钮command传参问题

直接将command=create_video(...)会导致函数在按钮创建时立即执行,且错误传递参数。需要用lambda或functools.partial包装函数,实现点击按钮时才执行并传入正确的文件路径:

方法一:使用lambda

假设你用tkinter的StringVar存储选中的文件路径:

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
img_path_var = tk.StringVar()
audio_path_var = tk.StringVar()

# 示例:选择图片文件的函数
def select_image():
    path = filedialog.askopenfilename(filetypes=[("图片文件", "*.png;*.jpg")])
    img_path_var.set(path)

# 示例:选择音频文件的函数
def select_audio():
    path = filedialog.askopenfilename(filetypes=[("音频文件", "*.mp3;*.wav")])
    audio_path_var.set(path)

def create_video(image_path, audio_path, output_path):
    # 上面的ffmpeg逻辑

# 创建按钮
select_img_btn = tk.Button(root, text="选择图片", command=select_image)
select_audio_btn = tk.Button(root, text="选择音频", command=select_audio)
convert_btn = tk.Button(
    root, 
    text="合成视频", 
    command=lambda: create_video(
        img_path_var.get(), 
        audio_path_var.get(), 
        "output.mp4"
    )
)

select_img_btn.pack()
select_audio_btn.pack()
convert_btn.pack()
root.mainloop()

方法二:使用functools.partial

如果路径是已获取的固定变量,也可以用partial绑定参数:

from functools import partial
import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
img_path = ""
audio_path = ""

def select_image():
    global img_path
    img_path = filedialog.askopenfilename(filetypes=[("图片文件", "*.png;*.jpg")])

def select_audio():
    global audio_path
    audio_path = filedialog.askopenfilename(filetypes=[("音频文件", "*.mp3;*.wav")])

def create_video(image_path, audio_path, output_path):
    # 上面的ffmpeg逻辑

convert_btn = tk.Button(
    root, 
    text="合成视频", 
    command=partial(create_video, img_path, audio_path, "output.mp4")
)

select_img_btn = tk.Button(root, text="选择图片", command=select_image)
select_audio_btn = tk.Button(root, text="选择音频", command=select_audio)
select_img_btn.pack()
select_audio_btn.pack()
convert_btn.pack()
root.mainloop()

内容的提问来源于stack exchange,提问作者Karolek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 02:50:10