3

I wrote a Python program for Windows 10 which converts audio files from any supported format to .flac, and it uses FFMPEG to do the encoding.

FFMPEG is installed on my computer, so there isn't any problem when I run the program on my computer.

I used PyInstaller to export my program in .exe, and now I want to include FFMPEG in the distribution so people don't have to download it separately to run my script.

Can I wrap/include FFMPEG into my program, and automatically install/use it when required? How?

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67

1 Answers1

6

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
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67