簡體   English   中英

如何從 2 個不同的 def 函數中獲取 2 個東西並在不同的 def 中使用它們

[英]How can I grab 2 things from 2 different def functions and use them in a different def

我正在嘗試從中獲取圖像和音頻,並在另一個 def 中同時使用它們,我該怎么做?

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

這是我試圖使用它們的定義。

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

您需要返回imageaudio的值,如下所示:

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():
    image = image()
    audio = audio()
    
    os.system(f"ffmpeg -i {image} -i {audio} -vf scale=1920:1080 output.mp4")

此外,將文件名包裝在shlex.quote中可能是一個好主意,以使其與包含空格的文件名和惡意用戶命名的文件一起使用。

只需從其他函數返回值並在需要這些值的 function 中調用它們。

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")

返回值時實際發生的情況:

例 1。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM