0

I am a fresh programmer and I am learning the Python language. I have been trying to design a graphical application (using tkinter and ffmpeg-python libraries) that converts an image file along with audio into a video of audio length displaying the image. Unfortunately whenever I try run program I keep encountering errors

At first it was due to installing the ffmpeg library in pip package manager instead of ffmpeg-python. I uninstalled the previous one, installing the correct one making sure no other package causes problems. Using a short script like this, the library functioned smoothly:

'''
The Error causing Statement
import ffmpeg 
(
    ffmpeg.input("something.mp3")
    .output("output.wav")
    .run()
)
'''
# When I tried to use the ffmpeg library in a graphics program within a function, unfortunately I was not able to get it to work

import tkinter as tk
from tkinter import filedialog
from tkinter import ttk

def select_image():
    file_path = filedialog.askopenfilename(title="Select a file", filetypes=[("image files","*.jpg;*.png;*.gif;*.jfif;")])
    print("Selected file:", file_path)

def select_audio():
    file_path = filedialog.askopenfilename(title="Select a file", filetypes=[("audio files","*.mp3;*.wav;*.ogg;*.flac;*.m4a;*.acc;")])
    print("Selected file:", file_path)

def create_video(image_file, audio_file, output_format):
    import ffmpeg
    (
        ffmpeg.input(image_file, loop=1)
        .input(audio_file)
        .output("output" + output_format)
        .run()
    )   

def choose_directory():
    directory_path = filedialog.askdirectory(title="Select directory")
    print("Selected directory:", directory_path)


root = tk.Tk()

root.title("Audio To Video Converter")
root.geometry("500x700")

title = tk.Label(root, text="Audio To Video Converter", font=('Arial', 28))
title.pack(padx=20, pady=20)

selectImage_button = tk.Button(root, text="Select image", command=select_image, height=3, width=50)
selectImage_button.pack(pady=10, padx=20)
selectAudio_button = tk.Button(root, text="Select audio", command=select_audio, height=3, width=50)
selectAudio_button.pack(pady=10, padx=20)

label1 = tk.Label(root, text="Output video format", font=('Arial', 16))
label1.pack(padx=20, pady=10)

outputFormat = ttk.Combobox(root, width=55)
outputFormat['values'] = ('.avi', '.mkv', '.mp4', '.webm')
outputFormat['state'] = 'readonly'
outputFormat.pack(pady=5, padx=20)

label2 = tk.Label(root, text="Output directory", font=('Arial', 16))
label2.pack(padx=20, pady=10)

outputDirectory_button = tk.Button(root, text="Select directory", command=choose_directory, height=3, width=50)
outputDirectory_button.pack(pady=5, padx=60)


convert_button = tk.Button(root, text="Convert", height=3, width=50, command=create_video(selectImage_button, selectAudio_button, outputFormat['values'] ))

root.mainloop()

This is the only thing the console displayed after attempting to run

Traceback (most recent call last):
  File "c:\Users\Admin\Documents\konwerter\konwerter.py", line 56, in <module>
    convert_button = tk.Button(root, text="Convert", height=3, width=50, command=create_video(selectImage_button, selectAudio_button, outputFormat['values'] ))
                                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\Admin\Documents\konwerter\konwerter.py", line 17, in create_video
    .input(audio_file)
     ^^^^^
AttributeError: 'FilterableStream' object has no attribute 'input'

I did some research and did not find any information about this error except that it may be due to installing the wrong libraries. This is my list of installed libraries in pip package manager:

colored           2.2.4 customtkinter     5.2.2 darkdetect        0.8.0 ffmpeg-python     0.2.0 future            1.0.0 Gooey             1.0.8.1 packaging         24.1 pillow            10.4.0 pip               24.2 psutil            6.0.0 pyee              12.0.0 pygtrie           2.5.0 six               1.16.0 tkinterdnd2       0.4.2 typing_extensions 4.12.2 wxPython          4.2.2

Perhaps the solution to the problem is simple, but I am new and don't understand many things. I will be very grateful for your answer!

3
  • 1
    Why are you passing in a tk.Button in create_video? Also you have this problem with your code. I also recommend that you read this Wikipedia article because right now the path for the selected image/audio are only being printed out and never stored in a variable accessable from create_video. Commented Sep 26, 2024 at 10:16
  • Welcome to stackoverflow, in future question please show your research also into the question. Commented Sep 26, 2024 at 10:16
  • @FaraazKurawle okay, thank u for info. Commented Sep 26, 2024 at 11:43

1 Answer 1

0

I cannot comment over the function's of ffmpeg, but your tkinter program has some logical errors within it.

  1. select_audio, select_image and choose_directory function does'nt return any value, add the value to a tkinter variable ext. Therefore once function ends, you dont have access to those values.

  2. tkinter.Button object's command parameter accepts only refrence to the function to be executed, like you did in:

    selectImage_button = tk.Button(root, text="Select image", command=select_image, height=3, width=50)

    but in:

    convert_button = tk.Button(root, text="Convert", height=3, width=50, command=create_video(options))

    you have passed the returned value of that function that is of create_video. The solution would be to use command= lambda: create_video(options).

  3. In:

    convert_button = tk.Button(root, text="Convert", height=3, width=50, command=create_video(selectImage_button, selectAudio_button, outputFormat['values'])))

    The tkinter widgets returns the widgets object and not the value of executing command function since currently thats what you were expecting. The solution would be to use tkinter variables.

Suggestion

I advice you to first learn Basics or Core Python thoroughly before making a step into GUI Programming.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! You are indeed correct and the program no longer returns the error. Thanks for noticing these errors, I will take them into consideration in the future when I encounter a similar problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.