Trying to convert Python scripts to exe with PyInstaller.
In my code, I use ffmpeg-python:
import ffmpeg
....
def ffmpeg_save_clip(self,output_video: str, clip_start: str, clip_end: str):
(ffmpeg
.input(self.file.get_videopath(), )
.output(output_video, vcodec='copy', ss=clip_start, to=clip_end, acodec='copy')
.global_args('-y')
.run())
So Ii call PyInstaller from terminal with related flag:
pyinstaller --windowed --hidden-import "ffmpeg-python" --noconsole --icon=app.ico --name "app" main.py
I checked also:
pip install ffmpeg-python
Requirement already satisfied: ffmpeg-python in c:python38libsite-packages (0.2.0)
Requirement already satisfied: future in c:python38libsite-packages (from ffmpeg-python) (0.18.3)
But I get from PyInstaller:
Hidden import 'ffmpeg-python' not found
App works in visual-studio, but when I run pyinstaller exe and try to save clip, it freeze without response.
Note: I also:
-
try to add ffmpeg.exe into root folder of pyinstaller app.
-
try to use .spec file with:
binaries=[('C:\ffmpeg\bin\ffmpeg.exe', 'ffmpeg/),('C:\ffmpeg\bin\ffplay.exe','ffplay/'), ('C:\ffmpeg\bin\ffprobe.exe', 'ffprobe/')]
,
Nothing changes
UPDATE
Tank you @happy_code_egg, you explained me such a things.
I tried what you said and I don’t have error any more.
But I can’t make work ffmpeg anywhere on Pyinstaller exe.
I added a try/except block to isolate problem
(when I launch app on Visualstudio it works, but not when use exe).
block is (very symilar to past def):
def ffmpeg_save_clip(self,output_video: str, clip_start: str, clip_end: str):
input = 'C:UsersxDesktopinput.mp4'
output = 'C:UsersxDesktopoutput.mp4'
try:
(ffmpeg
.input(input)
.output(output, vcodec='copy', ss=clip_start, to=clip_end, acodec='copy')
.global_args('-y')
.run(capture_stdout=True))
except Exception as e:
self.logger.error('Video window -> ffmpeg_save_clip -> error ' + str(e))
self.logger.error('Video window -> ffmpeg_save_clip -> input file: ' + input)
self.logger.error('Video window -> ffmpeg_save_clip -> output file ' + output)
raise ValueError(str(e))
Note: (input and output fixed are only as simple path examples)
When I open log I have:
2024-08-09 11:28:50,293 - ERROR - Video window -> ffmpeg_save_clip -> error [WinError 2] File not found error
2024-08-09 11:28:50,293 - ERROR - Video window -> ffmpeg_save_clip -> input file: C:UsersxDesktopinput.mp4
2024-08-09 11:28:50,293 - ERROR - Video window -> ffmpeg_save_clip -> output file C:UsersxDesktopouput.mp4
(I tried with ‘/’ and also ” to create path)
File paths are correct and, on Visualstudio, they can be processed by ffmpeg (no error), but the exe seems to not find (input or output)
I tried solution described here:
Python ffmpeg won’t accept path
(without effects)
I found also these discussions:
in both discussions seems to be a problem with ffmpeg + ffmpeg-python libs cohexistence.
I tried (as said) to uninstal both and reinstall ffmpeg-python,
but nothing change.
Note:
I’ve also three other warning during Pyinstaller:
(I write them only to complete context, but i think they have no repercussions on this problem.)
57283 ERROR: Hidden import 'fiona._shim' not found
57298 WARNING: Hidden import "fiona._shim" not found!
57392 WARNING: Hidden import "importlib_resources.trees" not found!
tried with:
--hidden-import fiona._shim --hidden-import fiona.schema
tried also with:
--add-data="fiona/*;fiona”
but warnings still remains.
2
Answers
For anyone encountered this issue, I finally found a way to make it work. tank you for @happy_code_egg support (and it's explaining capacity)
To solve ffmpeg-python including to pyinstaller, follow what @happy_code_egg said.
Anyway (as I wrote in update section of my question), it still remained a problem: using ffmpeg python command in pyinstaller .exe I had:
It was not referenced to video file, but to ffmpeg.exe itself. (app was not able to find ffmpeg.exe) As said also by @happy_code_egg in comment.
to make it works you can use ffmpeg flag to find ffmpeg.exe:
Adding cmd flag it works!
Anyway still remains a big problem.. Ffmpeg should be installed on the host computer witch uses pyinstaller exe!
I'm trying to solve this issue, placing ffmpeg.exe into main.py root app folder. but it's not possible to add a relative path as:
because command would be:
but it not works
I can't find a solution to add to cmd=r'..' a relative path Any suggestion is well received
PyInstaller didn’t pack the
ffmpeg-python
module, notffmpeg.exe
.First, go to
c:python38libsite-packages
and copy theffmpeg
package folder.Next, paste the
ffmpeg
package folder at your project’s root directory.At last, build the exe. Whatever your command is, you should use
--add-data
command to add theffmpeg
package folder when building exe.In your case, use this command.
--add-data="ffmpeg/*;ffmpeg"
this means to add all the files and subfolders inside theffmpeg
folder to theffmpeg
folder created inside the exe or app folder.This should solve the problem. If there are other modules not found, use the same steps to pack the modules.
By the way, you can also directly copy and paste the
ffmpeg
folder to the root folder of pyinstaller app. In that case, you don’t need to use the--add-data
command.