Search code examples
pythonpyinstalleropencv3.0

Exe file build by Pyinstaller can not save video with cv2.VideoWriter


I have written a python script called videoSave.py to read camera and save video. The code is as below:

import cv2


def saveCam():
    video = cv2.VideoCapture(0)
    ret, img = video.read()

    h, w = img.shape[:2]
    videoSaver = cv2.VideoWriter("videoSave_test.avi", cv2.VideoWriter_fourcc(*'DIVX'), 10, (w, h))

    key = 0
    while key != 27:
        ret, img = video.read()
        if not ret:
            break

        cv2.imshow('img', img)
        key = cv2.waitKey(1) & 0xFF
        videoSaver.write(img)

    videoSaver.release()


if __name__ == '__main__':
    saveCam()

This script works well when runed directly. It can show the image obtained by the camera and save the content to a video file.

Then I build this script into exe file using command pyinstaller -F videoSave.py. I can get the exe file. And when excuting it, I can see the image obtained by the camera, but the video file saved by it has zero size!

My environment is:
Python 3.7.4
Windows 7
Pyinstaller 3.5
opencv 4.1.2


Solution

  • I have solved this problem by copying opencv_video_ffmpeg412_64.dll from Anaconda3\envs\your_env_name to the dir where exe file lies. But I donot know why.