Search code examples
python-2.7py2exepyinstallercx-freezemoviepy

Python - Console closes after the code has been frozen


I wrote an app in Python 2.7 that retrieves the length of media files using moviepy. If I run it from the command line, everything works fine; but after freezing the code, when I run it the console window closes immediately. I've tried cx_freeze, pyinstaller and py2exe, all with the same results. Is there something wrong with my code or is this an issue with moviepy? I'm testing on Windows 10, this will be used on Windows 7 in the end. Here's the code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Video-Audio Length Retriever
#
# Version: 0719A
#
# Author: Simon Lachaîne


import codecs
from moviepy.editor import VideoFileClip, AudioFileClip
import os


directories = []


def read_directories():
    global directories
    directories_txt = raw_input("Enter the path and name of the text file containing the source directories: ")

    with codecs.open(directories_txt, "r", encoding="utf8") as source_dirs:
        directories = [line.rstrip() for line in source_dirs]


def write_text(report, text2save):
    with open(report, "a") as report:
        report.write(text2save)


def check_duration():
    for directory in directories:
        for root, dirs, files in os.walk(directory):
            os.chdir(root)
            for fichier in files:

                try:
                    video = VideoFileClip(fichier)

                    m, s = divmod(video.duration, 60)
                    h, m = divmod(m, 60)

                    length = fichier + " ; " + "%02d:%02d:%02d\n" % (h, m, s)
                    write_text(durations_report, length)
                    print "Processed file " + fichier

                except IOError:
                    pass

                except KeyError:
                    try:
                        audio = AudioFileClip(fichier)

                        m, s = divmod(audio.duration, 60)
                        h, m = divmod(m, 60)

                        length = fichier + " ; " + "%02d:%02d:%02d\n" % (h, m, s)
                        write_text(durations_report, length)
                        print "Processed file " + fichier

                    except IOError:
                        pass


read_directories()
durations_report = raw_input("Enter the path and name of the report to create: ")
check_duration()

Solution

  • You can run your frozen code from command line to view the error message.

    As far as pyinstaller is concerned, I can't see a hook in the hooks folder for moviepy and most likely this was not bundled in the frozen version. You can add it (or anything else that might be missing) as a hidden import: https://pythonhosted.org/PyInstaller/when-things-go-wrong.html?highlight=hidden#listing-hidden-imports