Skip to content

Commit 55b34fe

Browse files
Update python companion app to always close out on finish
1 parent c6e1318 commit 55b34fe

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

PyMsuScripterApp/py_msu_scripter_app/video_creator.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def run(self) -> bool:
5353
return self.print_yaml(False, f"Track file {track_file} does not exist. Exiting.")
5454

5555
stop_event = threading.Event()
56-
t = threading.Thread(target=self.writer_thread, args=(self.progress_file, stop_event))
56+
t = threading.Thread(target=self.writer_thread, args=(self.progress_file, stop_event), daemon=True)
5757

5858
try:
5959
t.start()
@@ -72,19 +72,23 @@ def run(self) -> bool:
7272

7373
logger = MyBarLogger(self)
7474

75-
audio_clip = AudioFileClip(output_wav)
76-
video_clip = ColorClip(size=(720, 576), color=(0, 0, 0), duration=audio_clip.duration)
77-
video_clip = video_clip.with_audio(audio_clip)
78-
video_clip.write_videofile(output_mp4, fps=24, logger=logger)
75+
with AudioFileClip(output_wav) as audio_clip:
76+
with ColorClip(size=(720, 576), color=(0, 0, 0), duration=audio_clip.duration) as video_clip:
77+
video_clip = video_clip.with_audio(audio_clip)
78+
video_clip.write_videofile(output_mp4, fps=24, logger=logger)
7979

8080
return self.print_yaml(True, "")
8181
except Exception as e:
8282
print(f"Error creating wav or mp4 file: {str(e)}", file=sys.stderr)
8383
return self.print_yaml(False, f"Error creating wav or mp4 file {str(e)}")
8484
finally:
8585
stop_event.set()
86-
t.join()
87-
86+
t.join(timeout=2)
87+
try:
88+
audio_clip.close()
89+
video_clip.close()
90+
except Exception:
91+
pass
8892

8993
def print_yaml(self, successful: bool, error: str) -> bool:
9094
data = dict(
@@ -102,11 +106,14 @@ def print_yaml(self, successful: bool, error: str) -> bool:
102106

103107
def writer_thread(self, filename, stop_event):
104108
while not stop_event.is_set():
105-
with open(filename, "w") as f:
106-
print(f"writer thread {self.phase} {self.phase_progress}/100")
107-
f.write(f"{self.phase}|{self.phase_progress}\n")
108-
f.flush() # ensure immediate write
109-
time.sleep(0.5) # 500 ms
109+
try:
110+
with open(filename, "w") as f:
111+
print(f"writer thread {self.phase} {self.phase_progress}/100")
112+
f.write(f"{self.phase}|{self.phase_progress}\n")
113+
f.flush() # ensure immediate write
114+
except Exception:
115+
pass
116+
time.sleep(0.5) # 500 ms
110117

111118

112119
class MyBarLogger(ProgressBarLogger):

PyMsuScripterApp/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "py-msu-scripter-app"
3-
version = "0.1.6"
3+
version = "0.1.9"
44
description = ""
55
authors = ["MattEqualsCoder <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)