If you want to use external files with your project you need to bundle it to your output executable. Which whenever your executable runs it would extract all dependencies into the temp directory and use it in your code. For this, you can add-data
flag.
First, download precompiled binaries from here. Then extract contents to a folder called ffmpeg
(next to script file). In below example, the app (script.py
) tries to play a sample video with ffplay
. I assumed that the path of input video would be passed as an external argument to the program.
import subprocess
import os
import sys
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
def play():
ffplay_path = "./ffmpeg/ffplay.exe"
if len(sys.argv) > 1:
file_path = sys.argv[1]
p = subprocess.Popen([resource_path(ffplay_path), file_path])
else:
print("No file passed as argument!")
if __name__ == "__main__":
play()
Use below command to generate the executable:
pyinstaller -F --add-data "./ffmpeg/*;./ffmpeg/" script.py
And use the program like this:
script.exe sample.mp4