我用 Python 2.7 编写了一个应用程序,它使用 moviepy 检索媒体文件的长度。如果我从命令行运行它,一切正常;但是在冻结代码后,当我运行它时,控制台窗口会立即关闭。我试过 cx_freeze、pyinstaller 和 py2exe,结果都一样。我的代码有问题还是moviepy有问题?我正在 Windows 10 上进行测试,最终将在 Windows 7 上使用。这是代码:
#!/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()