Home > Net >  How can I grab 2 things from 2 different def functions and use them in a different def
How can I grab 2 things from 2 different def functions and use them in a different def

Time:02-04

I'm trying to grab both the image and audio from those and use them both in another def, how would I do that?

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))
def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))

This is the def I'm trying to use them in.

def create():
    os.system(f"ffmpeg -i {image} -i {audio} -vf scale=1920:1080 output.mp4")

CodePudding user response:

You need to return the value of image and audio, like so:

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))

    return image

def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))

    return audio

And then use them:

def create():
    image = image()
    audio = audio()
    
    os.system(f"ffmpeg -i {image} -i {audio} -vf scale=1920:1080 output.mp4")

Additionally, it'd probably be a good idea to wrap the file names in shlex.quote to make it work with file names that have spaces in them and files named by a malicious user.

CodePudding user response:

Just return values from the other functions and call them in the function which needs these values.

def image():
    image = filedialog.askopenfilename(filetypes = (("Image Files",".png .jpg"),("all files","*.*")))
    return image

def audio():
    audio = filedialog.askopenfilename(filetypes = (("Audio Files",".mp3 .wav"),("all files","*.*")))
    return audio

def create():
    os.system(f"ffmpeg -i {image()} -i {audio()} -vf scale=1920:1080 output.mp4")

What actually happens when you return a value:

Ex 1.

def example():
    a = "Hello"
    return a
value = example() # --> value = a = "Hello"
  •  Tags:  
  • Related